In this lab, you will build a simple Java GUI drawing tool, such as the one shown on the right. This tool includes a Processing sketch as a drawing panel (based on your scribbling tool from lab exercise 4a.3) and a drawing control panel with a label, text field and two buttons. The Java Foundation Classes (JFC) provide a wide variety of classes that you will use in this lab. In addition to the text and lecture notes, here are two additional information sources for these classes that you might find useful:
You will develop the application shown here incrementally. Because you ’ve already
encapsulated existing Processing sketches and run them in a Java GUI frame, you’ll start
with that task. As usual, you should create a new package, |
Encapsulate your manual scribbling tool from lab exercise 4a.3 as a panel named DrawingPanel
in a simple Java GUI frame named DrawingController
. You don’t need to include a
GUI control panel in this application.
Make sure that this GUI application runs properly.
The sample application shown above includes a text field to specify the stroke weight for the drawing panel. The user enters a weight value, hits enter, and then all new lines are drawn with the modified stroke weight.
Add a text field to the GUI frame that allows the user to specify the stroke weight. Do this as follows:
JTextField
object to hold the stroke weight value;DrawingPanel
:
DrawingController
constructor method:
JTextField
named weightField
with an initial value of “1” and a width of 3 characters (using new
JTextField("1", 3)
);addActionListener(this)
);add(weightField, BorderLayout.SOUTH)
)
- Note that you also need to modify the add for DrawingPanel
so that it places that
panel on the BorderLayout.NORTH
;DrawingController
class definition:
ActionListener
interface
(using implements ActionListener
);actionPerformed()
to implement this algorithm:
Set weightValue = the integer value of the text inweightField
.
AskDrawingPanel
to set its weight value to weightValue.
Make sure that this GUI application runs properly.
The sample application shown above includes a control panel with which the user configures the behavior of the drawing tool.
Add a button to the interface to tells the drawing panel to use a random color. Do this as follows:
JButton
object;DrawingPanel
:
DrawingController
constructor method:
JButton
with the label
“random”;JPanel
to hold your control panel as follows:
JPanel
object named controlPanel
;FlowLayout
(using controlPanel.setLayout(new
FlowLayout())
);controlPanel.add(myWeightField)
)
- Note that you also need to modify the add for weightField
so that you add it to
the control panel as well;add(controlPanel)
);actionPerformed()
method:
Receive the current GUI action event from the calling program.Note that you can check the value of the action command using
Set weightValue = the integer value of the text inmyWeightField
.
Ask theDrawingPanel
object to set its weight value to weightValue.
If the action event indicates that the random color button was pressed
Tell theDrawingPanel
object to set its color to a random color.
eventParameter.getActionCommand().equals("random")
where “random” is the label written on the button.Make sure that this GUI application runs properly.
There are many things that you could do to enhance this program.
Add an additional button that sets the color to white (the same as the sketch background) so that you can “rub out” mistakes.
Save this version of your program so that you can submit it.
Submit all the code and supporting files for the exercises in this lab.