/* Race Applet Eric Jewart CPS 1 Spring 1998 This applet is the main class. It creates a RaceTrack, a car that extends RaceCar, and some other components. It also sticks itself in a Thread and runs. */ package awb; import java.awt.*; import java.util.*; import java.net.*; public class Race extends Panel implements Runnable { public final static int MS_PER_UPDATE = 100; // must be >= 10 public final static int MPP = 2; // meters per pixel private URL myCodeBase; private RaceTrack myTrack; private RaceCar myCar; private CarInfo myCarInfo; private RaceInfo myRaceInfo; private ScoreReporter myScorer; private Button myStartButton, myStopButton, myAdvanceButton, myReportButton; private Thread mySelf; private boolean myRaceIsOn = false; public Race(RaceCar rc) { setBackground(Color.white); try { myCodeBase = new URL("http://www.cs.duke.edu/~ecj/cps1/awb/track1.dat"); } catch (MalformedURLException ex) { } myCar = rc; myTrack = new RaceTrack(this, myCar, myCodeBase, "track1.dat"); System.out.print("creating components"); System.out.flush(); myCarInfo = new CarInfo(myCar); System.out.print(".."); System.out.flush(); myRaceInfo = new RaceInfo(this, myTrack); System.out.print(".."); System.out.flush(); myScorer = new ScoreReporter(this,myCar.getLogin()); System.out.print(".."); System.out.flush(); myStartButton = new Button("Go"); System.out.print(".."); System.out.flush(); myStopButton = new Button("Stop"); System.out.print(".."); System.out.flush(); myAdvanceButton = new Button("Advance"); System.out.print(".."); System.out.flush(); myReportButton = new Button("Report Score"); myReportButton.disable(); // have to go three laps min System.out.println("done"); setLayout(new BorderLayout()); add("Center", myTrack); Panel p = new Panel(); GridBagLayout g = new GridBagLayout(); p.setLayout(g); Constrainer.addComponent(g,myCarInfo,p,0,0,1,5,100,100, GridBagConstraints.BOTH,GridBagConstraints.CENTER); Constrainer.addComponent(g,myRaceInfo,p,1,0,1,5,100,100, GridBagConstraints.BOTH,GridBagConstraints.CENTER); Constrainer.addComponent(g,myStartButton,p,2,0,1,1,100,0, GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER); Constrainer.addComponent(g,myStopButton,p,2,1,1,1,100,0, GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER); Constrainer.addComponent(g,myAdvanceButton,p,2,2,1,1,100,0, GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER); Constrainer.addComponent(g,myReportButton,p,2,3,1,1,100,0, GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER); Constrainer.addComponent(g,new Panel(),p,2,4,1,1,100,300, GridBagConstraints.BOTH,GridBagConstraints.CENTER); add("South", p); } public void run() { while (myRaceIsOn) { myTrack.update(); myCarInfo.update(); myRaceInfo.update(); try { Thread.sleep(MS_PER_UPDATE - (MS_PER_UPDATE/10)); } catch (InterruptedException ex) { } } mySelf = null; // must be done at end of while } public URL getCodeBase() { return myCodeBase; } public void startRace() { // make sure not already started if (myRaceIsOn) return; myRaceIsOn = true; myRaceInfo.startRace(); mySelf = new Thread(this); mySelf.start(); } public void stopRace() { if (! myRaceIsOn) return; myRaceIsOn = false; myRaceInfo.stopRace(); myCarInfo.update(); // see if car crashed, finished } public void incrementLap() { myRaceInfo.incrementLap(); if (myRaceInfo.getLaps() >= 3) myReportButton.enable(); } public boolean action(Event e, Object o) { if (e.target == myStartButton) { startRace(); return true; } if (e.target == myStopButton) { stopRace(); return true; } if (e.target == myAdvanceButton) { myTrack.update(); myCarInfo.update(); return true; } if (e.target == myReportButton) { this.disable(); stopRace(); myScorer.show(myRaceInfo.getAvgTime()); return true; } return super.action(e,o); } }