The goal of this assignment is to develop a traffic light applet. The applet will consist of a traffic light and a switch that controls it. In order to develop this applet, you will first develop two other classes and test them. Create a directory off of your public-html/classes/csc241 directory named traffic, make it public. All .java, .class, and .html files for this assignment must reside in this directory.
The Light class gives us objects that represents simple lights with on or off states. Lights are NOT graphically represented in this class, we only wish for each light to know its current state (on/off) and know its own color depending on that state. Here is the specification:
import java.awt.*; public class Light { /** This is a light class with two states, on or off. The light color is determined at construction for each state. Whether the light starts as on or off is also determined at construction. No exceptions handling is required. **/ boolean on_; Color onColor_; Color offColor_; public Light (boolean on, Color onColor, Color offColor) /** Constructor that is paramaterized to set the fields of a Light object. **/ public void switchLight() /** If light is on, turn it off, otherwise turn it on **/ public boolean on() /** return on/off state, ture if on and false otherwise. **/ public Color color() /** return current color, depending on the light being on or off **/ }
Develop Light.java in the traffic directory and test it with testLight.java.
trafficLight is where we graphically represent a traffic light. A trafficLight has three lights where only one of the lights may be on at any point in time. To find the right Graphics methods, follow the link to Graphics class. Also, check out hello3.java for hints on drawing things. Here is the specification of the trafficLight class, there are only three methods that are left undefined (status, change, and paint):
import java.awt.*; public class trafficLight extends Canvas { /** Traffic light class graphically represents a traffic light as a rectangle with three internal circles. The height of the the rectangle is determined at construction. 360 pixels will be the default value for the height unless specified differently at construction. The other dimensions in the traffic light are dependent on the height. A traffic light must start as red (i.e. only the red light should be on). Each time the change method is invoked the traffice light changes state and gets redrawn. The color sequence is red to green to yellow and back to red. **/ protected Light red_ = new Light (true,Color.red,Color.lightGray); protected Light green_ = new Light (false,Color.green,Color.lightGray);; protected Light yellow_ = new Light (false,Color.yellow,Color.lightGray);; protected Light current_ = red_;//references the traffic light object that is on protected int height_; public trafficLight () { height_ = 360; resize( height_+4, height_+4 ); } public trafficLight (int h) { height_ = h; resize( height_+4, height_+4 ); } public Color status () /** Determine which light is on and return its color (red,yellow, or green) **/ public int height () /** Return the height of the trafficLight **/ /* Private methods used for determining dimensions when drawing the traffic light */ int width () { return height()/3;} int widthSeparation () { return width()/20;} int radius () { return (width()-widthSeparation()*2)/2;} int heightSeparation () { return (height_-radius()*6)/4;} public void change () /** Change light depending on which light current_ references. if current_ == red_ turn off red_, turn on green_, and have current_ reference green_ if current_ == green_ turn off green_, turn on yellow_, and have current_ reference yellow_ if current_ == yellow_ turn off yellow_, turn on red_, and have current_ reference red_ repaint(); to redraw the traffic light **/ public void paint(Graphics g) /** Draw the traffic light, use the methods such as width for determining dimensions **/ }
Use traffic traffic2 to test your trafficLight class.
You will develop your first applet, a traffic light that is controlled by a switch button. To see what your light should look like follow the link for Traffic Light . You might as well use my html file for this applet. Use hello3.java for hints on creating a button and writing an action method. traffic2.java should make clear how trafficLight objects are constructed and changed. Add a link to your csc241 page for this traffic light applet.
Email your .java files (Light.java, trafficLight.java, and your applet) as attachments to me.
Remember that your .class file and the .html file and the path to them must be public, but DO NOT make your .java files public, in fact, perform a chmod 700 *.java in the traffic directory to make sure. Visit Lab #1 to see how you make files public.