home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / AppB / JumpingBox / MouseRun.class (.txt) next >
Encoding:
Java Class File  |  1996-08-02  |  3.3 KB  |  105 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Event;
  4. import java.awt.Graphics;
  5. import java.awt.Point;
  6. import java.awt.Rectangle;
  7.  
  8. public class MouseRun extends Applet implements Runnable {
  9.    Point mouse = new Point(0, 0);
  10.    Rectangle rectangle = new Rectangle(20, 20);
  11.    Rectangle bounds = new Rectangle();
  12.    Point vector = new Point(1, 1);
  13.    // $FF: renamed from: me java.lang.Thread
  14.    Thread field_0;
  15.    int onaroll;
  16.  
  17.    public void start() {
  18.       if (this.field_0 == null) {
  19.          this.field_0 = new Thread(this);
  20.          this.field_0.start();
  21.       }
  22.  
  23.    }
  24.  
  25.    public void stop() {
  26.       this.field_0 = null;
  27.    }
  28.  
  29.    public boolean mouseMove(Event evt, int nX, int nY) {
  30.       this.mouse.move(nX, nY);
  31.       return true;
  32.    }
  33.  
  34.    public boolean mouseDown(Event evt, int nX, int nY) {
  35.       this.mouse.move(nX, nY);
  36.       if (this.rectangle.inside(nX, nY)) {
  37.          switch (++this.onaroll) {
  38.             case 1:
  39.                ((Applet)this).getAppletContext().showStatus("HIT IT AGAIN! AGAIN!");
  40.                break;
  41.             case 2:
  42.             case 3:
  43.             case 4:
  44.                ((Applet)this).getAppletContext().showStatus("YOU'RE ON A ROLL:" + this.onaroll + "Hits!");
  45.                break;
  46.             default:
  47.                ((Applet)this).getAppletContext().showStatus("You're on your way to THE HALL OF FAME:" + this.onaroll + "Hits!");
  48.          }
  49.  
  50.          ((Applet)this).play(((Applet)this).getCodeBase(), "sounds/that.hurts.au");
  51.       } else {
  52.          ((Applet)this).getAppletContext().showStatus("You missed at (" + nX + ", " + nY + ")");
  53.          ((Applet)this).play(((Applet)this).getCodeBase(), "sounds/thin.bell.au");
  54.          this.onaroll = 0;
  55.       }
  56.  
  57.       return true;
  58.    }
  59.  
  60.    public void run() {
  61.       for(; this.field_0 != null; ((Component)this).repaint()) {
  62.          try {
  63.             Thread.sleep(20L);
  64.          } catch (InterruptedException var3) {
  65.             this.field_0 = null;
  66.          }
  67.       }
  68.  
  69.    }
  70.  
  71.    public void init() {
  72.       this.bounds.resize(((Component)this).size().width - 1, ((Component)this).size().height - 1);
  73.    }
  74.  
  75.    public void paint(Graphics g) {
  76.       g.drawRect(0, 0, this.bounds.width, this.bounds.height);
  77.       Point center = new Point(this.rectangle.x + this.rectangle.width / 2, this.rectangle.y + this.rectangle.height / 2);
  78.       this.vector.x = -1;
  79.       if (this.mouse.x < center.x) {
  80.          this.vector.x = 1;
  81.       }
  82.  
  83.       if ((center.x - this.mouse.x) * this.vector.x > 100) {
  84.          this.vector.x = 0;
  85.       }
  86.  
  87.       this.vector.y = -1;
  88.       if (this.mouse.y < center.y) {
  89.          this.vector.y = 1;
  90.       }
  91.  
  92.       if ((center.y - this.mouse.y) * this.vector.y > 100) {
  93.          this.vector.y = 0;
  94.       }
  95.  
  96.       this.rectangle.translate(this.vector.x, this.vector.y);
  97.       Rectangle intersect = this.rectangle.intersection(this.bounds);
  98.       if (!intersect.equals(this.rectangle)) {
  99.          this.rectangle.move(this.bounds.width / 2, this.bounds.height / 2);
  100.       }
  101.  
  102.       g.drawRect(this.rectangle.x, this.rectangle.y, this.rectangle.width, this.rectangle.height);
  103.    }
  104. }
  105.