FileDocCategorySizeDatePackage
RaceApplet.javaAPI DocExample2528Tue Dec 12 18:57:46 GMT 2000None

RaceApplet

public class RaceApplet extends JApplet 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);
        }
        addMouseListener(new MyAdapter());
        setContentPane(new AppletContentPane());
    
public voidrun()

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

        for (int i = 0; i < NUMRUNNERS; i++) {
            if (runners[i].isAlive())
                runners[i] = null;
        }
        if (updateThread.isAlive())
            updateThread = null;