title: A Swing novice, want to make a small moving eyes Java GUI practice but stuck
body: <p>I want to implement a panel that draws two eyes that moving to look up, to the center, or down depending on whether the mouse cursor is above, inside, or below the eyes. I first used this code to make the eyes:</p>
  
  <pre><code>public class EyesPanel extends JPanel implements ActionListener {
      // images
      public void paintComponent(Graphics g) {
          super.paintComponents(g);
          g.drawOval(130, 100, 120, 120);
          g.drawOval(250, 100, 120, 120);
          g.fillOval(175, y, 30, 30);   // x: 175  y: 145
          g.fillOval(295, y, 30, 30);   // x: 295  y: 145
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/x23Gz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x23Gz.jpg" alt="Eyes"></a></p>
  
  <p>And then is time to add event listener to make this class works, but here is the part I stuck. I know how to make graphics move (ActionListener) and I know how to implement MouseInputListener (extends MouseInputListener). However, combining those two together make me feel frustrated. Can anybody tell me how to do it, give me a sample code can be really helpful.</p>
  
  <p>The following is my code so far, not a functioning and complete code:</p>
  
  <pre><code>public class EyesPanel extends JPanel implements ActionListener {
      private JPanel panel;
      private int y;
      private int dy;
      private Timer t;
      private Mouse move;
  
      public EyesPanel() {
          dy = 5;
          y = 145;
  
          // mouse detector
          this.addMouseListener(new Mouse());
          this.addMouseMotionListener(new Mouse());
  
          // Timer 
          t = new Timer(100, this);
      }
  
      // images
      public void paintComponent(Graphics g) {
          super.paintComponents(g);
          g.drawOval(130, 100, 120, 120);
          g.drawOval(250, 100, 120, 120);
          g.fillOval(175, y, 30, 30);   // x: 175  y: 145
          g.fillOval(295, y, 30, 30);   // x: 295  y: 145
      }
  
      public void actionPerformed(ActionEvent event) {
          moveDown();     //➜ not complete, don't know how to implement
      }
  
      // move up
      private void moveUp() {
          if (move.move() == 1) {
              t.start();
              y = y + dy;
              repaint();
          } else {
              t.stop();
          }
      }
  
      // move down
      private void moveDown() {
          if (move.move() == -1) {
              t.start();
              y = y - dy;
              repaint();
          } else {
              t.stop();
          }
      }
      // ➜ not complete, trying, but no clue
  }
  </code></pre>
  
  <p>My mouse event class:</p>
  
  <pre><code>public class Mouse extends MouseInputAdapter {
      private int y;
  
      public void mouseEntered(MouseEvent event) {
          JPanel pane =  (JPanel) event.getSource();
          y = pane.getHeight();      // ➜ not complete
      }
  }
  </code></pre>