Home - Programming - Java- WatchMe Applet

WatchMe Applet


Download the Portable Copy HERE

001/*
002   Author: Brian Sonnie
003   Program: WatchMe[Applet]
004   7/21/09
005*/
006import javax.swing.*;
007import java.awt.*;
008import java.awt.event.*;
009 
010/**
011   This class is an applet that draws two eyes
012   that follow your mouse
013*/
014 
015public class WatchMe extends JApplet
016{
017   /**
018      init method
019   */
020   public void init()
021   {
022      // Set the background color
023      getContentPane().setBackground(Color.gray);
024       
025      // Add a mouse listener
026      addMouseListener(new MyMouseListener());
027      // Add a mouse motion listener
028      addMouseMotionListener(new MyMouseMotionListener());
029   }
030  final int EYE_WIDTH = 30;  // Width of the Eyes
031   final int EYE_HEIGHT = 40; // Height of the Eyes
032   final int PUPIL_SIZE = 10; // Size of the pupil
033   final int EYE_X1 = 70;     // Eye1 X coordinate
034   final int EYE_Y1 = 40;     // Eye1 Y coordinate
035   final int EYE_X2 = 110;    // Eye2 X coordinate
036   final int EYE_Y2 = 40;     // Eye2 Y coordinate
037   boolean Exited = false;    // Exited flag
038   boolean mouseDown = false; // mouseDown flag
039    
040   // enumeration for the status of the Eye
041   enum eyeStatus {STRAIGHT, UP, DOWN, LEFT, RIGHT};
042   eyeStatus currentStatus = eyeStatus.STRAIGHT;
043    
044   public void paint(Graphics g)
045   {
046      // Call the superclass paint method.
047      super.paint(g);
048      // set drawing color
049      g.setColor(Color.black);
050      // Draw 1st Eye
051      g.drawOval(EYE_X1,EYE_Y1, EYE_WIDTH, EYE_HEIGHT);
052      // Draw 2nd Eye
053      g.drawOval(EYE_X2,EYE_Y2, EYE_WIDTH, EYE_HEIGHT);
054       
055      // Draw the pupil
056      switch(currentStatus)
057      {
058         case STRAIGHT:
059            lookStraight(g);
060         break;
061         case UP:
062            lookUp(g);
063         break;
064         case DOWN:
065            lookDown(g);
066         break;
067         case LEFT:
068            lookLeft(g);
069         break;
070         case RIGHT:
071            lookRight(g);
072         break;
073      }
074   }
075   /**
076      Method lookStraight draws the pupils looking straight
077      @param g The applet's Graphics object.
078   */
079   public void lookStraight(Graphics g)
080   {
081      // Draw two filled ovals that represent pupils
082      g.fillOval(EYE_X1 + (EYE_WIDTH/2)-(PUPIL_SIZE/2), 
083            EYE_Y1+(EYE_HEIGHT/2)-(PUPIL_SIZE/2),
084                    PUPIL_SIZE,
085PUPIL_SIZE);
086       
087      g.fillOval(EYE_X2 + (EYE_WIDTH/2)-(PUPIL_SIZE/2),
088            EYE_Y2+(EYE_HEIGHT/2)-(PUPIL_SIZE/2),
089                    PUPIL_SIZE,
090 PUPIL_SIZE);
091   }
092    
093   /**
094      Method lookUp draws the pupils looking up
095      @param g The applet's Graphics object.
096   */
097   public void lookUp(Graphics g)
098   {
099      // Draw two filled ovals that represent pupils
100      g.fillOval(EYE_X1 + (EYE_WIDTH/2)-(PUPIL_SIZE/2),
101            EYE_Y1, PUPIL_SIZE,
102            PUPIL_SIZE);
103      
104      g.fillOval(EYE_X2 + (EYE_WIDTH/2)-(PUPIL_SIZE/2),
105            EYE_Y2, PUPIL_SIZE,
106            PUPIL_SIZE);
107   }
108    
109   /**
110      Method lookDown draws the pupils looking down
111      @param g The applet's Graphics object.
112   */
113   public void lookDown(Graphics g)
114   {
115      // Draw two filled ovals that represent pupils
116      g.fillOval(EYE_X1 + (EYE_WIDTH/2)-(PUPIL_SIZE/2),
117            EYE_Y1 + EYE_HEIGHT-PUPIL_SIZE,
118                    PUPIL_SIZE, PUPIL_SIZE);
119       
120      g.fillOval(EYE_X2 + (EYE_WIDTH/2)-(PUPIL_SIZE/2),
121            EYE_Y2 + EYE_HEIGHT-10,
122            PUPIL_SIZE, PUPIL_SIZE);
123   }
124    
125   /**
126      Method lookLeft draws the pupils looking left
127      @param g The applet's Graphics object.
128  */
129   public void lookLeft(Graphics g)
130   {
131      // Draw two filled ovals that represent pupils
132      g.fillOval(EYE_X1, EYE_Y1+(EYE_HEIGHT/2)-(PUPIL_SIZE/2),
133            PUPIL_SIZE,PUPIL_SIZE);
134       
135      g.fillOval(EYE_X2, EYE_Y1+(EYE_HEIGHT/2)-(PUPIL_SIZE/2),
136            PUPIL_SIZE,PUPIL_SIZE);
137   }
138    
139   /**
140      Method lookRight draws the pupils looking Right
141     @param g The applet's Graphics object.
142   */
143   public void lookRight(Graphics g)
144   {
145      // Draw two filled ovals that represent pupils
146      g.fillOval(EYE_X1+EYE_WIDTH-PUPIL_SIZE,
147            EYE_Y1+(EYE_HEIGHT/2)-(PUPIL_SIZE/2),
148                     PUPIL_SIZE,PUPIL_SIZE);
149       
150      g.fillOval(EYE_X2+EYE_WIDTH-PUPIL_SIZE,
151            EYE_Y1+(EYE_HEIGHT/2)-(PUPIL_SIZE/2),
152                     PUPIL_SIZE,PUPIL_SIZE);
153   }
154    
155   /**
156      Private inner class that handles mouse events
157      Deals with setting the flags for mouseDown and
158      Excited.
159   */
160   private class MyMouseListener implements MouseListener
161   {
162      public void mousePressed(MouseEvent e)
163      {
164         // Set mouseDown flag to true
165         mouseDown = true;
166      }
167       
168      // Overriden Method \
169      public void mouseClicked(MouseEvent e)
170     {}
171 
172      public void mouseReleased(MouseEvent e)
173      {
174         // Set mouseDown flag to false
175         mouseDown=false;
176      }
177 
178      public void mouseEntered(MouseEvent e)
179      {
180         // Set Exited flag to false
181         Exited = false;
182      }
183 
184      public void mouseExited(MouseEvent e)
185      {
186         currentStatus = eyeStatus.STRAIGHT;
187         // Set Exited flag to true
188         Exited = true;
189         // Force a call to the paint method
190         repaint();
191      }
192   }
193    
194   /**
195      Private inner class to handle mouse motion events.
196   */
197   private class MyMouseMotionListener implements MouseMotionListener
198   {
199      public void mouseDragged(MouseEvent e)
200      {}
201      
202      public void mouseMoved(MouseEvent e)
203      {
204         // Get the last Status of the Eyes.
205         eyeStatus oldStatus = currentStatus;
206          
207         if(!Exited)
208         {
209            // If mouse is above eyes
210            if(e.getY() < 40)
211               currentStatus = eyeStatus.UP;
212             
213            // If mouse is below eyes
214            else if(e.getY() > EYE_Y1+EYE_HEIGHT)
215               currentStatus = eyeStatus.DOWN;
216             
217            // If mouse is to the left of the eyes
218            else if(e.getX() < EYE_X1)
219               currentStatus = eyeStatus.LEFT;
220             
221            // If mouse is to the right of the eyes
222            else if(e.getX() > EYE_X2 + EYE_WIDTH)
223               currentStatus = eyeStatus.RIGHT;
224          
225            // Else it is on the eyes
226            else
227               currentStatus = eyeStatus.STRAIGHT;
228         }
229         else
230         {
231            currentStatus = eyeStatus.STRAIGHT;
232         }
233          
234         // If status changed repaint this improves display
235         if(oldStatus!=currentStatus)
236         repaint();
237      }
238   }
239 
240}
 

Ad Space

Ads