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).
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.