import draw.*; class LightWorld extends World { Posn redCenter, yellowCenter, greenCenter; int height, width, radius; LightWorld(int width, int height) { this.height = height; this.width = width; this.radius = width/2; this.redCenter = new Posn(radius, (height/3 - radius)); this.yellowCenter = new Posn(radius, (height/2)); this.greenCenter = new Posn(radius, ((2 * height/3) + radius)); } //Draws a traffic signal of the specified size. assumes that it's a vertical, not horizontal light boolean draw() { // redCenter ... yellowCenter ... greenCenter ... height ... width ... radius ... theCanvas return this.theCanvas.start(width,height) && this.theCanvas.drawDisk(redCenter,radius,new Red()) && this.theCanvas.drawCircle(yellowCenter,radius,new Yellow()) && this.theCanvas.drawCircle(greenCenter,radius,new Green()); } //To draw a Disk for the specified light, of the given color boolean On(Posn light, Color c) { // redCenter ... yellowCenter ... greenCenter ... height ... width ... radius ... theCanvas return this.theCanvas.drawDisk(light,radius,c); } //To color over the Disk at the specified light, and leave an outline of it's On color boolean Off(Posn light, Color c) { // redCenter ... yellowCenter ... greenCenter ... height ... width ... radius ... theCanvas return this.theCanvas.drawDisk(light,radius,new White()) && this.theCanvas.drawCircle(light,radius,c); } // Methods to turn On and Off specific lights boolean redOn() { return On(redCenter, new Red()); } boolean redOff() { return Off(redCenter, new Red()); } boolean yellowOn() { return On(yellowCenter, new Yellow()); } boolean yellowOff() { return Off(yellowCenter, new Yellow()); } boolean greenOn() { return On(greenCenter, new Green()); } boolean greenOff() { return Off(greenCenter, new Green()); } //The following methods are used to make animations boolean erase() { return true; } World onKeyEvent(String key) { return this; } World onTick() { return this; } } class TrafficLight { boolean red, yellow, green; LightWorld trafficLight; TrafficLight() { this.red = true; this.yellow = false; this.green = false; this.trafficLight = new LightWorld(50,150); } //To display the traffic light graphically void displayLight() { this.trafficLight.draw(); } //new TrafficLight().change() -> void // To change the lights in the US fashion for this TrafficLight // Note, will modify the booleans red, yellow, and green void change() { // this.red... this.yellow ... this.green ... trafficLight.LightWorldMethod() if (this.red) { this.red = false; this.trafficLight.redOff(); this.green = true; this.trafficLight.greenOn(); } else if (this.yellow) { this.red = true; this.trafficLight.redOn(); this.yellow = false; this.trafficLight.yellowOff(); } else { this.green = false; this.trafficLight.greenOff(); this.yellow = true; this.trafficLight.yellowOn(); } } //new TrafficLight().change() -> void // To change the lights in the British fashion for this TrafficLight // Note, will modify the booleans red, yellow, and green void changeBritishStyle() { // this.red... this.yellow ... this.green ... this.trafficLight.LightWorldMethod() ... if (this.red && !this.yellow) { this.yellow = true; this.trafficLight.yellowOn(); } else if (this.red && this.yellow) { this.red = false; this.trafficLight.redOff(); this.yellow = false; this.trafficLight.yellowOff(); this.green = true; this.trafficLight.greenOn(); } else if (this.green && !this.yellow) { this.yellow = true; this.trafficLight.yellowOn(); } else if (this.green && this.yellow) { this.green = false; this.trafficLight.greenOff(); } else { this.red = true; this.trafficLight.redOn(); this.yellow = false; this.trafficLight.yellowOff(); } } }