Go Back   Java Programming Forums > Java Standard Edition Programming Help > AWT / Java Swing


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-02-2010, 07:22 PM
Junior Member
 

Join Date: Jan 2010
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
olemagro is on a distinguished road
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.

Java Code
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; 08-02-2010 at 07:43 PM.
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 08-02-2010, 07:44 PM
Junior Member
 

Join Date: Jan 2010
Posts: 13
Thanks: 2
Thanked 0 Times in 0 Posts
olemagro is on a distinguished road
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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
updating EDT with thread swingworker/invokeLater ? mdstrauss AWT / Java Swing 0 11-10-2009 09:52 AM
updating EDT with thread swingworker/invokeLater ? mdstrauss Threads 0 10-10-2009 06:25 PM
updating database gurpreetm13 JDBC & Databases 3 09-10-2009 04:43 PM
application Task problem - updating JTree idandush AWT / Java Swing 2 18-06-2009 08:15 AM
[SOLVED] Threads and variable Koren3 Threads 3 21-04-2009 11:28 AM


100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 08:56 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.