home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / AppB / JumpingBox / MouseRun.java < prev    next >
Encoding:
Java Source  |  1996-08-02  |  4.6 KB  |  165 lines

  1. // MouseRun - based on the Sun demo applet MouseTrack. MouseTrack moved
  2. //            a rectangle about randomly on the screen and challenged
  3. //            the user to try to "hit it" with the mouse. MouseRun causes
  4. //            the rectangle to run away from the mouse (not just
  5. //            randomly jump about).
  6. import java.awt.*;
  7. import java.lang.Math;
  8. import java.applet.Applet;
  9.  
  10. public class MouseRun extends Applet implements Runnable {
  11.  
  12.     // location of the mouse
  13.     Point mouse = new Point(0, 0);
  14.  
  15.     // the target rectangle
  16.     Rectangle rectangle = new Rectangle(20, 20);
  17.  
  18.     // the bounding rectangle contains the entire playing area
  19.     Rectangle bounds = new Rectangle();
  20.  
  21.     // direction to run
  22.     Point vector = new Point(1, 1);
  23.  
  24.     // save off our thread
  25.     Thread me = null;
  26.  
  27.     // counts the number of hits in a row
  28.     int onaroll = 0;
  29.  
  30.     // init() - size the window and the bounding rectangle
  31.     public void init() 
  32.     {
  33.         bounds.resize(size().width - 1, size().height - 1);
  34.     }
  35.  
  36.     // start() - start the thread to move the rectangle
  37.     public void start()
  38.     {
  39.         if(me == null) 
  40.         {
  41.             me = new Thread(this);
  42.             me.start();
  43.         }
  44.     }
  45.  
  46.     // stop() - stop the thread
  47.     public void stop()
  48.     {
  49.         me = null;
  50.     }
  51.  
  52.     // run() - repeatedly repainting the screen let's the rectangle
  53.     //         move away
  54.     public void run() {
  55.         while (me != null)
  56.         {
  57.             try
  58.             {
  59.                 Thread.sleep(20);
  60.             }
  61.             catch (InterruptedException e)
  62.             {
  63.                 me = null;
  64.             }
  65.             repaint();
  66.         }
  67.     }
  68.  
  69.     // paint() - move and display the target rectangle
  70.     public void paint(Graphics g)
  71.     {
  72.         // repaint the bounding rectangle
  73.         g.drawRect(0, 0, bounds.width, bounds.height);
  74.  
  75.         // move the rectangle away from the mouse; as soon as
  76.         // the rectangle is "far enough" away, stop moving it
  77.         Point center = new Point(rectangle.x + rectangle.width/2,
  78.                                  rectangle.y + rectangle.height/2);
  79.         vector.x = -1;
  80.         if (mouse.x < center.x)
  81.         {
  82.             vector.x = 1;
  83.         }
  84.         if ((center.x - mouse.x) * vector.x > 100)
  85.         {
  86.             vector.x = 0;
  87.         }
  88.  
  89.         vector.y = -1;
  90.         if (mouse.y < center.y)
  91.         {
  92.             vector.y = 1;
  93.         }
  94.         if ((center.y - mouse.y) * vector.y > 100)
  95.         {
  96.             vector.y = 0;
  97.         }
  98.         rectangle.translate(vector.x, vector.y); // move rectangle
  99.  
  100.  
  101.         // make sure rectangle doesn't leave the window
  102.         Rectangle intersect = rectangle.intersection(bounds);
  103.         if (!intersect.equals(rectangle))
  104.         {
  105.             rectangle.move(bounds.width/2, bounds.height/2);
  106.         }
  107.  
  108.         // now draw the target rectangle
  109.         g.drawRect(rectangle.x,     rectangle.y,
  110.                    rectangle.width, rectangle.height);
  111.     }
  112.  
  113.     /*
  114.      * Mouse methods
  115.      */
  116.     // mouseDown() - count the number of hits/misses
  117.     public boolean mouseDown(java.awt.Event evt, int nX, int nY)
  118.     {
  119.         // first position the mouse
  120.         mouse.move(nX, nY);
  121.  
  122.         // now see if we hit it
  123.         if(rectangle.inside(nX, nY))
  124.         {
  125.             switch (++onaroll)
  126.             {
  127.                 case 1:
  128.                     getAppletContext().showStatus("HIT IT AGAIN! AGAIN!");
  129.                     break;
  130.                 case 2:
  131.                 case 3:
  132.                 case 4:
  133.                      getAppletContext().showStatus("YOU'RE ON A ROLL:"
  134.                                                   + onaroll
  135.                                                   + "Hits!");
  136.                     break;
  137.                 default:
  138.                     getAppletContext().showStatus(
  139.                                   "You're on your way to THE HALL OF FAME:"
  140.                                   + onaroll
  141.                                   + "Hits!");
  142.             }
  143.             play(getCodeBase(), "sounds/that.hurts.au");
  144.         }
  145.         else
  146.         {
  147.             getAppletContext().showStatus("You missed at ("
  148.                                           + nX
  149.                                           + ", " 
  150.                                           + nY 
  151.                                           + ")");
  152.             play(getCodeBase(), "sounds/thin.bell.au");
  153.             onaroll = 0;
  154.         }
  155.         return true;
  156.     }
  157.  
  158.     // mouseMove() - record the mouse location
  159.     public boolean mouseMove(java.awt.Event evt, int nX, int nY)
  160.     {
  161.         mouse.move(nX, nY);
  162.         return true;
  163.     }
  164. }
  165.