handout #8

CSE190L—Object Oriented Programming & Design

Programming Assignment #3

due: Monday, 4/16/07, 11 pm

The assignment will give you practice with event handling as described in chapter 8 of Core Java.  You are to write a program that allows the user to draw a series of interconnected lines.  You should use the overall structure shown in the final Colors example in handout #7: a main class that creates an instance of a frame; a frame class that stores a reference to a panel and a JFrame with a set of buttons in a panel on top and the painting panel in the center; and a panel class that is a subclass of JPanel.  All controls (buttons, mouse listener, etc) should be defined in the frame, not the panel.

The user will use the mouse to select specific points on the screen.  Your drawing panel should draw all possible lines connecting the various points.  In other words, for every two points that aren’t equal to each other, you are to draw a line between those two points.  This will require you to introduce a MouseListener that handles mouse clicks. Be sure you write your loops so that you don’t draw the same line twice.  In addition to the lines, you are to draw a centered circle of diameter 8 at each of the points.

Your program should also have four buttons labeled: Foreground, Background and Clear. The Foreground and Background buttons should change the foreground and background color of the drawing panel and the Clear button should go back to the original state of 0 points and no lines.

In changing the foreground and background colors, you are to cycle through at least 8 different colors of your choice.  As the user clicks on one of these buttons, the color will change and cycle through a sequence of distinct colors that includes at least 8 total colors.  You should use the same sequence of colors for both the foreground and background but you should make sure that the foreground and background colors are never equal.  As you cycle through the colors, you should skip a color if it would lead to the foreground and background being the same color.

Your program should also display at the bottom of the panel the current number of points (initially 0, increasing by 1 each time the user selects another point, reset to 0 when the Reset button is pressed).  You should use an 18-point bold Serif font.  The text is to be centered horizontally and the baseline should be the point size above the bottom of the panel.  The panel itself should be set to 500 wide by 400 high.

You should include constants for the circle diameter and the point size.  You should store the points using an ArrayList and you are allowed to declare your variables and fields to be of type ArrayList rather than List to avoid the potential ambiguity with java.awt.List.  The number of points should be drawn in the foreground color and you should correctly display “1 point” versus “2 points”, “0 points”, “3 points”, etc.  The panel of buttons should have a white background.

Below is an example snapshot taken on a Windows machine:

There is no source folder for this assignment because you are to define all of the files yourself.  Follow the standard turnin instructions on the class webpage.