public void setAngle(int ang) {
angle = ang;
}
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.
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.