Modify the Security Panel application to use Java Speech API. The completed application is shown in Fig. 28.20.

a) Copying the template to your working directory. Copy the C:\Examples\ Tutorial28\Exercises\SecurityPanelEnhancement directory to your C:\Sim- plyJava directory.
b) Opening the template file. Open the SecurityPanel.java file in your text editor.
c) Importing Java Speech API packages. Import the javax.speech and the javax.speech.synthesis packages.
d) Declaring instance variables. Above the SecurityPanel constructor, Declare an instance variable of type Synthesizer, which is used to speak text.
e) Creating a Synthesizer object. Inside the SecurityPanel constructor, create a Synthesizer object, allocate the resource and get the synthesizer ready to speak.
f) Adding code to the enterJButtonActionPerformed method. Find the enterJBut- tonActionPerformed method, which immediately follows nineJButtonActionPer- formed. Add code to the enterJButtonActionPerformed method to use the speech synthesizer. If the user enters a valid access code, the application should welcome the user and state the type of employee that the access code represents. If the access code is invalid, then the application should state that an invalid code was provided and
that access is denied.
g) Releasing the resources allocated to the speech synthesizer. Find the frameWindow- Closing method, which immediately follows enterJButtonActionPerformed. Inside the frameWindowClosing method, add code to release the resources allocated to the speech synthesizer.
h) Saving the application. Save your modified source code file.
i) Opening the Command Prompt window and changing directories. Open the Command Prompt window by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:\SimplyJava\Security- PanelEnhancement.
j) Setting the CLASSPATH. Type SetClasspath.bat in the Command Prompt window to set the CLASSPATH environment variable. [Note: if your FreeTTS is installed in a directory other than C:\FreeTTS then replace C:\FreeTTS with your FreeTTS instal- lation directory in the SetClasspath.bat file.]
k) Compiling the application. Compile your application by typing javac Securi- tyPanel.java.
l) Running the completed application. When your application compiles correctly, run it by typing java SecurityPanel. Enter a correct access code, then click the # JBut- ton to verify that the application speaks the correct message. Try entering an incorrect access code. You should hear the application speak an error message notifying you that you did not enter a correct code.
m)Closing the application. Close your running application by clicking its close button.
n) Closing the Command Prompt window. Close the Command Prompt window by clicking its close button.


