CSC 241- Assignment 1 (Due September 19th, 2003)

You will build two classes for this project: Light and trafficLight. 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. Keep in mind that all .class files and .html files associated with the assignment need to be public as well; however, .java files need to be left protected. To find the right Graphics methods, follow the link to Graphics class. Also, check out the paint method in hellowAndGoodbye for hints.


Light class

The Light class gives us objects that represent simple lights with on or off states. Lights are drawable objects; they know their size, and coordinates on a canvas, and are drawn as a circle in two possible colors depending on their on/off state. When a Light's switchLight method is invoked; it switches its state from on to off or off to on. When a Light's draw method is invoked; it draws itself on a Graphics object passed to it--in the appropriate color and coordinates. Here is the specification; note that there are only the constructor and two methods that are left for you to define:


import java.awt.*;

public class Light {

  /*Things a Light object knows */
  boolean on_=false;
  Color onColor_;
  int size_;
  int x_;
  int y_;

  /*The constructor, just set the Light's fields to the parameters sent in*/
  public Light (int size, Color onColor, int x, int y) {
  }

  /*Controls the on_ variable; if its true, sets it to false; if its false, 
    sets it to true.(i.e. turn off the light if on or turn it on if off)*/
  public void switchLight() {
  }

  /*set g's color to either lightGray or onColor and draw a circle with
    the appropriate size and the known coordinates*/
  public void draw(Graphics g) {
  }

  public boolean on() {return on_;}

  public Color onColor() {return onColor_;}

  public int size() {return size_;}

  public int x () {return x_;}

  public int y () {return y_;}
}

Create Light.java in yourtraffic directory using the above as the skeleton for building the class.

In your browser window, download lightApplet.html and lightApplet.java. In each case, click on their link. Once the page is loaded, use the Save Page As choice in the File pull-down menu of the browser and save the file in your traffic directory. You now should have a Light.java, a lightApplet.java, and a ligthApplet.html in your traffic directory.

Compile the .java files and make the .class files and the .html file public. Remember, the traffic directory itself needs to be public as well.

Simply typing appletviewer lightApplet.html in a terminal window (you need to be in the traffic directory) will open an appletviewer frame and execute the applet for you; you won't need to test things in a browser. The lightApplet must work the same way as it did for me; in other words, it should run like it did when you loaded my applet in the browser earlier.

Add a link like the following to your 241.html file: <A_HREF="http://www.oswego.edu/~your_username/classes/csc241/traffic/lightApplet.html">lightApplet</a>


trafficLight class

trafficLight is where we graphically represent a traffic light. A trafficLight has three Light objects (red, yellow, and green) where only one of the lights may be on at any point in time. Here is the specification of the trafficLight class, there are only three methods that are left for you to define (status, change, and paint):


import java.awt.*;

/** 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 pixles will be the
    default value for the height, unless specified differently at
    construction.  A traffic light must start as red (i.e. only
    the red light should be on when constructed).  Each time the 
    change method is invoked, the color changes and 
    repaint() call is issued to redraw the trafficLight.
    The color sequence is red to green to yellow and back to red.
 **/

public class trafficLight extends Canvas {
  Light red_;
  Light green_;
  Light yellow_;
  Light current_;
  int height_;

  public trafficLight () {
    height_ = 360;
    init();
  }
  public trafficLight (int h) {
    height_ = h;
    init();
  }
  void init() {
    setSize( height_+4, height_+4 );
    red_ =  new Light (lightSize (),Color.red,
                       widthSeparation(),heightSeparation());
    red_.switchLight();
    green_ = new Light (lightSize (),Color.green,widthSeparation(),
                        heightSeparation()*3+lightSize()*2);
    yellow_ = new Light (lightSize (),Color.yellow,widthSeparation(),
                         heightSeparation()*2+lightSize());
    current_=red_;
  }

  public int height () { return height_;}
  public int width () { return height_/3;}
  public int widthSeparation () { return width()/20;}
  public int lightSize() { return width()-widthSeparation()*2;}
  public int heightSeparation () { return (height_-lightSize()*3)/4;}
  
  /** Determine which light is on and return its color (red,yellow, or green)
      by returning the color of the light that is currently on
   **/
  public Color status () {
  }

  /** 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 change () {
  }

  /** Draw a traffic light. You need a filled rectangle to represent the body
      of the traffic light, and you need the three individual lights to draw 
      themselves.  Use width() and height() for dimensions of
      the rectangle.
   **/
  public void paint(Graphics g) {
  }
}

Use traffic1.html and traffic1.java andtraffic2.html and traffic2.java to test your trafficLight class. Follow the process described for Light.


What/How to turn in Assignment

Email your .java files (Light.java, and trafficLight.java) as attachments to me. There should be links to the three applets that you have downloaded in your 241.html page.

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.