home *** CD-ROM | disk | FTP | other *** search
- // MouseRun - based on the Sun demo applet MouseTrack. MouseTrack
- // moves a rectangle about randomly on the screen and
- // challenges the user to try to "hit" it with the
- // mouse. MouseRun causes the rectangle to run away
- // from the mouse (not just jump about randomly).
- import java.awt.*;
- import java.lang.Math;
- import java.applet.Applet;
-
- public class MouseRun extends Applet implements Runnable {
-
- // location of the mouse
- Point mouse = new Point(0, 0);
-
- // the target rectangle
- Rectangle rectangle = new Rectangle(20, 20);
-
- // the bounding rectangle contains the entire playing area
- Rectangle bounds = new Rectangle();
-
- // direction to run
- Point vector = new Point(1, 1);
-
- // save our thread
- Thread me = null;
-
- // counts the number of hits in a row
- int onaroll = 0;
-
- // init - size the window and the bounding rectangle
- public void init()
- {
- resize(500, 500);
-
- bounds.resize(size().width - 1, size().height - 1);
- }
-
- // start - start the thread to move the rectangle
- public void start()
- {
- if(me == null)
- {
- me = new Thread(this);
- me.start();
- }
- }
-
- // stop - stop the thread
- public void stop()
- {
- me = null;
- }
-
- // run - repeatedly repaint the screen to let the rectangle
- // move away
- public void run() {
- while (me != null)
- {
- try
- {
- Thread.sleep(20);
- }
- catch (InterruptedException e)
- {
- me = null;
- }
- repaint();
- }
- }
-
- // paint - move and display the target rectangle
- public void paint(Graphics g)
- {
- // repaint the bounding rectangle
- g.drawRect(0, 0, bounds.width, bounds.height);
-
- // move the rectangle away from the mouse; as soon as
- // the rectangle is "far enough" away, stop moving it
- Point center = new Point(rectangle.x + rectangle.width/2,
- rectangle.y + rectangle.height/2);
- vector.x = -1;
- if (mouse.x < center.x)
- {
- vector.x = 1;
- }
- if ((center.x - mouse.x) * vector.x > 100)
- {
- vector.x = 0;
- }
-
- vector.y = -1;
- if (mouse.y < center.y)
- {
- vector.y = 1;
- }
- if ((center.y - mouse.y) * vector.y > 100)
- {
- vector.y = 0;
- }
- rectangle.translate(vector.x, vector.y);
-
-
- // make sure rectangle doesn't leave the window
- Rectangle intersect = rectangle.intersection(bounds);
- if (!intersect.equals(rectangle))
- {
- rectangle.move(bounds.width/2, bounds.height/2);
- }
-
- // now draw the target rectangle
- g.drawRect(rectangle.x, rectangle.y,
- rectangle.width, rectangle.height);
- }
-
- /*
- * Mouse methods
- */
- // mouseDown - count the number of hits/misses
- public boolean mouseDown(java.awt.Event evt, int nX, int nY)
- {
- // first position the mouse
- mouse.move(nX, nY);
-
- // now see if we hit it
- if(rectangle.inside(nX, nY))
- {
- switch (++onaroll)
- {
- case 1:
- getAppletContext().showStatus(
- "HIT IT AGAIN! AGAIN!");
- break;
- case 2:
- case 3:
- case 4:
- getAppletContext().showStatus(
- "YOU'RE ON A ROLL:"
- + onaroll
- + "Hits!");
- break;
- default: // this handles 5 or more hits in a row
- getAppletContext().showStatus(
- "You're on your way to THE HALL OF FAME:"
- + onaroll
- + "Hits!");
- }
- play(getCodeBase(), "sounds/that.hurts.au");
- }
- else
- {
- // miss!
- getAppletContext().showStatus("You missed at ("
- + nX
- + ", "
- + nY
- + ")");
- play(getCodeBase(), "sounds/thin.bell.au");
- onaroll = 0;
- }
- return true;
- }
-
- // mouseMove - record the mouse location
- public boolean mouseMove(java.awt.Event evt, int nX, int nY)
- {
- mouse.move(nX, nY);
- return true;
- }
- }
-