CS 2121 Introduction to Java:
Programming Assignment 6

Due May 7, 30 points


Overview

For this lab you will add a "Colors" menu to the program that you wrote for Programming Assignment 3. This menu will contain "Text Color...", "Draw Color...", and "Fill Color..." menu items for changing colors. Each of these menu items will pop up a JColorChooser dialog for selecting a color.

You will need to design most of the code for this assignment. Only a brief sketch is given here. However, there is not very much code to add or change. If you start early enough to give yourself time to think about it and ask questions, it can be one of the easier assignments. There are two major parts of the assignment, which are sketched in the following sections.

Color Variables and Related Changes

You will need three Color instance variables in each of the classes for the text, draw and fill colors. In the GraphicsApplet class, you will need a getter method for each of its color variables. The getter methods should be invoked in the DrawPanel updateDrawing() method, with the returned values assigned to the appropriate DrawPanel color variable. The only other changed needed in the DrawPanel class is replacing the color constants used in the paintComponent() method with the new color variables.

Action Variables and Menus

You will need an action instance variable for each of the three color choices. These are similar to the action variables in the previous assignment. Their actionPerformed() methods should have code based on the following code outline.
    Color c = JColorChooser.showDialog(null, dialog title, color variable);
    if (c != null) {
	assign c to color variable
	send updateDrawing() message to the draw panel
    }

You will also need to add code in the GraphicsApplet init() method to set up the name property for each of the actions and install them in a menu. Use the code from the previous assignment as an example.

Caution

The new color and action variables in the GraphicsApplet class should be declared before the drawPanel variable. Constructors in initialized variable declarations are executed in order. When the drawPanel is constructed, its constructor invokes updateDrawing(), which invokes all of the GraphicsApplet getter methods. If the variables in the getter methods have not yet been constructed, you will get a run time error.

Checking Your Program

For best results, the height specified in your applet HTML file should be changed to 446. Your applet should look like the following demonstration applet.

Demonstration Applet