Write an applet that displays a drawing of two eyes in the centre of its window

Question:

Write an applet that displays a drawing of two eyes in the centre of its window. When the mouse cursor is not inside the window, the eyes should look ahead. When the mouse cursor is inside the window, the eyes should follow the cursor

Answer:

  import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
   
    /**
       This is a simple applet.
    */
   
    public class WatchMe extends JApplet
    {
        private int currentX = 0; //cursor's current X position
        private int currentY = 0; //cursor's current Y position
        private int leyeX = 118; //pupil's default X position
        private int reyeX = 148; //pupil's default X position
        private int leyeY = 68; //pupil's default Y position
        private int reyeY = 68; //pupil's default Y position
     
       public void init()
       {
          getContentPane().setBackground(Color.white);
       
          // Add a mouse motion listener to this applet.
          addMouseMotionListener(new MyMouseMotionListener());
       }
     
       public void paint(Graphics g)
       {
           //Call the superclass paint method.
           super.paint(g);
           //draw a black unfilled rectangle
           g.setColor(Color.black);
         
           //draw the eyes
           g.drawOval(110, 55, 25, 40);
           g.drawOval(140, 55, 25, 40);
         
           //draw the pupils
           g.setColor(Color.black);
           g.fillOval(leyeX, leyeY, 10, 10);
           g.fillOval(reyeX, reyeY, 10, 10);
       }
     
       private class MyMouseMotionListener
                            implements MouseMotionListener
       {
           public void mouseDragged(MouseEvent e)
           {
                   currentX = e.getX();
                   currentY = e.getY();
                   if (currentX > 149)
                    {
                        leyeX = 123;
                        reyeX = 153;
                    }
                   if (currentX < 149)
                    {
                        leyeX = 113;
                        reyeX = 143;
                    }
                   if (currentY > 99)
                    {
                        leyeY = 73;
                        reyeY = 73;
                    }
                   if (currentY < 99)
                    {
                        leyeY = 63;
                        reyeY = 63;
                    }
                 
                   //repaint the window
                   repaint();
                 
           }
         
           public void mouseMoved(MouseEvent e)
           {
                   currentX = e.getX();
                   currentY = e.getY();
                   if (currentX > 149)
                    {
                        leyeX = 123;
                        reyeX = 153;
                    }
                   if (currentX < 149)
                    {
                        leyeX = 113;
                        reyeX = 143;
                    }
                   if (currentY > 99)
                    {
                        leyeY = 73;
                        reyeY = 73;
                    }
                   if (currentY < 99)
                    {
                        leyeY = 63;
                        reyeY = 63;
                    }
                 
                   //repaint the window
                   repaint();
                 
           }      
       }
    }
Click to Rate This Answer!
[Total: 0 Average: 0]

Leave a Comment

Your email address will not be published. Required fields are marked *