I have the following code that produces an applet of two eyeballs. I have a mouseMotionListener that is set so that when the mouse is moved around the pair of eyes, the eyeballs follow the pointer. I have the eyeballs set up in four locations. To the left, right, north, and south in the eye. However, when I move the mouse onto the applet all four are displayed. Is there a simple way so that the correct set of eyes are displayed?
Thank you in advance, here is my code....
package project5;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Main extends JApplet
{
private int X;
private int Y;
boolean mouseLeft= false;
boolean mouseRight = false;
boolean mouseUp = false;
boolean mouseDown = false;
public void init()
{
getContentPane().setBackground(Color.WHITE);
addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMouseMotionListener());
}
public void paint(Graphics g)
{
super.paint(g);
//Original
g.setColor(Color.BLACK);
g.drawOval(50,50,50,50);
g.drawOval(200, 50, 50, 50);
g.fillOval(65,65, 20, 20);
g.fillOval(215, 65, 20, 20);
//this is what is drawn when in zone- left
if (mouseLeft)
{
g.fillOval(45,65, 20,20);
g.fillOval(195, 65, 20, 20);
g.setColor(Color.WHITE);
g.fillOval(65,65, 20, 20);
g.fillOval(215, 65, 20, 20);
}
//right
if (mouseRight)
{
g.setColor(Color.BLACK);
g.fillOval(85,65, 20,20);
g.fillOval(235, 65, 20, 20);
g.setColor(Color.WHITE);
g.fillOval(65,65, 20, 20);
g.fillOval(215, 65, 20, 20);
}
//up
if (mouseUp)
{
g.setColor(Color.BLACK);
g.fillOval(65,45, 20,20);
g.fillOval(215, 45, 20, 20);
g.setColor(Color.WHITE);
g.fillOval(65,65, 20, 20);
g.fillOval(215, 65, 20, 20);
}
//down
if (mouseDown)
{
g.setColor(Color.BLACK);
g.fillOval(65,85, 20,20);
g.fillOval(215, 85, 20, 20);
g.setColor(Color.WHITE);
g.fillOval(65,65, 20, 20);
g.fillOval(215, 65, 20, 20);
}
else
{
g.fillOval(65,65, 20, 20);
}
}
private class MyMouseListener implements MouseListener
{
public void mousePressed(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
private class MyMouseMotionListener implements MouseMotionListener
{
public void mouseDragged(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
X = e.getX();
Y = e.getY();
mouseLeft = true;
if (X <= 50)
{
repaint();
}
mouseRight = true;
if (X >= 250)
{
repaint();
}
mouseUp = true;
if(Y < 50)
{
repaint();
}
mouseDown = true;
if(Y> 250)
{
repaint();
}
}
}
}