FileDocCategorySizeDatePackage
RaceApplet.javaAPI DocExample2134Tue Dec 12 18:57:48 GMT 2000None

RaceApplet

public class RaceApplet extends Applet implements Runnable

Fields Summary
private static final int
NUMRUNNERS
private static final int
SPACING
private Runner[]
runners
private Thread
updateThread
Constructors Summary
Methods Summary
public voidinit()


       
        String raceType = getParameter("type");
        for (int i = 0; i < NUMRUNNERS; i++) {
            runners[i] = new Runner();
            if (raceType.compareTo("unfair") == 0)
                    runners[i].setPriority(i+2);
            else
                    runners[i].setPriority(2);
        }
        if (updateThread == null) {
            updateThread = new Thread(this, "Thread Race");
            updateThread.setPriority(NUMRUNNERS+2);
        }
    
public booleanmouseDown(java.awt.Event evt, int x, int y)

        if (!updateThread.isAlive())
            updateThread.start();
        for (int i = 0; i < NUMRUNNERS; i++) {
            if (!runners[i].isAlive())
                runners[i].start();
        }
        return true;
    
public voidpaint(java.awt.Graphics g)

        g.setColor(Color.lightGray);
        g.fillRect(0, 0, size().width, size().height);
        g.setColor(Color.black);
        for (int i = 0; i < NUMRUNNERS; i++) {
            int pri = runners[i].getPriority();
            g.drawString(new Integer(pri).toString(), 0, (i+1)*SPACING);
        }
        update(g);
    
public voidrun()

	Thread myThread = Thread.currentThread();
        while (updateThread == myThread) {
            repaint();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) { }
        }
    
public voidstop()

        for (int i = 0; i < NUMRUNNERS; i++) {
            if (runners[i].isAlive())
                runners[i] = null;
        }
        if (updateThread.isAlive())
            updateThread = null;
    
public voidupdate(java.awt.Graphics g)

        for (int i = 0; i < NUMRUNNERS; i++) {
            g.drawLine(SPACING, (i+1)*SPACING, 
		       SPACING + (runners[i].tick)/1000, (i+1)*SPACING);
        }