home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / AppB / JumpingBox / MouseRun.java1 < prev    next >
Encoding:
Text File  |  1996-07-10  |  4.8 KB  |  170 lines

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