home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / threads / example / RaceApplet.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.8 KB  |  91 lines

  1. /*
  2.  * Copyright (c) 1995-1997 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 = null;
  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+2);
  34.             else
  35.                     runners[i].setPriority(2);
  36.         }
  37.         if (updateThread == null) {
  38.             updateThread = new Thread(this, "Thread Race");
  39.             updateThread.setPriority(NUMRUNNERS+2);
  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 (Thread.currentThread() == updateThread) {
  72.             repaint();
  73.             try {
  74.                 Thread.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] = null;
  84.             }
  85.         }
  86.         if (updateThread.isAlive()) {
  87.             updateThread = null;
  88.         }
  89.     }
  90. }
  91.