Write an application that allows users to design a com pany logo. It should be able to draw lines as well as both filled and empty rectangles and ovals with a simple coordinate input interface. Your GUI should look like Fig. 27.35.



a) Copying the template to your working directory. Copy the C:Examples Tutorial27ExercisesLogoDesigner directory to your C:SimplyJava directory.

b) Opening the template file. Open the MyRectangle.java and MyOval.java files in your text editor.

c) Modifying the MyRectangle and MyOval classes. This will add the ability to draw both filled and outlined shapes to your shape hierarchy.

d) Opening the template file. Open the DrawJPanel.java file in your text editor.

e) Adding the addShape method. At line 31, add a comment indicating that the method will add the shape to shapeArray and then repaint. On line 32, add the method header for the addShape method. This method does not return a value and takes an argument of type MyShape named shape. Add shape to shapeArrayList by calling the add method on shapeArrayList and passing it shape. Then, call the repaint

method so that the newly added shape will be displayed. Be sure to end the method with a right brace on line 37.<


```
1 // LogoDesigner.java
2 // Application that allows the user to design a logo for a company.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.event.*;
7 import javax.swing.border.TitledBorder;
8
9 public class LogoDesigner extends JFrame
10 {
11 // DrawJPanel for drawing shapes to
12 private DrawJPanel drawingJPanel;
13
14 // JPanel for holding JRadioButtons
15 private JPanel shapeJPanel;
16
17 // JRadioButtons to choose a shape
18 private JRadioButton rectangleJRadioButton;
19 private JRadioButton ellipseJRadioButton;
20 private JRadioButton lineJRadioButton;
21 private JRadioButton filledRectangleJRadioButton;
22 private JRadioButton filledEllipseJRadioButton;
23
24 // ButtonGroup for grouping JRadioButtons
25 private ButtonGroup shapeButtonGroup;
26
27 // JPanel for holding settings input controls
28 private JPanel settingsJPanel;
29
30 // JLabel and JTextField for x or x1
31 private JLabel xJLabel;
32 private JTextField xJTextField;
33
34 // JLabel and JTextField for y or y1
35 private JLabel yJLabel;
36 private JTextField yJTextField;
37
38 // JLabel and JTextField for width or x2
39 private JLabel widthJLabel;
40 private JTextField widthJTextField;
41
42 // JLabel and JTextField for height or y2
43 private JLabel heightJLabel;
44 private JTextField heightJTextField;
45
46 // JPanel for holding colorJComboBox
47 private JPanel colorJPanel;
48
49 // JComboBox to choose the color
50 private JComboBox colorJComboBox;
51
52 // JButton to add shape to drawing area
53 private JButton addJButton;
54
55 // JButton to clear the drawing area
56 private JButton clearJButton;
57
58 // String array for storing the Strings in the JComboBox
59 private final String[] colorNames = { "Black", "Blue", "Green",
60 "Magenta", "Orange", "Red", "Yellow" };
61
62 // Color array for storing the Color objects that
63 // correspond to the colorNames array
64 private final Color[] colors = { Color.BLACK, Color.BLUE,
65 Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.RED,
66 Color.YELLOW };
67
68 // no-argument constructor
69 public LogoDesigner()
70 {
71 createUserInterface();
72 }
73
74 // set up the GUI components
75 public void createUserInterface()
76 {
77 // get content pane for attaching GUI components
78 Container contentPane = getContentPane();
79
80 // enable explicit positioning of GUI components
81 contentPane.setLayout( null );
82
83 // set up drawingJPanel
84 drawingJPanel = new DrawJPanel();
85 drawingJPanel.setBackground( Color.WHITE );
86 drawingJPanel.setBounds( 15, 15, 460, 175 );
87 drawingJPanel.setOpaque( true );
88 contentPane.add( drawingJPanel );
89
90 // set up shapeJPanel
91 shapeJPanel = new JPanel();
92 shapeJPanel.setBounds( 10, 200, 355, 130 );
93 shapeJPanel.setBorder( new TitledBorder( "Shape" ) );
94 shapeJPanel.setLayout( null );
95 contentPane.add( shapeJPanel );
96
97 // set up rectangleJRadioButton
98 rectangleJRadioButton = new JRadioButton();
99 rectangleJRadioButton.setText( "Rectangle" );
100 rectangleJRadioButton.setBounds( 20, 25, 90, 20 );
101 shapeJPanel.add( rectangleJRadioButton );
102
103 // set up ellipseJRadioButton
104 ellipseJRadioButton = new JRadioButton();
105 ellipseJRadioButton.setText( "Ellipse" );
106 ellipseJRadioButton.setBounds( 20, 60, 100, 20 );
107 shapeJPanel.add( ellipseJRadioButton );
108
109 // set up lineJRadioButton
110 lineJRadioButton = new JRadioButton();
111 lineJRadioButton.setText( "Line" );
112 lineJRadioButton.setBounds( 20, 95, 70, 20 );
113 shapeJPanel.add( lineJRadioButton );
114 lineJRadioButton.addChangeListener(
115
116 new ChangeListener() // anonymous inner class
117 {
118 // event handler called when the
119 // state of lineJRadioButton is changed
120 public void stateChanged( ChangeEvent event )
121 {
122 lineJRadioButtonActionPerformed( event );
123 }
124
125 } // end anonymous inner class
126
127 ); // end call to addActionListener
128
129 // set up filledRectangleJRadioButton
130 filledRectangleJRadioButton = new JRadioButton()
131 filledRectangleJRadioButton.setText( "Filled Rectangle" );
132 filledRectangleJRadioButton.setBounds( 130, 25, 150, 20 );
133 shapeJPanel.add( filledRectangleJRadioButton );
134
135 // set up filledEllipseJRadioButton
136 filledEllipseJRadioButton = new JRadioButton();
137 filledEllipseJRadioButton.setText( "Filled Ellipse" );
138 filledEllipseJRadioButton.setBounds( 130, 60, 150, 20 );
139 shapeJPanel.add( filledEllipseJRadioButton );
140
141 // set up shapeButtonGroup
142 shapeButtonGroup = new ButtonGroup();
143 shapeButtonGroup.add( rectangleJRadioButton );
144 shapeButtonGroup.add( filledRectangleJRadioButton );
145 shapeButtonGroup.add( ellipseJRadioButton );
146 shapeButtonGroup.add( filledEllipseJRadioButton );
147 shapeButtonGroup.add( lineJRadioButton );
148
149 // set up settingsJPanel
150 settingsJPanel = new JPanel();
151 settingsJPanel.setBounds( 10, 340, 350, 60 );
152 settingsJPanel.setBorder( new TitledBorder( "Settings" ) );
153 settingsJPanel.setLayout( null );
154 contentPane.add( settingsJPanel );
155
156 // set up xJLabel
157 xJLabel = new JLabel();
158 xJLabel.setText( "X:" );
159 xJLabel.setBounds( 14, 25, 20, 20 );
160 xJLabel.setHorizontalAlignment( JTextField.RIGHT );
161 settingsJPanel.add( xJLabel );
162
163 // set up xJTextField
164 xJTextField = new JTextField();
165 xJTextField.setBounds( 35, 25, 45, 20 );
166 xJTextField.setHorizontalAlignment( JTextField.RIGHT );
167 settingsJPanel.add( xJTextField );
168
169 // set up yJLabel
170 yJLabel = new JLabel();
171 yJLabel.setText( "Y:" );
172 yJLabel.setBounds( 84, 25, 20, 20 );
173 yJLabel.setHorizontalAlignment( JTextField.RIGHT );
174 settingsJPanel.add( yJLabel );
175
176 // set up yJTextField
177 yJTextField = new JTextField();
178 yJTextField.setBounds( 105, 25, 45, 20 );
179 yJTextField.setHorizontalAlignment( JTextField.RIGHT );
180 settingsJPanel.add( yJTextField );
181
182 // set up widthJLabel
183 widthJLabel = new JLabel();
184 widthJLabel.setText( "Width:" );
185 widthJLabel.setBounds( 154, 25, 40, 20 );
186 widthJLabel.setHorizontalAlignment( JTextField.RIGHT );
187 settingsJPanel.add( widthJLabel );
188
189 // set up widthJTextField
190 widthJTextField = new JTextField();
191 widthJTextField.setBounds( 195, 25, 45, 20 );
192 widthJTextField.setHorizontalAlignment( JTextField.RIGHT );
193 settingsJPanel.add( widthJTextField );
194
195 // set up heightJLabel
196 heightJLabel = new JLabel();
197 heightJLabel.setText( "Height:" );
198 heightJLabel.setBounds( 249, 25, 40, 20 );
199 heightJLabel.setHorizontalAlignment( JTextField.RIGHT );
200 settingsJPanel.add( heightJLabel );
201
202 // set up heightJTextField
203 heightJTextField = new JTextField();
204 heightJTextField.setBounds( 290, 25, 45, 20 );
205 heightJTextField.setHorizontalAlignment( JTextField.RIGHT );
206 settingsJPanel.add( heightJTextField );
207
208 // set up colorJPanel
209 colorJPanel = new JPanel();
210 colorJPanel.setBounds( 370, 200, 110, 70 );
211 colorJPanel.setBorder( new TitledBorder( "Color" ) );
212 colorJPanel.setLayout( null );
213 contentPane.add( colorJPanel );
214
215 // set up colorJComboBox
216 colorJComboBox = new JComboBox( colorNames );
217 colorJComboBox.setBounds( 10, 25, 90, 25 );
218 colorJPanel.add( colorJComboBox );
219
220 // set up addJButton
221 addJButton = new JButton();
222 addJButton.setText( "Add" );
223 addJButton.setBounds( 400, 320, 75, 25 );
224 contentPane.add( addJButton );
225 addJButton.addActionListener(
226
227 new ActionListener() // anonymous inner class
228 {
229
230
231
232
233
234
235
236
237 );
238
239 //
240 clearJButton = new JButton();
241 clearJButton.setText( "Clear" );
242 clearJButton.setBounds( 400, 355, 75, 25 );
243 contentPane.add( clearJButton );
244 clearJButton.addActionListener(
245
246 new ActionListener() // anonymous inner class
247 {
248 // event handler called when clearJButton is pressed
249 public void actionPerformed( ActionEvent event )
250 {
251 clearJButtonActionPerformed( event );
252 }
253
254 } // end anonymous inner class
255
256 ); // end call to addActionListener
257
258 // set properties of application's window
259 setTitle( "Logo Designer" ); // set title bar string
260 setSize( 500, 435 ); // set window size
261 setVisible( true ); // display win

Computer Science & Information Technology

You might also like to view...

Look up how HTTP/1.1 address this problem and summarize what you found. Answer:

When a HTTP server sends the contents of a document to a client in a response body, it uses the Content-Length header line to specify the byte length of the body. For static documents, the byte length is provided by the file system. But for dynamically generated web pages, such as those generated by a CGI script, the length must be determined at runtime, on the fly.

Computer Science & Information Technology

Match the following terms to their meanings:

I. Landscape A. Type of page orientation that is used for ordinary text II. Section break B. Part of a document that is formatted differently than the rest III. Portrait of the document IV. Section C. Used to divide a document into multiple segments V. Next page D. A type of page orientation sometimes used for charts E. A type of section break

Computer Science & Information Technology

Some special formatting features are unavailable for use with paragraph text but are available for use in tables

Indicate whether the statement is true or false

Computer Science & Information Technology

What is digital grease?

What will be an ideal response?

Computer Science & Information Technology