Im trying to have the center circles follow the mouse if it enters the screen and go back to the original position if the mouse exits screen. Im not sure what to put into my mouse listener and would appeciate some help

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class WatchMe extends JApplet {
	
	public void eyesPoint(){
		
		
		
	}

	public void init()
	{
		getContentPane().setBackground(Color.white);
		
		addMouseListener(new MyMouseListener());
		
		addMouseMotionListener(new MyMouseMotionListener());
		
		
		
	}
	
	public class MyMouseListener extends MouseAdapter
	{
		public void mouseEntered(MouseEvent e){
		
			
		}
		
		
		
	}
	
	public class MyMouseMotionListener extends MouseMotionAdapter
	{
		public void mouseMoved(MouseEvent e){
			
	
			
			
		}
		
	}
	
	public void paint(Graphics g)
	{
		
		super.paint(g);
		
		
		//create outside circle1
		g.setColor(Color.black);
		g.drawOval(430, 449, 75, 100);

		//create outside circle2
		
		g.setColor(Color.black);
		g.drawOval(525, 449, 75, 100);

		//create inside filled in circle3
		
		g.setColor(Color.black);
		g.fillOval(450, 479, 35, 35);

		//create inside filled in circle4
		
		g.setColor(Color.black);
		g.fillOval(545, 479, 35, 35);
		
		
	}
	


	//method for when mouse comes in
	
	

	//method for eyes following the mouse
	
	
	
}

My inclination would be to have a class variable(s) that track the Mouse position. The way you could do this is by setting up the variables to track x, y position and in the MouseMoved method, first you'd update those variable, then you'd call repaint() which causes paint() to be called. In the paint() method you could then utilize these variables. As for detecting when the mouse moves off screen, use the MouseListener interface, specifically the mouseExited method. Combined with some simple if statements in your paint method, this should be all you need.

It's been a long time since I've coded Java ("coding" sharepoint now), so hopefully this was good advice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.