Step 4


Introduction

In this step, you will add behavior to the two controls in the applet control panel. You will first need to add two short methods to the FractalPanel class. The rest of the modifications will be in the FractalApplet class, where you will add a utility method for converting combo box selected values to integers and add listeners to the combo boxes.

Setting Up Lines of Communication

The controls for the applet will ultimately alter the values of the instance variables in the FractalPanel class. Right now, there is no way that the instance variables can be accessed from the FractalApplet class. To provide access, you need to add two setter methods, setAngle() and setLength(), to the FractalPanel class. Each setter method has a single integer parameter and it just assigns it to the appropriate variable. For example, the code for the setAngle() method is
    public void setAngle(int ang) {
	angle = ang;
    }

Adding a getIntValue() Utility Method

The values in the combo box controls are strings, but we need integers for the setAngle() and setLength() parameters. Keep in mind that different kinds of data in a computer are coded in different ways. Even though the string "10" looks like a number to us, in the computer it uses a string coding scheme. This is different from an integer coding scheme.

It will simplify the coding a bit if we set up a method that handles the conversion between the coding schemes. That is the motivation for the getIntValue() method. Its code is

    int getIntValue(JComboBox cb) {
	String str = (String)(cb.getSelectedItem());
	return Integer.parseInt(str);
    }
This method just obtains the selected item from its combo box parameter. The selected item is a string, which is sent in a parseInt() message to the Integer class. The parseInt() method converts a string parameter to an int, which is returned by the method. You do not need to understand the details of this code.

Adding Listeners to the Controls

Each combo box should have its own event listener, created in the applet init method. Here is example code for one of the controls:
    angleControl.addItemListener(new ItemListener() {
	public void itemStateChanged(ItemEvent e) {
	    fractalPanel.setAngle(getIntValue(angleControl));
	    fractalPanel.repaint();
	}
    });
This code should be added at the end of the set up code for the combo box.

The portion between the parentheses of the addItemListener() method is an anonymous constructor. It creates an object of a new unnamed class (which explains the term "anonymous") that implements the ItemListener interface. It defines the itemStateChanged() function to set the drawing angle in the fractal canvas and repaint it. An itemStateChanged message is sent to all of the combo box listeners whenever the user makes a selection in the combo box.

If you recompiled the FractalApplet class at this point, you would get an error message. Whenever a local variable is mentioned in an anonymous constructor, the Java language requires that the variable always refers to the same object. You indicate this by adding the keyword final in front of a variable definition. You will need to do this for the fractalPanel variable and both of the combo box variables.

Testing

When you have implemented the event listeners, recompile both classes and rerun your applet. Try changing the selections in each of the combo boxes. The "Angle" combo box should change the orientation of the drawing and the "Length" combo box should change the length of the lines. You should get the same behavior as the following demonstration applet.

Demonstration Applet