CPSC 120B Fall 2003
Program 2: Traffic Light
Due Friday, October 11

Problem

Write a Java applet that draws a picture of a traffic intersection with the following components: In addition, your program should be resizable from 200x200 up to 1000x700 and the picture should just look like it it being shrunk or blown up. That is, it should look the same, just a different size. More on resizing below.

Program Requirements

As usual, your program should use PAGE_HEIGHT and PAGE_WIDTH constants for the height and width of the page. But instead of just setting these constants to literal integers, call the getHeight() and getWidth() methods to get their values. For example, you would initialize PAGE_HEIGHT as follows:
    final int PAGE_HEIGHT = getHeight();
The getHeight() and getWidth() methods are available to all applets, and in our situation they retrieve the values set in the applet tag of the .html file from which the applet is called. This way nothing in the .java file needs to change for the applet to be resized; it doesn't even have to be recompiled.

Once the page height and width have been established, method void resize(int width, int height) can be called to resize the applet window to the given width and height. Be sure to do this after PAGE_HEIGHT and PAGE_WIDTH get values but before you do any drawing.

IMPORTANT: The applet is resized by changing the values of the Height and Width attributes to the applet tag in the .html file. No changes should be needed in the .java file for resizing.

Since the page width and page height can change, most of your coordinates will have to be directly or indirectly related to the PAGE_WIDTH and PAGE_HEIGHT constants. For example, the width of the road might be 1/5 of PAGE_HEIGHT, the width of the traffic light might 1/2 the width of the road, and the height of the traffic light might be half its width. These are just examples; experiment and find values that you like the looks of!

Your traffic light should randomly be red, yellow, or green with equal probability. Be sure the unlighted colors are visible as well, just somehow deemphasized (e.g., lighter in color, or not filled in).

Documentation and Style

Provide a program header that gives your name, the name of the file, and a description of the program. Use good names for variables and constants, and use an explanatory comment whenever a variable's purpose is not clear from its name. Also be sure to follow the capitalization conventions discussed in class. Your code should fall into logical sections as you draw each of the components in the picture; be sure to document each section.

Good style is very important in keeping this program readable. Remember that the sizes and coordinates of almost everything you draw will be relative to the page height and width constants. To keep this from turning into a mess, use well-named variables to store these values. For example, you might have variables called roadWidth to hold the width of the road, carLength for the length of the car, and wheelDiam for the diameter of a wheel. You should also use variables for the coordinates of the stoplight, of the road boundaries, and so on. Doing this will improve readability tremendously.

Constants also play an important role in this program. For example, if you use a non-standard color for something, don't just stick the code for the color into the constructor -- create a Color constant with an appropriate name (e.g., GRAYISH_BROWN) and create a new color object with the appropriate value to store in that constant.

Enhancements

You can add up to 5 points to your program grade by enhancing your picture in interesting ways. Add a speed limit sign (best if the font size changes with the page size), a bird or flower that shows up in a random location (but not in the road!), or something else that adds to the picture. The more interesting your enhancement(s), the more credit you'll get.

What to Turn In

Turn in hardcopy of your program and e-mail the source code to bloss@roanoke.edu. Put cs120 prog2 in the subject line. Both the hardcopy and the e-mail are due by 4:00 on the date above.