home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / AppB / JumpingBox / MouseTrack.java < prev    next >
Encoding:
Java Source  |  1996-07-10  |  3.0 KB  |  111 lines

  1. import java.awt.Graphics;
  2. import java.lang.Math;
  3.  
  4. public class MouseTrack extends java.applet.Applet {
  5.  
  6.     // mx, my are the mouse x and y positions
  7.     int mx, my;
  8.  
  9.     // onaroll counts the number of times in a row you hit
  10.     int onaroll;
  11.  
  12.     // init - set the window size and let it go at that
  13.     public void init() {
  14.     onaroll = 0;
  15.     resize(500, 500);
  16.     }
  17.  
  18.     // paint - paint target rectangle
  19.     public void paint(Graphics g) {
  20.     // start by drawing a bounding rectangle around entire
  21.     // window
  22.     g.drawRect(0, 0, size().width - 1, size().height - 1);
  23.  
  24.     // now calculate a random location for the target window
  25.     // and draw it there
  26.     mx = (int)(Math.random()*1000) %
  27.                        (size().width - (size().width/10));
  28.     my = (int)(Math.random()*1000) %
  29.                        (size().height - (size().height/10));
  30.     g.drawRect(mx, my,
  31.                (size().width/10) - 1,
  32.                (size().height/10) - 1);
  33.     }
  34.  
  35.     /*
  36.      * Mouse methods
  37.      */
  38.     // mouseDown - determine if you got a hit
  39.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  40.     requestFocus();
  41.     if((mx < x && x < mx+size().width/10-1) &&
  42.        (my < y && y < my+size().height/10-1)) {
  43.         if(onaroll > 0) {
  44.         switch(onaroll%4) {
  45.         case 0:
  46.             play(getCodeBase(),
  47.                            "sounds/tiptoe.thru.the.tulips.au");
  48.             break;
  49.         case 1:
  50.             play(getCodeBase(), "sounds/danger,danger...!.au");
  51.             break;
  52.         case 2:
  53.             play(getCodeBase(), "sounds/adapt-or-die.au");
  54.             break;
  55.         case 3:
  56.             play(getCodeBase(), "sounds/cannot.be.completed.au");
  57.             break;
  58.         }
  59.         onaroll++;
  60.         if(onaroll > 5)
  61.             getAppletContext().showStatus(
  62.                     "You're on your way to THE HALL OF FAME:"
  63.                     + onaroll + "Hits!");
  64.         else
  65.             getAppletContext().showStatus(
  66.                      "YOU'RE ON A ROLL:" + onaroll + "Hits!");
  67.         }
  68.         else {
  69.         getAppletContext().showStatus("HIT IT AGAIN! AGAIN!");
  70.         play(getCodeBase(), "sounds/that.hurts.au");
  71.         onaroll = 1;
  72.         }
  73.     }
  74.     else {
  75.         getAppletContext().showStatus(
  76.            "You hit nothing at (" + x + ", " + y + "), exactly");
  77.         play(getCodeBase(), "sounds/thin.bell.au");
  78.         onaroll = 0;
  79.     }
  80.     repaint();
  81.     return true;
  82.     }
  83.  
  84.     // mouseMove - repaint the rectangle whenever the mouse
  85.     //             moves, enters the window, or exits the
  86.     //             window
  87.     public boolean mouseMove(java.awt.Event evt, int x, int y) {
  88.     if((x % 3 == 0) && (y % 3 == 0))
  89.         repaint();
  90.     return true;
  91.     }
  92.  
  93.     public void mouseEnter() {
  94.     repaint();
  95.     }
  96.  
  97.     public void mouseExit() {
  98.     onaroll = 0;
  99.     repaint();
  100.     }
  101.  
  102.     /**
  103.      * Focus methods
  104.      */
  105.     public void keyDown(int key) {
  106.     requestFocus();
  107.     onaroll = 0;
  108.     play(getCodeBase(), "sounds/ip.au");
  109.     }
  110. }
  111.