Extend the Painter application to enable a user to change the size and color of the circles drawn (Fig. 21.34). This application will use an ArrayList to keep track of the data for all previously drawn circles (as in the Screen Saver application). Each time the paintComponent method is called, draw all previously drawn circles.
a) Copying the template to your working directory. Copy the C:Examples Tutorial21ExercisesAdvancedPainter directory to your C:SimplyJava direc- tory.
b) Opening the template file. Open the PainterJPanel.java file in your text editor.
c) Coding the mouseDragged event handler. Inside the mouseDragged event handler (lines 30–32), add code to call the painterJPanelMouseDragged method with the argument event. You will declare this method in the next step.
d) Declaring the painterJPanelMouseDragged method. After the paintComponent method, add the method header for painterJPanelMouseDragged. This method should be declared private, return void and take a MouseEvent object as its param- eter. Inside the painterJPanelMouseDragged method, add an if…else statement to test if the right mouse button was pressed. If the right mouse button was pressed,
create a new Circle object and assign it to instance variable newCircle. The Circle
constructor should take the diameter of
```
1 // AdvancedPainter.java
2 // This application allows the user to draw with four different
3 // colors and three different sized brushes.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import javax.swing.border.*;
8
9 public class AdvancedPainter extends JFrame
10 {
11 // PainterJPanel for drawing circles
12 private PainterJPanel myPainterJPanel;
13
14 // JPanel and ButtonGroup for color JRadioButtons
15 private JPanel colorJPanel;
16 private ButtonGroup colorButtonGroup;
17
18 // JRadioButtons for color of circle
19 private JRadioButton redJRadioButton;
20 private JRadioButton blueJRadioButton;
21 private JRadioButton greenJRadioButton;
22 private JRadioButton blackJRadioButton;
23
24 // JPanel and ButtonGroup for size JRadioButtons
25 private JPanel sizeJPanel;
26 private ButtonGroup sizeButtonGroup;
27
28 // JRadioButtons for size of circle
29 private JRadioButton smallJRadioButton;
30 private JRadioButton mediumJRadioButton;
31 private JRadioButton largeJRadioButton;
32
33 // size constants for diameter of circle
34 final int SMALL = 4;
35 final int MEDIUM = 8;
36 final int LARGE = 10;
37
38 // no-argument constructor
39 public AdvancedPainter()
40 {
41 createUserInterface();
42 }
43
44 // create and position GUI components; register event handlers
45 public void createUserInterface()
46 {
47 // get content pane for attaching GUI components
48 Container contentPane = getContentPane();
49
50 // enable explicit positioning of GUI components
51 contentPane.setLayout( null );
52
53 // set up myPainterJPanel to paint small, black circles
54 myPainterJPanel = new PainterJPanel( Color.BLACK, SMALL );
55 myPainterJPanel.setBounds( 112, 8, 336, 272 );
56 myPainterJPanel.setBackground( Color.WHITE );
57 contentPane.add( myPainterJPanel );
58
59 // set up colorJPanel
60 colorJPanel = new JPanel();
61 colorJPanel.setBounds( 8, 8, 96, 152 );
62 colorJPanel.setBorder( new TitledBorder( "Color" ) );
63 colorJPanel.setOpaque( false );
64 colorJPanel.setLayout( null );
65 contentPane.add( colorJPanel );
66
67 // set up colorButtonGroup
68 colorButtonGroup = new ButtonGroup();
69
70 // set up redJRadioButton
71 redJRadioButton = new JRadioButton();
72 redJRadioButton.setBounds( 16, 16, 56, 24 );
73 redJRadioButton.setText( "Red" );
74 colorButtonGroup.add( redJRadioButton );
75 colorJPanel.add( redJRadioButton );
76 redJRadioButton.addActionListener(
77
78 new ActionListener() // anonymous inner class
79 {
80 // event handler called when redJRadioButton is selected
81 public void actionPerformed( ActionEvent event )
82 {
83 redJRadioButtonActionPerformed( event );
84 }
85
86 } // end anonymous inner class
87
88 ); // end call to addActionListener
89
90 // set up blueJRadioButton
91 blueJRadioButton = new JRadioButton();
92 blueJRadioButton.setBounds( 16, 48, 56, 24 );
93 blueJRadioButton.setText( "Blue" );
94 colorButtonGroup.add( blueJRadioButton );
95 colorJPanel.add( blueJRadioButton );
96 blueJRadioButton.addActionListener(
97
98 new ActionListener() // anonymous inner class
99 {
100 // event handler called when blueJRadioButton is selected
101 public void actionPerformed( ActionEvent event )
102 {
103 blueJRadioButtonActionPerformed( event );
104 }
105
106 } // end anonymous inner class
107
108 ); // end call to addActionListener
109
110 // set up greenJRadioButton
111 greenJRadioButton = new JRadioButton();
112 greenJRadioButton.setBounds( 16, 80, 62, 24 );
113 greenJRadioButton.setText( "Green" );
114 colorButtonGroup.add( greenJRadioButton );
115 colorJPanel.add( greenJRadioButton );
116 greenJRadioButton.addActionListener(
117
118 new ActionListener() // anonymous inner class
119 {
120 // event handler called when greenJRadioButton is
121 // selected
122 public void actionPerformed( ActionEvent event )
123 {
124 greenJRadioButtonActionPerformed( event );
125 }
126
127 } // end anonymous inner class
128
129 ); // end call to addActionListener
130
131 // set up blackJRadioButton
132 blackJRadioButton = new JRadioButton();
133 blackJRadioButton.setBounds( 16, 112, 62, 24 );
134 blackJRadioButton.setText( "Black" );
135 blackJRadioButton.setSelected( true );
136 colorButtonGroup.add( blackJRadioButton );
137 colorJPanel.add( blackJRadioButton );
138 blackJRadioButton.addActionListener(
139
140 new ActionListener() // anonymous inner class
141 {
142 // event handler called when blackJRadioButton is
143 // selected
144 public void actionPerformed( ActionEvent event )
145 {
146 blackJRadioButtonActionPerformed( event );
147 }
148
149 } // end anonymous inner class
150
151 ); // end call to addActionListener
152
153 // set up sizeJPanel
154 sizeJPanel = new JPanel();
155 sizeJPanel.setBounds( 8, 168, 96, 112 );
156 sizeJPanel.setBorder( new TitledBorder( "Size" ) );
157 sizeJPanel.setOpaque( false );
158 sizeJPanel.setLayout( null );
159 contentPane.add( sizeJPanel );
160
161 // set up sizeButtonGroup
162 sizeButtonGroup = new ButtonGroup();
163
164 // set up smallJRadioButton
165 smallJRadioButton = new JRadioButton();
166 smallJRadioButton.setBounds( 16, 16, 64, 24 );
167 smallJRadioButton.setText( "Small" );
168 smallJRadioButton.setSelected( true );
169 sizeButtonGroup.add( smallJRadioButton );
170 sizeJPanel.add( smallJRadioButton );
171 smallJRadioButton.addActionListener(
172
173 new ActionListener() // anonymous inner class
174 {
175 // event handler called when smallJRadioButton is
176 // selected
177 public void actionPerformed( ActionEvent event )
178 {
179 smallJRadioButtonActionPerformed( event );
180 }
181
182 } // end anonymous inner class
183
184 ); // end call to addActionListener
185
186 // set up mediumJRadioButton
187 mediumJRadioButton = new JRadioButton();
188 mediumJRadioButton.setBounds( 16, 48, 70, 24 );
189 mediumJRadioButton.setText( "Medium" );
190 sizeButtonGroup.add( mediumJRadioButton );
191 sizeJPanel.add( mediumJRadioButton );
192 mediumJRadioButton.addActionListener(
193
194 new ActionListener() // anonymous inner class
195 {
196 // event handler called when mediumJRadioButton is
197 // selected
198 public void actionPerformed( ActionEvent event )
199 {
200 mediumJRadioButtonActionPerformed( event );
201 }
202
203 } // end anonymous inner class
204
205 ); // end call to addActionListener
206
207 // set up largeJRadioButton
208 largeJRadioButton = new JRadioButton();
209 largeJRadioButton.setBounds( 16, 80, 64, 24 );
210 largeJRadioButton.setText( "Large" );
211 sizeButtonGroup.add( largeJRadioButton );
212 sizeJPanel.add( largeJRadioButton );
213 largeJRadioButton.addActionListener(
214
215 new ActionListener() // anonymous inner class
216 {
217 // event handler called when largeJRadioButton is
218 // selected
219 public void actionPerformed( ActionEvent event )
220 {
221 largeJRadioButtonActionPerformed( event );
222 }
223
224 } // end anonymous inner class
225
226 ); // end call to addActionListener
227
228 // set up properties of application’s window
229 setTitle( "Advanced Painter" ); // set title bar text
230 setSize( 462, 312 ); // set window size
231 setVisible( true ); // display window
232
233 } // end method createUserInterface
234
235 // set circle color to red
236 private void redJRadioButtonActionPerformed( ActionEvent event )
237 {
238 myPainterJPanel.setColor( Color.RED );
239
240 } // end method redJRadioButtonActionPerformed
241
242 // set circle color to blue
243 private void blueJRadioButtonActionPerformed( ActionEvent event )
244 {
245 myPainterJPanel.setColor( Color.BLUE );
246
247 } // end method blueJRadioButtonActionPerformed
248
249 // set circle color to green
250 private void greenJRadioButtonActionPerformed( ActionEvent event )
251 {
252 myPainterJPanel.setColor( Color.GREEN );
253
254 } //
You might also like to view...
In a function, the data type of the expression in the Return statement must agree with the data type specified in the ____ section of the header.
A. Type As dataType B. Return As dataType C. As dataType (Return) D. As dataType
Objects can be removed from a navigation form
Indicate whether the statement is true or false
A ____ is an audio output device that converts text to speech.?
A. ?sampler B. ?voice synthesizer C. ?headset D. ?multifunction device
A _______ is the general term for a collection of key-value pairs.
a) book b) glossary c) dictionary d) lexicon