/* Name: Chris Johnson johnson@dcs.gla.ac.uk Last modified: 27/10/98 Description: Declares three lights and places them in different positions on the canvas. From an idea originally suggested by: Quintin Blane qablane@surfaid.org */ import java.awt.*; import java.applet.Applet; public class Traffic_Lights extends Canvas { A_Light Red_Light = new A_Light("Red", 0, 0); A_Light Amber_Light = new A_Light("Amber", 0, 50); A_Light Green_Light = new A_Light("Green", 0, 100); private Lights parent; Traffic_Lights (Lights owner) { super(); // make sure this is exactly like any other // canvas - avoids us having to initialise // lots of other variables - see Judy Bishop // page 278-280. parent = owner; } public void set_lights(String the_color) { Red_Light.off(); Green_Light.off(); Amber_Light.off(); if(the_color.equals("Amber")) Amber_Light.on(); else if(the_color.equals("Green")) Green_Light.on(); else Red_Light.on(); update(getGraphics()); // update redraws the background of the canvas and then // calls paint() - see below. We need to redraw the // background of the canvas in order to get rid of the // previous lights that might have been on. } public void paint(Graphics g) { Red_Light.paint(g); Green_Light.paint(g); Amber_Light.paint(g); } }