Enhance the Painter application that you developed to include menus to allow the user to select the size and color of the painted ellipses and the color of the JFrame (Fig. 22.34). (The menus replace the JRadioButtons.) Also, add a multiline JTextArea that allows the user to type text to accompany the painting. The user should be able to use menus to select the font style and color of the text and the background color of the JTextArea.
a) Copying the template to your working directory. Copy the C:Examples Tutorial22ExercisesPainterEnhanced directory to your C:SimplyJava direc- tory.
b) Opening the template file. Open the Painter.java file in your text editor.
c) Creating paintJMenu. In line 75, add the code that will set up paintJMenu. To do this, create a new JMenu and assign it to paintJMenu. Next, set the text of paintJ- Menu to "Paint". Set the mnemonic for paintJMenu to the P key. You will need to use KeyEvent constant VK_P. The, add paintJMenu to painterJMenuBar.
d) Creating textJMenu. Before the code that sets up textColorJMenuItem, add the code that will set up textJMenu. To do this, create a new JMenu and assign it to text- JMenu. Next, set the text of textJMenu to "Text". Set the mnemonic for textJMenu to the T key. You will need to use KeyEvent constant VK_T. Then, add textJMenu to painterJMenuBar.
e) Allowing the user to select the paint color. In the paintColorJMenuItemAction- Performed method, dec
```
1 // Painter.java
2 // Application enables user to paint objects and name them.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import java.util.*;
7
8 public class Painter extends JFrame
9 {
10 // JMenuBar to hold paint and text options
11 private JMenuBar painterJMenuBar;
12
13 // JMenu to holds paint and text options
14 private JMenu paintJMenu;
15 private JMenu textJMenu;
16
17 // JMenuItems and JMenu allow user to change application colors
18 private JMenuItem paintColorJMenuItem;
19 private JMenu paintSizeJMenu;
20
21 // array of JRadioButtonMenuItems and ButtonGroup for different
22 // paint size options
23 private JRadioButtonMenuItem paintSizeItems[];
24 private ButtonGroup paintSizeButtonGroup;
25
26 // JMenuItems and JMenu allow user to change text properties
27 private JMenuItem textColorJMenuItem;
28 private JMenu formatJMenu;
29
30 // JMenus allow user to format text
31 private JMenu styleJMenu;
32 private JMenu sizeJMenu;
33
34 // JRadioButtonMenuItems, ButtonGroup and JCheckBoxMenuItems
35 // display style and size options
36 private JCheckBoxMenuItem styleItems[];
37 private JRadioButtonMenuItem sizeItems[];
38 private ButtonGroup sizeButtonGroup;
39
40 // JTextArea for user to type in and Font of the JTextArea
41 private JTextArea displayJTextArea;
42 private Font displayFont;
43
44 // PainterJPanel to allow user to paint with mouse
45 private PainterJPanel myPainterJPanel;
46
47 // String array to hold paint size options
48 private final String paintSizes[] = { "4", "6", "8", "10" };
49
50 // String array to hold font style options
51 private final String styles[] = { "Bold", "Italic" };
52
53 // String array to hold font size options
54 private final String fontSizes[] = { "12", "16", "20" };
55
56 // no-argument constructor
57 public Painter()
58 {
59 createUserInterface();
60 }
61
62 // create and position GUI components; register event handlers
63 private void createUserInterface()
64 {
65 // get content pane for attaching GUI components
66 Container contentPane = getContentPane();
67
68 // enable explicit positioning of GUI components
69 contentPane.setLayout( null );
70
71 // set up painterJMenuBar
72 painterJMenuBar = new JMenuBar();
73 setJMenuBar( painterJMenuBar );
74
75 // set up paintJMenu
76 paintJMenu = new JMenu();
77 paintJMenu.setText( "Paint" );
78 paintJMenu.setMnemonic( KeyEvent.VK_P );
79 painterJMenuBar.add( paintJMenu );
80
81 // set up paintColorJMenuItem
82 paintColorJMenuItem = new JMenuItem();
83 paintColorJMenuItem.setText( "Paint Color..." );
84 paintColorJMenuItem.setMnemonic( KeyEvent.VK_C );
85 paintJMenu.add( paintColorJMenuItem );
86 paintColorJMenuItem.addActionListener(
87
88 new ActionListener() // anonymous inner class
89 {
90 // event handler called when paintColorJMenuItem
91 // is selected
92 public void actionPerformed( ActionEvent event )
93 {
94 paintColorJMenuItemActionPerformed( event );
95 }
96
97 } // end anonymous inner class
98
99 ); // end call to addActionListener
100
101 // set up paintSizeJMenu
102 paintSizeJMenu = new JMenu();
103 paintSizeJMenu.setText( "Paint Size" );
104 paintSizeJMenu.setMnemonic( KeyEvent.VK_S );
105 paintJMenu.add( paintSizeJMenu );
106
107 // set up paintSizeItems
108 paintSizeItems = new JRadioButtonMenuItem[ paintSizes.length ];
109 paintSizeButtonGroup = new ButtonGroup();
110
111 for ( int count = 0; count < paintSizes.length; count ++ )
112 {
113 paintSizeItems[ count ] =
114 new JRadioButtonMenuItem( paintSizes[ count ] );
115 paintSizeJMenu.add( paintSizeItems[ count ] );
116 paintSizeButtonGroup.add( paintSizeItems[ count ] );
117 paintSizeItems[ count ].addActionListener(
118
119 new ActionListener() // anonymous inner class
120 {
121 // event handler called when a sizeItem is selected
122 public void actionPerformed( ActionEvent event )
123 {
124 sizeJMenuActionPerformed( event );
125 }
126
127 } // end anonymous inner class
128
129 ); // end call to addActionListener
130
131 } // end for
132
133 // set up textJMenu
134 textJMenu = new JMenu();
135 textJMenu.setText( "Text" );
136 textJMenu.setMnemonic( KeyEvent.VK_T );
137 painterJMenuBar.add( textJMenu );
138
139 // set up textColorJMenuItem
140 textColorJMenuItem = new JMenuItem();
141 textColorJMenuItem.setText( "Text Color..." );
142 textColorJMenuItem.setMnemonic( KeyEvent.VK_C );
143 textJMenu.add( textColorJMenuItem );
144 textColorJMenuItem.addActionListener(
145
146 new ActionListener() // anonymous inner class
147 {
148 // event handler called when textColorJMenuItem
149 // is selected
150 public void actionPerformed( ActionEvent event )
151 {
152 textColorJMenuItemActionPerformed( event );
153 }
154
155 } // end anonymous inner class
156
157 ); // end call to addActionListener
158
159 // set up formatJMenu
160 formatJMenu = new JMenu();
161 formatJMenu.setText( "Format" );
162 formatJMenu.setMnemonic( KeyEvent.VK_F );
163 textJMenu.add( formatJMenu );
164
165 // set up styleJMenu
166 styleJMenu = new JMenu();
167 styleJMenu.setText( "Style" );
168 styleJMenu.setMnemonic( KeyEvent.VK_S );
169 formatJMenu.add( styleJMenu );
170
171 // set up styleItems
172 styleItems = new JCheckBoxMenuItem[ styles.length ];
173
174 for ( int count = 0; count < styleItems.length; count++ )
175 {
176 styleItems[ count ] = new JCheckBoxMenuItem(
177 styles[ count ] );
178 styleJMenu.add( styleItems[ count ] );
179 styleItems[ count ].addItemListener(
180
181 new ItemListener() // anonymous inner class
182 {
183 // event handler called when a style item is selected
184 public void itemStateChanged( ItemEvent event )
185 {
186 styleItemsStateChanged( event );
187 }
188
189 } // end anonymous inner class
190
191 ); // end call to addItemListener
192
193 } // end for
194
195 // set up sizeJMenu
196 sizeJMenu = new JMenu();
197 sizeJMenu.setText( "Size" );
198 sizeJMenu.setMnemonic( KeyEvent.VK_Z );
199 formatJMenu.add( sizeJMenu );
200
201 // set up sizeItems
202 sizeItems = new JRadioButtonMenuItem[ fontSizes.length ];
203 sizeButtonGroup = new ButtonGroup();
204
205 for ( int count = 0; count < sizeItems.length; count++ )
206 {
207 sizeItems[ count ] = new JRadioButtonMenuItem(
208 fontSizes[ count ] );
209 sizeJMenu.add( sizeItems[ count ] );
210 sizeButtonGroup.add( sizeItems[ count ] );
211 sizeItems[ count ].addActionListener(
212
213 new ActionListener() // anonymous inner class
214 {
215 // event handler called when a sizeItem is selected
216 public void actionPerformed( ActionEvent event )
217 {
218 fontSizeItemsActionPerformed( event );
219 }
220
221 } // end anonymous inner class
222
223 ); // end call to addActionListener
224
225 } // end for
226
227 // set up displayJTextArea
228 displayJTextArea = new JTextArea();
229 displayJTextArea.setBounds( 16, 16, 300, 75 );
230 displayJTextArea.setLineWrap( true );
231 displayFont = displayJTextArea.getFont();
232 contentPane.add( displayJTextArea );
233
234 // set up myPainterJPanel
174 for ( int count = 0; count < styleItems.length; count++ )
175 {
176 styleItems[ count ] = new JCheckBoxMenuItem(
177 styles[ count ] );
178 styleJMenu.add( styleItems[ count ] );
179 styleItems[ count ].addItemListener(
180
181 new ItemListener() // anonymous inner class
182 {
183 // event handler called when a style item is selected
184 public void itemStateChanged( ItemEvent event )
185 {
186 styleItemsStateChanged( event );
187 }
188
189 } // end anonymous inner class
190
191 ); // end call to addItemListener
192
193 } // end for
194
195 // set up sizeJMenu
196 sizeJMenu = new JMenu();
197 sizeJMenu.setText( "Size" );
198 sizeJMenu.se
You might also like to view...
An administrator checks a server running a RAID 1 with ten drives and notices three drives have blinking amber lights. Which of the following is the FIRST action the administrator should take?
A. Reseat the drives one at a time B. Inform the users the server is down C. Shut down the server and replace the drives D. Call support and open a case
A table is a grid consisting of rows and charts.
Answer the following statement true (T) or false (F)