home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / MouseTrack.java < prev    next >
Text File  |  1997-05-22  |  4KB  |  135 lines

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