Step 3
Introduction
In this step, you will add two combo box controls into the control panel of
the applet.
All of the code that you add should be in the init() method of the
FractalApplet class.
Installing Combo Box Controls
You need to add two JComboBox variables and add titled borders to each.
Since you have set up a BoxLayout layout manager for the control panel, the
combo boxes will be layed out in a vertical column in the order that you
add them.
The code for setting up the combo boxes should be entered just before the
control panel is added into the content pane of the applet.
Each of the combo box controls is set up as follows:
-
You define a variable for the combo box and initialize it with a new
JComboBox.
The JComboBox constructor does not have any parameters.
-
You add user choices for the combo box with its addItem() method.
A parameter specifies a value that is displayed when the user presses the
left mouse button over the combo box.
-
You specify the default selection with its setSelectedItem()
method.
A parameter specifies the value that is selected when the applet starts.
The parameter should match the initialized value of the corresponding
FractalPanel variable.
-
You put a titled border around the combo box with its setBorder
method.
In fact, you can do this with any of the Swing components.
You can get various kinds of borders from the BorderFactory class.
For a titled border, you send a createTitledBorder() message to
the BorderFactory class with the title as a parameter.
-
You add the combo box to the control panel by invoking the panel
add() method, specifying the combo box variable as a parameter.
For example, the following code sets up a combo box control for the angle
of the fractal, assuming that you used the name controlPanel for
the control panel variable.
JComboBox angleControl = new JComboBox();
angleControl.addItem("0");
angleControl.addItem("15");
angleControl.addItem("30");
angleControl.addItem("45");
angleControl.addItem("60");
angleControl.addItem("75");
angleControl.setSelectedItem("0");
angleControl.setBorder(BorderFactory.createTitledBorder("Angle"));
controlPanel.add(angleControl);
This combo box will display the choices 0, 15, 30, 45, 60, and 75, with 0
being the initially selected choice.
The title for the combo box will be "Angle".
You should also set up another combo box control for the length of the
fractal pattern.
This control should display the choices 75, 100, and 150, with 100 as the
initially selected choice.
Its title should be "Length".
When you have made these modifications, try recompiling the FractalApplet
class and display the applet again with appletviewer.
The controls should respond visually to mouse operations, but they will
have no effect on the drawing in the fractal panel.
Your applet should have the same appearence as the following demonstration
applet.
Demonstration Applet