Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: MouseMotionListener is not updating my variable

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default [SOLVED]MouseMotionListener is not updating my variable

    Hi,

    I've been having some trouble finding a way writing the code for a pupil following the mouse cursor and staying inside of the bounds. I've got all the math right, but I don't think my mouselistener is getting updated when I move my mouse. All feedback is highly appreciated.

    PS: I am not very good at writing code, so the tiniest mistake could be the solution.

    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.geom.Ellipse2D.Double;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class oving3stuff extends JApplet{
     
    	public static void main (String[]args) {
    		JFrame frame = new JFrame();
    		frame.setTitle("Øye");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JApplet applet = new oving3stuff();
    		applet.init();
    		frame.getContentPane().add(applet);
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	public void init() {
    		JPanel panel = new oving3panel();
    		getContentPane().add(panel);
    	}
    }
     
    class oving3panel extends JPanel implements MouseMotionListener{
     
    	private Ellipse2D.Double eye, pupil;
    	private Point2D.Double eyeCenter, pupilCenter;
    	private Point mouseHere = new Point();
    	Point testPoint = new Point(0,0);
    	private double w, h;
    	private double oX, oY, x, y;
    	private double eyeRadius, pupilRadius;
     
    	public oving3panel() {
    		setPreferredSize(new Dimension(640, 480));
    		setBackground(Color.white);
    	}
     
    	@Override public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D)g;
     
    		w = this.getWidth(); h = this.getHeight();
    		oX = w/2; oY = h/2; x = w/20; y = h/20;
    		eyeCenter = new Point2D.Double(oX, oY);
    		eyeRadius = x*5;
    		pupilRadius = x*2;
     
    		pupilCenter = getPupilCenterLoc(eyeCenter, mouseHere);
    		eye = new Ellipse2D.Double(oX-(x*5), oY-(y*5), x*10, y*10);
    		pupil = new Ellipse2D.Double(pupilCenter.x, pupilCenter.y, pupilRadius, pupilRadius);
     
    		g2.drawLine(0, (int)oY, (int)w, (int)oY);
    		g2.drawLine((int)oX, 0, (int)oX, (int)h);
    		g2.draw(eye);
    		g2.draw(pupil);
    	}
     
    	private Point2D.Double getPupilCenterLoc(Point2D.Double eyeCenter, Point mousePoint) {
    		double x1 = eyeCenter.x;
    		double y1 = eyeCenter.y;
     
    		double mouseX = mousePoint.getX();
    		double mouseY = mousePoint.getY();
    		Point2D.Double refPoint = new Point2D.Double(mouseX, mouseY);
     
    		double distance = getDistance(eyeCenter, refPoint);
    		double radius = eyeRadius - pupilRadius;
     
    		double x2 = x1 + ((mouseX - x1)*(radius/distance));
    		double y2 = y1 + ((mouseY - y1)*(radius/distance));
     
    		return new Point2D.Double(x2, y2);
    	}
     
    	private double getDistance(Point2D.Double p1, Point2D.Double p2) {
    		return Math.sqrt(square(p1.x - p2.x) + square(p1.y - p2.y));
    	}
     
    	private double square(double d) {
    		return d*d;
    	}
     
    	public void mouseDragged(MouseEvent e) {}
    	public void mouseMoved(MouseEvent e) {
    		mouseHere = e.getPoint();
    		System.out.println(e.getPoint().toString());
    		repaint();
    	}
    }

    When I run this code, the output is the picture under, where the smaller circle isn't moving when I move my cursor.



    Thanks in advance
    //Ole Martin
    Last edited by olemagro; February 8th, 2010 at 03:43 PM.


  2. #2
    Junior Member
    Join Date
    Jan 2010
    Posts
    17
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: MouseMotionListener is not updating my variable

    I forgot to add the listener to the constructor in the JPanel class. Minor but fatal error.

Similar Threads

  1. updating EDT with thread swingworker/invokeLater ?
    By mdstrauss in forum AWT / Java Swing
    Replies: 0
    Last Post: October 11th, 2009, 04:52 AM
  2. Replies: 0
    Last Post: October 10th, 2009, 01:25 PM
  3. updating database
    By gurpreetm13 in forum JDBC & Databases
    Replies: 3
    Last Post: October 9th, 2009, 11:43 AM
  4. application Task problem - updating JTree
    By idandush in forum AWT / Java Swing
    Replies: 2
    Last Post: June 18th, 2009, 03:15 AM
  5. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM