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();
          }
          
   }
   }
}

You should only be setting mouseLeft, mouseRight, etc to true if, in fact, the mouse was moved in that sector. When your mouseMoved method gets called, you are setting ALL of them to true, then calling repaint. The way you're currently doing it, if mouseLeft were to be set true, it would always remain true, which is one problem. Also, currently, if mouseDown were true, it would call repaint. However, the mouseMoved method just set mouseUp, mouseDown, mouseLeft, and mouseRight ALL to true - so all of them will be repainted.

hope that helps.

To demonstrate the second point I made above, consider what happens when you move the mouse. Lets say you move the mouse down, but you do not move it in any other direction. The mouseMoved method is called, and mouseLeft is set to true. Now, lets say you eventually make it down to mouseDown = true. Then, repaint is called where you have if (Y > 250). Now, you enter the repaint method. Since mouseLeft is true, and mouseRight is true... both of those things will be repainted.

Now, another thing will happen: since you never reset mouseLeft, mouseRight, mouseUp, and mouseDown to "false"... every time you enter the repaint method, these things might be repainted. Consider using "else if" statements inside of mouseMoved to call repaint, so that repaint is never called when you don't want it to be. Also, make sure that at the end of mouseMoved, you reset all of the mouseUp, down, left, and right variables to false. Also, do not set these variables to "true" until you have determined that they should be true. In other words, setting them to true should be done inside the if statement, not outside of it.

Also, I'm not exactly sure how your assignment works, but you should consider that any time you color something, you are never un-coloring it. So lets say you color something because of mouseDown. What happens when the thing you colored isn't in the "mouseDown" sector anymore? Shouldn't you un-color it?

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();
          }
          
   }
   }
}
commented: Helpful. +15
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.