CSC 241- Assignment 1 (Due February 7th, 2001)

You will learn the basic concepts involved in writing applets, doing drawing, and handling events in java.

Start by Creating a directory off of your public-html/classes/csc241 directory named Light, make it public. All .java, .class, and .html files for this assignment must reside in this directory.


Light class

light class gives us objects that represent simple lights with on or off states. A Light is graphically represented here as a circle with a black border and an on color and an off color depending on the state of the light.

Develop light.java in the Light directory using the following as the skeleton for the class and fill in the missing parts.


import java.awt.*;

public class light extends Canvas {
/** 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.
 **/
  protected Color onColor_=Color.red;
  protected Color offColor_=Color.lightGray;
  protected boolean on_=true;
  protected int size_=360;

  /** The default constructor for class light **/
  public light () {
    setSize( size_+4, size_+4 );
  }

  public light (int s, boolean on, Color onColor, Color offColor) {
    /* Write the assignment statements to assign the fields, onColor_, on_, etc.
       to the parameters in this constructor */
    setSize( size_+4, size_+4 );
  }

  public int lightSize ()  { /* return the size */}
  public boolean on() {/* return true if light on; otherwise, false */}
  public void change () {
    /*turn the light on, if its off; otherwise, turn it off */
    repaint(); /*automatically repaints the light */
  }

  public void paint(Graphics g) {
    /* draw two black borders for our circular light.
       fill the inside portion with either the on or off color depending on the
       state of the light.
       To find the right Graphics methods, follow the link to 
       Graphics class.  Also, check out the paint method in helloAndGoodbye.java for hints on drawing things.  
    */
  }
}


Test you light class by first downloading lightApplet.java and lightApplet.html. When you click each link and bring the page up, you can use the save as option of the file pull-down menu in your brower window to save them. Just be sure to place both files in your Light directory.

Compile the .java files and make the .class file and the .html file public. You should now be able to type the URL for your lightApplet.html file and bring it up in your web browser.


Write another applet that will behave like my lightApplet2.html which acts like a traffic light. Use my .html file. Here are some hints and thoughts on writing this applet ...

What/How to turn in Assignment

Email your .java files (light.java, lightApplet2.java) as attachments to me.

Also include links to your lightApplet.html and lightApplet2.html pages in your 241.html file.

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 Light directory to make sure. Visit Lab #1 to see how you make files public.