Write an application that will test the user’s knowledge of road signs. Your application should display a random sign image and ask the user to select the sign name from a JComboBox. This application should look like Fig. 16.43. [Hint: The application is similar to the Flag Quiz application.] You can find the road sign images in C:\Examples\Tutorial16\Exercises\RoadSignTest\images.
a) Copying the template to your working directory. Copy the C:Examples Tutorial16ExercisesRoadSignTest directory to your C:SimplyJava directory.
b) Opening the template file. Open the RoadSignTest.java file in your text editor.
c) Declaring an array to contain the user’s options. On lines 29–33, create String array signs (use line 29 for a comment and lines 30–33 to declare and initialize the array). Have the array contain the values "Do Not Enter", "Narrow Bridge", "No Bicycles", "No Left Turn", "No Pedestrians", "No U-turn", "Road Narrows", "Stop", "Stop Sign Ahead", "Traffic Signals Ahead", "Winding Road Ahead" and "Yield".
d) Declaring an array to store which signs are used. On lines 35–36, create a boolean array named signsUsed (use line 35 for a comment and line 36 to create the array). Specify the array to be of size 12.
e) Sorting array signs. On line 73, sort array signs alphabetically.
f) Customizing signJComboBox. Modify line 76 so that signJComboBox
```
1 // RoadSignTest.java
2 // Tests the user on their knowledge of road signs. The user must
3 // try to match five signs to their meanings.
4 import java.util.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8 import javax.swing.border.*;
9
10 public class RoadSignTest extends JFrame
11 {
12 // JPanel and JLabel for displaying a sign image
13 private JPanel signIconJPanel;
14 private JLabel signIconJLabel;
15
16 // JLabel and JComboBox for choosing a sign
17 private JLabel chooseJLabel;
18 private JComboBox signJComboBox;
19
20 // JTextField for providing feedback to the user
21 private JTextField feedbackJTextField;
22
23 // JButton to submit the users answer and display the result
24 private JButton submitJButton;
25
26 // JButton to display the next sign image
27 private JButton nextSignJButton;
28
29 // String array to hold signs
30 private String[] signs = { "Do Not Enter", "Narrow Bridge",
31 "No Bicycles", "No Left Turn", "No Pedestrians", "No U-turn",
32 "Road Narrows", "Stop", "Stop Sign Ahead",
33 "Traffic Signals Ahead", "Winding Road Ahead", "Yield" };
34
35 142–147, define a do…while statement that generates
36 h) Saving the application. Save your modified source code file.
37
38 private int currentIndex; // integer holds the current array index
39 private int count = 1; // integer counts number of signs displayed
40
41 // no-argument constructor
42 public RoadSignTest()
43 {
44 createUserInterface();
45 }
46
47 // create and position GUI components; register event handlers
48 private void createUserInterface()
49 {
50 // get content pane for attaching GUI components
51 Container contentPane = getContentPane();
52
53 // enable explicit positioning of GUI components
54 contentPane.setLayout( null );
55
56 // set up signIconJPanel
57 signIconJPanel = new JPanel();
58 signIconJPanel.setBounds( 16, 8, 100, 94 );
59 signIconJPanel.setBorder( new TitledBorder( "Sign" ) );
60 contentPane.add( signIconJPanel );
61
62 // set up signIconJLabel
63 signIconJLabel = new JLabel();
64 signIconJLabel.setHorizontalAlignment( JLabel.CENTER );
65 signIconJPanel.add( signIconJLabel );
66
67 // set up chooseJLabel
68 chooseJLabel = new JLabel();
69 chooseJLabel.setBounds( 136, 8, 88, 21 );
70 chooseJLabel.setText( "Select sign:" );
71 contentPane.add( chooseJLabel );
72
73 Arrays.sort( signs ); // sort the array
74
75 // set up signJComboBox
76 signJComboBox = new JComboBox( signs );
77 signJComboBox.setBounds( 136, 32, 144, 21 );
78 signJComboBox.setMaximumRowCount( 4 );
79 contentPane.add( signJComboBox );
80
81 displaySign(); // display the first sign
82
83 // set up feedbackJTextField
84 feedbackJTextField = new JTextField();
85 feedbackJTextField.setBounds( 136, 64, 144, 36 );
86 feedbackJTextField.setHorizontalAlignment( JTextField.CENTER );
87 feedbackJTextField.setEditable( false );
88 contentPane.add( feedbackJTextField );
89
90 // set up submitJButton
91 submitJButton = new JButton();
92 submitJButton.setBounds( 296, 8, 88, 32 );
93 submitJButton.setText( "Submit" );
94 contentPane.add( submitJButton );
95 submitJButton.addActionListener(
96
98 {
99 // event handler called when submitJButton is pressed
100 public void actionPerformed( ActionEvent event )
101 {
102 submitJButtonActionPerformed( event );
103 }
104
105 } // end anonymous inner class
106
107 ); // end call to addActionListener
108
109 // set up nextSignJButton
110 nextSignJButton = new JButton();
111 nextSignJButton.setBounds( 296, 48, 88, 32 );
112 nextSignJButton.setText( "Next Sign" );
113 nextSignJButton.setEnabled( false );
114 contentPane.add( nextSignJButton );
115 nextSignJButton.addActionListener(
116
117 new ActionListener() // anonymous inner class
118 {
119 // event handler called when nextSignJButton is pressed
120 public void actionPerformed( ActionEvent event )
121 {
122 nextSignJButtonActionPerformed( event );
123 }
124
125 } // end anonymous inner class
126
127 ); // end call to addActionListener
128
129 // set properties of application's window
130 setTitle( "Road Sign Test" ); // set title bar string
131 setSize( 400, 140 ); // set window size
132 setVisible( true ); // display window
133
134 } // end method createUserInterface
135
136 // return an unused random number
137 private int getUniqueRandomNumber()
138 {
139 Random generator = new Random();
140 int randomNumber = 0;97
141
142 // generate random numbers until unused sign is found
143 do
144 {
145 randomNumber = ( int )generator.nextInt( 12 );
146 }
147 while ( signsUsed[ randomNumber ] == true );
148
149 signsUsed[ randomNumber ] = true;
150 return randomNumber;
151
152 } // end method getUniqueRandomNumber
153
154 // choose a sign and display it in the JLabel
155 private void displaySign()
156 {
157 currentIndex = getUniqueRandomNumber(); // get an unused sign
158
159 // create the path for that sign
160 String signPath = "images/sign" + currentIndex + ".png";
161
162 // set the label to display the sign
163 signIconJLabel.setIcon( new ImageIcon( signPath ) );
164
165 } // end method displaySign
166
167 // check the answer and update the quiz
168 private void submitJButtonActionPerformed( ActionEvent event )
169 {
170 // determine whether the answer was correct
171 if ( signJComboBox.getSelectedIndex() == currentIndex )
172 {
173 feedbackJTextField.setText( "Correct!" );
174 }
175 else
176 {
177 feedbackJTextField.setText( "Sorry, incorrect." );
178 }
179
180 // inform the user if the quiz is over
181 if ( count == 5 )
182 {
183 nextSignJButton.setEnabled( false );
184 submitJButton.setEnabled( false );
185 signJComboBox.setEnabled( false );
186 }
187 else
188 {
189 submitJButton.setEnabled( false );
190 nextSignJButton.setEnabled( true );
191 }
192
193 } // end method submitJButtonActionPerformed
194
195 // display next sign in the quiz
196 private void nextSignJButtonActionPerformed( ActionEvent event )
197 {
198 displaySign(); // display the next sign
199 count++; // increment the counter
200
201 // reset the GUI components to the initial states
202 feedbackJTextField.setText( "" );
203 signJComboBox.setSelectedIndex( 0 );
204 submitJButton.setEnabled( true );
205 nextSignJButton.setEnabled( false );
206
207 } // end method nextSignJButtonActionPerformed
208
209 // main method
210 public static void main( String[] args )
211 {
212 RoadSignTest application = new RoadSignTest();
213 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
214
215 } // end method main
216
217 } // end class RoadSignTest
```
You might also like to view...
A type of break that is used to create a new section that can be formatted differently from the rest of
the document. A) Continuous B) Page C) Column
In SharePoint, members of a ________ site gain reputation points through participation, starting discussions, replying to and liking posts, and by specifying best replies
A) team B) project C) blog D) community