home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / raceappl.jav < prev    next >
Encoding:
Text File  |  1996-02-26  |  2.8 KB  |  93 lines

  1. /*
  2.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18.  
  19. public class RaceApplet extends java.applet.Applet implements Runnable {
  20.  
  21.     final static int NUMRUNNERS = 2;
  22.     final static int SPACING = 20;
  23.  
  24.     Runner runners[] = new Runner[NUMRUNNERS];
  25.  
  26.     Thread updateThread;
  27.  
  28.     public void init() {
  29.     String raceType = getParameter("type");
  30.     for (int i = 0; i < NUMRUNNERS; i++) {
  31.         runners[i] = new Runner();
  32.         if (raceType.compareTo("unfair") == 0)
  33.             runners[i].setPriority(i+1);
  34.         else
  35.             runners[i].setPriority(2);
  36.         }
  37.         if (updateThread == null) {
  38.             updateThread = new Thread(this, "Thread Race");
  39.             updateThread.setPriority(NUMRUNNERS+1);
  40.         }
  41.     }
  42.  
  43.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  44.     if (!updateThread.isAlive())
  45.             updateThread.start();
  46.     for (int i = 0; i < NUMRUNNERS; i++) {
  47.         if (!runners[i].isAlive())
  48.             runners[i].start();
  49.     }
  50.     return true;
  51.     }
  52.  
  53.     public void paint(Graphics g) {
  54.         g.setColor(Color.lightGray);
  55.         g.fillRect(0, 0, size().width, size().height);
  56.         g.setColor(Color.black);
  57.         for (int i = 0; i < NUMRUNNERS; i++) {
  58.         int pri = runners[i].getPriority();
  59.         g.drawString(new Integer(pri).toString(), 0, (i+1)*SPACING);
  60.     }
  61.         update(g);
  62.     }
  63.  
  64.     public void update(Graphics g) {
  65.         for (int i = 0; i < NUMRUNNERS; i++) {
  66.         g.drawLine(SPACING, (i+1)*SPACING, SPACING + (runners[i].tick)/1000, (i+1)*SPACING);
  67.     }
  68.     }
  69.  
  70.     public void run() {
  71.         while (updateThread != null) {
  72.             repaint();
  73.             try {
  74.         updateThread.sleep(10);
  75.         } catch (InterruptedException e) {
  76.         }
  77.         }
  78.     }    
  79.  
  80.     public void stop() {
  81.     for (int i = 0; i < NUMRUNNERS; i++) {
  82.         if (runners[i].isAlive()) {
  83.             runners[i].stop();
  84.             runners[i] = null;
  85.         }
  86.         }
  87.     if (updateThread.isAlive()) {
  88.             updateThread.stop();
  89.             updateThread = null;
  90.     }
  91.     }
  92. }
  93.