home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / MouseTrack.java < prev    next >
Encoding:
Java Source  |  1997-07-30  |  3.8 KB  |  119 lines

  1. // $Header: z:/admin/metro_examples/java/demo/JumpingBox/rcs/MouseTrack.java 1.1 1997/02/06 00:30:18 IPGIntel-2 Exp $ 
  2. /*
  3.  * @(#)MouseTrack.java    1.2 96/12/06
  4.  *
  5.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  8.  * modify and redistribute this software in source and binary code form,
  9.  * provided that i) this copyright notice and license appear on all copies of
  10.  * the software; and ii) Licensee does not utilize the software in a manner
  11.  * which is disparaging to Sun.
  12.  *
  13.  * This software is provided "AS IS," without a warranty of any kind. ALL
  14.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  15.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  16.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  17.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  18.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  19.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  20.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  21.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  22.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  23.  * POSSIBILITY OF SUCH DAMAGES.
  24.  *
  25.  * This software is not designed or intended for use in on-line control of
  26.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  27.  * the design, construction, operation or maintenance of any nuclear
  28.  * facility. Licensee represents and warrants that it will not use or
  29.  * redistribute the Software for such purposes.
  30.  */
  31.  
  32. import java.awt.Graphics;
  33. import java.lang.Math;
  34.  
  35. public class MouseTrack extends java.applet.Applet {
  36.  
  37.     int mx, my;
  38.     int onaroll;
  39.  
  40.     public void init() {
  41.     onaroll = 0;
  42.     resize(500, 500);
  43.     }
  44.  
  45.     public void paint(Graphics g) {
  46.     g.drawRect(0, 0, size().width - 1, size().height - 1);
  47.     mx = (int)(Math.random()*1000) % (size().width - (size().width/10));
  48.     my = (int)(Math.random()*1000) % (size().height - (size().height/10));
  49.     g.drawRect(mx, my, (size().width/10) - 1, (size().height/10) - 1);
  50.     }
  51.  
  52.     /*
  53.      * Mouse methods
  54.      */
  55.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  56.     requestFocus();
  57.     if((mx < x && x < mx+size().width/10-1) && (my < y && y < my+size().height/10-1)) {
  58.         if(onaroll > 0) {
  59.         switch(onaroll%4) {
  60.         case 0:
  61.             play(getCodeBase(), "sounds/tiptoe.thru.the.tulips.au");
  62.             break;
  63.         case 1:
  64.             play(getCodeBase(), "sounds/danger,danger...!.au");
  65.             break;
  66.         case 2:
  67.             play(getCodeBase(), "sounds/adapt-or-die.au");
  68.             break;
  69.         case 3:
  70.             play(getCodeBase(), "sounds/cannot.be.completed.au");
  71.             break;
  72.         }
  73.         onaroll++;
  74.         if(onaroll > 5)
  75.             getAppletContext().showStatus("You're on your way to THE HALL OF FAME:"
  76.             + onaroll + "Hits!");
  77.         else
  78.             getAppletContext().showStatus("YOU'RE ON A ROLL:" + onaroll + "Hits!");
  79.         }
  80.         else {
  81.         getAppletContext().showStatus("HIT IT AGAIN! AGAIN!");
  82.         play(getCodeBase(), "sounds/that.hurts.au");
  83.         onaroll = 1;
  84.         }
  85.     }
  86.     else {
  87.         getAppletContext().showStatus("You hit nothing at (" + x + ", " + y + "), exactly\n");
  88.         play(getCodeBase(), "sounds/thin.bell.au");
  89.         onaroll = 0;
  90.     }
  91.     repaint();
  92.     return true;
  93.     }
  94.  
  95.     public boolean mouseMove(java.awt.Event evt, int x, int y) {
  96.     if((x % 3 == 0) && (y % 3 == 0))
  97.         repaint();
  98.     return true;
  99.     }
  100.  
  101.     public void mouseEnter() {
  102.     repaint();
  103.     }
  104.  
  105.     public void mouseExit() {
  106.     onaroll = 0;
  107.     repaint();
  108.     }
  109.  
  110.     /**
  111.      * Focus methods
  112.      */
  113.     public void keyDown(int key) {
  114.     requestFocus();
  115.     onaroll = 0;
  116.     play(getCodeBase(), "sounds/ip.au");
  117.     }
  118. }
  119.