Step 4


Overview

In this step, you will add the mechanism for updating the draw panel when the controls are operated by the user. In the DrawPanel class, this involves adding some instance variables, an updateDrawing() method, a paintComponent() method, and a updateDrawing() message in the constructor. In the GraphicsApplet class, you will add listeners to the two controls and two getter methods, getMethod() and getString(). The listeners send updateDrawing messages to the draw panel.

Changes in the DrawPanel Class

You should first define four instance variables described in the table below.
Type Name Initialization
String method "drawString"
String string "Hit return after change."
int x 100
int y 100

Next, you should add a method updateDrawing() to the class. This method has a void return type and no parameters. It should send a getMethod() message to the applet variable and assign its return value to the method variable. It should also send a getString() message to the applet variable and assign its return value to the string variable. Finally, it should send a repaint() message to the outerPanel variable. When the outer panel is repainted, the draw panel that it contains is also repainted.

The getMethod() and getString() methods have not yet been defined in the GraphicsApplet class. They will be described in the next section. All you need to know at this time is that they do not have any parameters.

For repainting to do any good, you will also need to write a paintComponent() method. It should start out like a standard paintComponent() method (don't forget the super.paintComponent(); message). Then you add the following if statement.

    if (method.equals("drawString")) {
	g.drawString(string, x, y);
    }
This if statement is redundant at this time, since the only possible value for string is "drawString". When more options are added to the method control, it will make more sense.

Finally, you need to add an updateDrawing() message at the end of the DrawPanel constructor code. This insures that the draw panel starts off correctly.

If you compile the DrawPanel class at this time, you should get two error messages about the getMethod() and getString() messages. This is to be expected since these methods have not yet been implemented in the GraphicsApplet class.

Changes in the GraphicsApplet Class

There are two kinds of changes in the GraphicsApplet class: adding the getMethod() and getString() getter methods and adding listeners in the createMethodControl() and createStringControl() helper methods.

The two new methods both return strings and have no parameters. Each has a single return statement. The statement in the getMethod() method is

    return (String)methodControl.getSelectedItem();
Items in a JComboBox can be any type of object, but we will only use strings as items. The (String) tells the compiler to treat the selected item from the method control as a string.

The statement in the getString() method is

    return stringControl.getText();
This just returns the text in the JTextField control.

For the listeners, you need to write code in the createMethodControl() and createStringControl() methods. In the first, you create a new ItemListener and add it to the control. In the second, you create a new ActionListener and add it to the control. In the ItemListener, you define an itemStateChanged() method. In the ActionListener, you define an actionPerformed() method. There is only one statement in each of these methods: sending an updateDrawing() message to the drawPanel variable.

You have dealt with both kinds of listeners before. For examples, see Programming Assignment 1 (action listeners) or Programming Assignment 2 (item listeners).

After adding the geter methods and listeners, you should be able to compile both classes without errors. If you are successful, run the applet. There should be some text drawn in the draw panel. You should be able to change it using the string control. A JTextField does not generate an action event until the user hits the "Enter" key, so you will have to do that to get changes to appear in the draw panel.

Your applet should look and behave like the following demonstration applet.

Demonstration Applet