```
1 // SecurityPane
2 // Enable user to enter securit
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import java.text.DateFormat;
7 import java.util.*;
8 import javax.speech.*;
9 import javax.speech.synthesis.*;
10
11 public class SecurityPanel extends JFrame
12 {
13 // JLabel and JPasswordField for user to input security code
14 private JLabel securityCodeJLabel;
15 private JPasswordField securityCodeJPasswordField;
16
17 // JButtons to represent security keypad
18 private JButton oneJButton;
19 private JButton twoJButton;
20 private JButton threeJButton;
21 private JButton fourJButton;
22 private JButton fiveJButton;
23 private JButton sixJButton;
24 private JButton sevenJButton;
25 private JButton eightJButton;
26 private JButton nineJButton;
27 private JButton clearJButton;
28 private JButton zeroJButton;
29 private JButton enterJButton;
30
31 // JLabel, JTextArea and JScrollPane to display access log
32 private JLabel accessLogJLabel;
33 private JTextArea accessLogJTextArea;
34 private JScrollPane accessLogJScrollPane;
35
36 // Synthesizer to speak text
37 private Synthesizer speechSynthesizer;
38
39 // no-argument constructor
40 public SecurityPanel()
41 {
43 try
44 {
45 // create SynthesizerModeDesc for FreeTTS synthesizer
46 SynthesizerModeDesc descriptor = new SynthesizerModeDesc(
47 "Unlimited domain FreeTTS Speech Synthesizer " +
48 "from Sun Labs", null, Locale.US, Boolean.FALSE, null);
49
50 // create a Synthesizer
51 speechSynthesizer = Central.createSynthesizer( descriptor );
52
53 // Synthesizer created
54 if ( speechSynthesizer
55 {
56 // get synthesizer ready to speak
57 speechSynthesizer.allocate();
58 speechSynthesizer.resume();
59
60 // get synthesizer properties
61 SynthesizerProperties properties =
62 speechSynthesizer.getSynthesizerProperties();
63
64 // set up speaking rate
65 properties.setSpeakingRate( 100.0f );
66
67 } // end if
68 else
69 {
70 System.err.println( "Synthesizer creation failed.");
71 System.exit( 1 );
72 }
73
74 } // end try
75
76
77
78 myException.printStackTrace();
79 }
80
81 createUserInterface(); // set up GUI
82
83 } // end SecurityPanel constructor
84
85 } create and position GUI components; register event handlers
86 private void createUserInterface()
87 {
85 //
86 private void createUserInterface()
87 {
88 // get content pane for attaching GUI components
89 Container contentPane = getContentPane();
90
91 // enable explicit positioning of GUI components
92 contentPane.setLayout( null );
93
94 // set up securityCodeJLabel securityCodeJLabel
95 = new JLabel(); securityCodeJLabel.setBounds(
96 16, 16, 90, 21 ); securityCodeJLabel.setText(
97 "Security code:" ); contentPane.add(
98 securityCodeJLabel );
99
100 // set up securityCodeJPasswordField
101 securityCodeJPasswordField = new JPasswordField(
102 ); securityCodeJPasswordField.setBounds( 114, 16, 172, 26 );
103 securityCodeJPasswordField.setEditable( false );
104 contentPane.add( securityCodeJPasswordField );
105
106 // set up oneJButton
107 oneJButton = new JButton();
108 oneJButton.setBounds( 80, 64, 50, 50 );
109 oneJButton.setText( "1" );
110 contentPane.add( oneJButton );
111 oneJButton.addActionListener(
112
113 new ActionListener() // anonymous inner class
114 {
115 // event handler called when oneJButton is pressed
116 public void actionPerformed( ActionEvent event )
117 {
118 oneJButtonActionPerformed( event );
119 }
120
121 } // end anonymous inner class
122
123 ); // end call to addActionListener
124
125 // set up twoJButton
126 twoJButton = new JButton();
127 twoJButton.setBounds( 130, 64, 50, 50 );
128 twoJButton.setText( "2" );
129 contentPane.add( twoJButton );
130 twoJButton.addActionListener(
131
132 new ActionListener() // anonymous inner class
133 {
134 // event handler called when twoJButton is pressed
135 public void actionPerformed( ActionEvent event )
136 {
137 twoJButtonActionPerformed( event );
138 }
139
140 } // end anonymous inner class
141
142 ); // end call to addActionListener
143
144 // set up threeJButton
145 threeJButton = new JButton();
146 threeJButton.setBounds( 180, 64, 50, 50 );
147 threeJButton.setText( "3" );
148 contentPane.add( threeJButton );
149 threeJButton.addActionListener(
150
151 new ActionListener() // anonymous inner class
152 {
153 // event handler called when threeJButton is pressed
154 public void actionPerformed( ActionEvent event )
155 {
156 threeJButtonActionPerformed( event );
157 }
158
159 } // end anonymous inner class
160
161 ); // end call to addActionListener
162
163 // set up fourJButton
164 fourJButton = new JButton();
165 fourJButton.setBounds( 80, 114, 50, 50 );
166 fourJButton.setText( "4" );
167 contentPane.add( fourJButton );
168 fourJButton.addActionListener(
169
170 new ActionListener() // anonymous inner class
171 {
172 // event handler called when fourJButton is pressed
173 public void actionPerformed( ActionEvent event )
174 {
175 fourJButtonActionPerformed( event );
176 }
177
178 } // end anonymous inner class
179
180 ); // end call to addActionListener
181
182 // set up fiveJButton
183 fiveJButton = new JButton();
184 fiveJButton.setBounds( 130, 114, 50, 50 );
185 fiveJButton.setText( "5" );
186 contentPane.add( fiveJButton );
187 fiveJButton.addActionListener(
188
189 new ActionListener() // anonymous inner class
190 {
191 // event handler called when fiveJButton is pressed
192 public void actionPerformed( ActionEvent event )
193 {
194 fiveJButtonActionPerformed( event );
195 }
196
197 } // end anonymous inner class
198
199 ); // end call to addActionListener
200
201 // set up sixJButton
202 sixJButton = new JButton();
203 sixJButton.setBounds( 180, 114, 50, 50 );
204 sixJButton.setText( "6" );
205 contentPane.add( sixJButton );
206 sixJButton.addActionListener(
207
208 new ActionListener() // anonymous inner class
209 {
210 // event handler called when sixJButton is pressed
211 public void actionPerformed( ActionEvent event )
212 {
213 sixJButtonActionPerformed( event );
214 }
215
216 } // end anonymous inner class
217
218 ); // end call to addActionListener
219
220 // set up sevenJButton
221 sevenJButton = new JButton();
222 sevenJButton.setBounds( 80, 164, 50, 50 );
223 sevenJButton.setText( "7" );
224 contentPane.add( sevenJButton );
225 sevenJButton.addActionListener(
226
227 new ActionListener() // anonymous inner class
228 {
229 // event handler called when sevenJButton is pressed
230 public void actionPerformed( ActionEvent event )
231 {
232 sevenJButtonActionPerformed( event );
233 }
234
235 } // end anonymous inner class
236
237 ); // end call to addActionListener
238
239 // set up eightJButton
240 eightJButton = new JButton();
241 eightJButton.setBounds( 130, 164, 50, 50 );
242 eightJButton.setText( "8" );
243 contentPane.add( eightJButton );
244 eightJButton.addActionListener(
245
246 new ActionListener() // anonymous inner class
247 {
248 // event handler called when eightJButton is pressed
249 public void actionPerformed( ActionEvent event )
250 {
251 eightJButtonActionPerformed( event );
252 }
253
254 } // end anonymous inner class
255
256 ); // end call to addActionListener
257
258 // set up nineJButton
259 nineJButton = new JButton();
260 nineJButton.setBounds( 180, 164, 50, 50 );
261 nineJButton.setText( "9" );
262 contentPane.add( nineJButton );
263 nineJButton.addActionListener(
264
265 new ActionListener() // anonymous inner class
266 {
267 // event handler called when nineJButton is pressed
268 public void actionPerformed( ActionEvent event )
269 {
270 nineJButtonActionPerformed( event );
271 }
272
273 } // end anonymous inner class
274
275 ); // end call to addActionListener
276
277 // set up clearJButton
278 clearJButton = new JButton();
279 clearJButton.setBounds( 80, 214, 50, 50 );
280 clearJButton.setText( "C" );
281 contentPane.add( clearJButton );
282 clearJButton.addActionListener(
283
284

Computer Science & Information Technology

You might also like to view...

Small-screen devices, such as tablets, e-readers, and smartphones, use mobile browsers

Indicate whether the statement is true or false

Computer Science & Information Technology

When using MLA style, the lines of the research paper are single-spaced

Indicate whether the statement is true or false

Computer Science & Information Technology

The special character for & is rendered using ____.

A. ® B. © C. " D. &

Computer Science & Information Technology

In the figure above, which of the following is true about the three text frames?

A. They are aligned B. They are distributed evenly C. They are neither aligned nor distributed D. They are grouped

Computer Science & Information Technology