Step 8


Overview

In this step, you will do two things: change the colors in the draw panel depending on the type of draw method, and disable drawing parameter controls that are not needed for a drawing method.

Changing Colors

Your objective is to get drawings made in a color that depends on the drawing method. For this objective, there are three kinds of drawing methods: drawString, other draw methods, and fill methods. You want to use black for the first kind, red for the second kind, and blue for the third kind. The easiest way to accomplish your objective is with an extended if-else statement in the DrawPanel paintComponent() method.

An extended if-else statement has the following form.

    if (condition 1) {
	statements 1
    } else if (condition 2) {
	statements 2
		.
		.
		.
    } else if (condition n) {
	statements n
    } else {
	otherwise statements
    }
When this statement is executed, condition 1, condition 2, ... , condition n are tested in order. For the first one whose value is true, the corresponding statements block is executed. If none of the conditions is true then the otherwise statements block is executed.

For controling colors, you need an extended if-else statement with three possible outcomes.

    if (condition 1) {
	statements 1
    } else if (condition 2) {
	statements 2
    } else {
	otherwise statements
    }
Your condition 1 should either test if the method name is equal to "drawString" or if it ends with "String". Then statements 1 should set the color to black. Your condition 2 should test if the method name starts with "draw". Then statements 2 should set the color to red. The otherwise statements should set the color to blue. For condition 2, there is a String startsWith() method that is similar to the endsWith() method.

Disabling Unused Drawing Parameter Controls

All of the changes that you need to make for disabling unused controls are in the GraphicsApplet class.

There are two places where you need to have code to enable or disable sliders according to the drawing method: when the control panel is constructed (in the createControlPanel() helper method) and when the user selects a new drawing method (in the method control listener). To avoid duplicate code, you should write an enableSliders() method and invoke it in the two places.

The enableSliders() method has void return type and no parameters. It should not be invoked from outside the GraphicsApplet class, so you should omit the public keyword in the method declaration. Here is the declaration along with its first two lines of code.

    void enableSliders() {
	String method = getMethod();
	stringControl.setEnabled(method.endsWith("String"));
    }
The JComponent setEnabled() method has a boolean parameter. If the parameter has value true then the receiving component is enabled; otherwise it is disabled. Thus the second line of code enables the string control when the drawing method name ends in "String", and disables it when the name does not end in "String".

Following this example, you should add a line of code for each of the other drawing parameter controls. If you do not remember which parameters are needed for the various drawing methods, check the demonstration applet at the end of this step. By selecting the various drawing methods, you can see which of the controls should be enabled. You can also reread the "Handling New Drawing Methods" section of Step 7.

The enableSlider() method should be invoked in two places.

  • Near the end of the createControlPanel() helper method, just before the return statement.
  • At the beginning of the itemStateChanged() method, which is in the listener that is created near the end of the createMethodControl() method. After you have made these changes, recompile your classes and run the applet with appletviewer. Your applet should look like the following demonstration applet.

    Demonstration Applet