home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / vjplusb / msdev / samples / sun / jumpingbox / mousetrack.java < prev    next >
Encoding:
Java Source  |  1996-07-10  |  2.1 KB  |  88 lines

  1. import java.awt.Graphics;
  2. import java.lang.Math;
  3.  
  4. public class mousetrack extends java.applet.Applet {
  5.  
  6.     int mx, my;
  7.     int onaroll;
  8.  
  9.     public void init() {
  10.     onaroll = 0;
  11.     resize(500, 500);
  12.     }
  13.  
  14.     public void paint(Graphics g) {
  15.     g.drawRect(0, 0, size().width - 1, size().height - 1);
  16.     mx = (int)(Math.random()*1000) % (size().width - (size().width/10));
  17.     my = (int)(Math.random()*1000) % (size().height - (size().height/10));
  18.     g.drawRect(mx, my, (size().width/10) - 1, (size().height/10) - 1);
  19.     }
  20.  
  21.     /*
  22.      * Mouse methods
  23.      */
  24.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  25.     requestFocus();
  26.     if((mx < x && x < mx+size().width/10-1) && (my < y && y < my+size().height/10-1)) {
  27.         if(onaroll > 0) {
  28.         switch(onaroll%4) {
  29.         case 0:
  30.             play(getCodeBase(), "sounds/tiptoe.thru.the.tulips.au");
  31.             break;
  32.         case 1:
  33.             play(getCodeBase(), "sounds/danger,danger...!.au");
  34.             break;
  35.         case 2:
  36.             play(getCodeBase(), "sounds/adapt-or-die.au");
  37.             break;
  38.         case 3:
  39.             play(getCodeBase(), "sounds/cannot.be.completed.au");
  40.             break;
  41.         }
  42.         onaroll++;
  43.         if(onaroll > 5)
  44.             getAppletContext().showStatus("You're on your way to THE HALL OF FAME:"
  45.             + onaroll + "Hits!");
  46.         else
  47.             getAppletContext().showStatus("YOU'RE ON A ROLL:" + onaroll + "Hits!");
  48.         }
  49.         else {
  50.         getAppletContext().showStatus("HIT IT AGAIN! AGAIN!");
  51.         play(getCodeBase(), "sounds/that.hurts.au");
  52.         onaroll = 1;
  53.         }
  54.     }
  55.     else {
  56.         getAppletContext().showStatus("You hit nothing at (" + x + ", " + y + "), exactly\n");
  57.         play(getCodeBase(), "sounds/thin.bell.au");
  58.         onaroll = 0;
  59.     }
  60.     repaint();
  61.     return true;
  62.     }
  63.  
  64.     public boolean mouseMove(java.awt.Event evt, int x, int y) {
  65.     if((x % 3 == 0) && (y % 3 == 0))
  66.         repaint();
  67.     return true;
  68.     }
  69.  
  70.     public void mouseEnter() {
  71.     repaint();
  72.     }
  73.  
  74.     public void mouseExit() {
  75.     onaroll = 0;
  76.     repaint();
  77.     }
  78.  
  79.     /**
  80.      * Focus methods
  81.      */
  82.     public void keyDown(int key) {
  83.     requestFocus();
  84.     onaroll = 0;
  85.     play(getCodeBase(), "sounds/ip.au");
  86.     }
  87. }
  88.