Modify the Craps Game application from Tutorial 15 to use the Java Speech API. The completed application is shown in Fig. 28.19



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

b) Opening the template file. Open the CrapsGame.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 CrapsGame constructor, declare an instance variable of type Synthesizer, which is used to speak text.

e) Creating a Synthesizer object. Inside the CrapsGame constructor, create a Synthe- sizer object, allocate the resource and prepare the synthesizer to speak.

f) Adding code to the instructionsJButtonActionPerformed method. Find the instructionsJButtonActionPerformed method, which immediately follows createUserInterface. Add code to the instructionsJButtonActionPerformed method so that the speech synthesizer speaks the instructions of the game.

g) Adding code to the playJButtonActionPerformed method. Find the playJButtonAct


```
6 import javax.swing.*;
7 import javax.swing.border.*;
8 import javax.speech.*;
9 import javax.speech.synthesis.*;
10
11 public class CrapsGame extends JFrame
12 {
13 // JPanel and TitledBorder to contain dice
14 private JPanel pointDiceJPanel;
15 private TitledBorder pointDiceTitledBorder;
16
17 // JLabels to display the die images in pointDiceJPanel
18 private JLabel pointDie1JLabel;
19 private JLabel pointDie2JLabel;
20
21 // JLabels to display the die images from the rolls of the dice
22 private JLabel die1JLabel;
23 private JLabel die2JLabel;
24
25 // JButtons to allow user to interact with game
26 private JButton instructionsJButton;
27 private JButton playJButton;
28 private JButton rollJButton;
29
30 // JLabel and JTextField to show results of game
31 private JLabel resultJLabel;
32 private JTextField resultJTextField;
33
34 // constants representing winning dice rolls
35 private final int LUCKY_SEVEN = 7;
36 private final int YO_LEVEN = 11;
37
38 // constants representing losing dice rolls
39 private final int SNAKE_EYES = 2;
40 private final int TREY = 3;
41 private final int BOX_CARS = 12;
42 private final int CRAPS = 7;
43
44 // file name and directory constants
45 private final String FILE_PREFIX = "Images/die";
46 private final String FILE_SUFFIX = ".png";
47
48 // instance variables
49 private int myPoint = 0;
50 private Random randomObject = new Random();
51
52 // Synthesizer to speak text
53 private Synthesizer speechSynthesizer;
54
55 // no-argument constructor
56 public CrapsGame()
57 {
58 // initialize Synthesizer
59 try
60 {
61 // create SynthesizerModeDesc for FreeTTS synthesizer
62 SynthesizerModeDesc descriptor = new SynthesizerModeDesc(
63 "Unlimited domain FreeTTS Speech Synthesizer " +
64 "from Sun Labs", null, Locale.US, Boolean.FALSE, null);
65
66 // create a Synthesizer
67 speechSynthesizer = Central.createSynthesizer( descriptor );
68
69 // Synthesizer created successfully
70 if ( speechSynthesizer != null )
71 {
72 // get synthesizer ready to speechSynthesizer.resume();
73 speak speechSynthesizer.allocate();
74 speechSynthesizer.getSynthesizerProperties();
75
76 // get synthesizer properties
77 SynthesizerProperties properties =
78 speechSynthesizer.getSynthesizerProperties();
79
80 // set up speaking rate properties.
81 setSpeakingRate( 120.0f );
82
83 } // end if
84 else
85 {
86 System.err.println( "Synthesizer creation failed." );
87 System.exit( 1 );
88 }
89
90 } // end try
91
92 catch ( Exception myException )
93 {
94 myException.printStackTrace();
95 }
96
97 createUserInterface(); // set up GUI
98 }
99
100 // create and position GUI components; register event handlers
101 private void createUserInterface()
102 {
103 // get content pane for attaching GUI components
104 Container contentPane = getContentPane();
105
106 //
107 contentPane.setLayout( null );
108
109 //
110 pointDiceTitledBorder = new TitledBorder( "Point" );
111
112 //
113 pointDiceJPanel = new JPanel();
114 pointDiceJPanel.setBounds( 16, 16, 200, 116 );
115 pointDiceJPanel.setLayout( null );
116 pointDiceJPanel.setBorder( pointDiceTitledBorder );
117 contentPane.add( pointDiceJPanel );
118
119 //
120 pointDie1JLabel = new JLabel();
121 pointDie1JLabel.setBounds( 24, 34, 64, 56 );
122 pointDiceJPanel.add( pointDie1JLabel );
123
124 //
125 pointDie2JLabel = new JLabel();
126 pointDie2JLabel.setBounds( 120, 34, 64, 56 );
127 pointDiceJPanel.add( pointDie2JLabel );
128
129 //
130 die1JLabel = new JLabel();
131 die1JLabel.setBounds( 40, 150, 64, 64 );
132 contentPane.add( die1JLabel );
133
134 //
135 die2JLabel = new JLabel();
136 die2JLabel.setBounds( 136, 150, 64, 56 );
137 contentPane.add( die2JLabel );
138
139 //
140 instructionsJButton = new JButton();
141 instructionsJButton.setBounds( 232, 10, 115, 23 );
142 instructionsJButton.setText( "Instructions" );
143 contentPane.add( instructionsJButton );
144 instructionsJButton.addActionListener(
145
146 new ActionListener() // anonymous inner class
147 {
148 // event handler called when instructionsJButton
149 // is clicked
150 public void actionPerformed ( ActionEvent event )
151 {
152 instructionsJButtonActionPerformed( event );
153 }
154
155 } // end anonymous inner class
156
157 ); // end call to addActionListener
158
159 // set up playJButton
160 playJButton = new JButton();
161 playJButton.setBounds( 232, 40, 115, 23 );
162 playJButton.setText( "Play" );
163 contentPane.add( playJButton );
164 playJButton.addActionListener(
165
166 new ActionListener() // anonymous inner class
167 {
168 // event handler called when playJButton is pressed
169 public void actionPerformed ( ActionEvent event )
170 {
171 playJButtonActionPerformed( event );
172 }
173
174 } // end anonymous inner class
175
176 ); // end call to addActionListener
177
178 // set up rollJButton
179 rollJButton = new JButton();
180 rollJButton.setBounds( 232, 70, 115, 23 );
181 rollJButton.setText( "Roll" );
182 rollJButton.setEnabled( false );
183 contentPane.add( rollJButton );
184 rollJButton.addActionListener(
185
186 new ActionListener() // anonymous inner class
187 {
188 // event handler called when rollJButton is pressed
189 public void actionPerformed ( ActionEvent event )
190 {
191 rollJButtonActionPerformed( event );
192 }
193
194 } // end anonymous inner class
195
196 ); // end call to addActionListener
197
198 // set up resultJLabel
199 resultJLabel = new JLabel();
200 resultJLabel.setBounds( 232, 100, 48, 16 );
201 resultJLabel.setText( "Result:" );
202 contentPane.add( resultJLabel );
203
204 // set up resultJTextField
205 resultJTextField = new JTextField();
206 resultJTextField.setBounds( 232, 120, 115, 24 );
207 resultJTextField.setHorizontalAlignment( JTextField.CENTER );
208 resultJTextField.setEditable( false );
209 contentPane.add( resultJTextField );
210
211 // set properties of application's window
212 setTitle( "Craps Game" ); // set title bar string
213 setSize( 370, 250 ); // set window size
214 setVisible( true ); // display window
215
216 // ensure synthesizer is cleaned up
217 // when user closes application
218 addWindowListener(
219
220 new WindowAdapter() // anonymous inner class
221 {
222 public void windowClosing( WindowEvent event )
223 {
224 frameWindowClosing( event );
225 }
226
227 } // end anonymous inner class
228
229 ); // end addWindowListener
230
231 } // end method createUserInterface
232
233 // listen to the instructions to play the game
234 private void instructionsJButtonActionPerformed(
235 ActionEvent event )
236 {
237 String instructions = " Instructions: Click the Play button" +
238 " to begin the game. Clicking Play causes you to roll" +
239 " two dice. If the sum of your dice roll is 7 or 11 on " +
240 "your first throw, then you win. However, if the sum is " +
241 "2, 3, or 12 on your first throw, then you lose. If the " +
242 "sum is 4, 5, 6, 7, 8, 9 or 10 on your first throw, " +
243 "then that sum becomes your point. To win, you must " +
244 "continue rolling the dice until you roll the point " +
245 "value again. You lose if you roll a 7 before reaching " +
246 "your point value.";
247
248 // state the instructions
249 speechSynthesizer.speakPlainText( instructions, null );
250
251 } // end method instructionsJButtonActionPerformed
252
253 // start new game of craps
254 private void playJButtonActionPerformed( ActionEvent event )
255 {
256 // clear point icons
257 pointDie1JLabel.setIcon( null );
258 pointDie2JLabel.setIcon( null );
259
260 // reset title of border
261 pointDiceTitledBorder.setTitle( "Point" );
262 pointDiceJPanel.repaint();
263
264 int sumOfDice = rollDice(); // roll dice
265
266 // check results of the first dice roll
267 switch ( sumOfDice )
268 {
269 // win on first roll
270 case LUCKY_SEVEN:
271 case YO_LEVEN:
272 resultJTextField.setText( "You win!!!" );
273 speechSynthesizer.speakPlainText(
274 "Congratulations, you won!", null );
275 break;
276
277 // lose on first roll
278 case SNAKE_EYES:
279 case TREY:
280 case BOX_CARS:
281 resultJTextField.setText( "Sorry, you lose." );
282 speechSynthesizer.speakPlainText(
283 "Sorry, you lost!", null );
284 break;
285
286 // remember point in instance variable
287 default:
288
289 // set the point and output result
290 myPoint = sumOfDice;
291 resultJTextField.se

Computer Science & Information Technology

You might also like to view...

Which cmdlet should you use if you want to display information about a cmdlet?

A. list-cmdlet B. display-item C. show-info D. get-help

Computer Science & Information Technology

In Access, the ________ concatenates the string with the rest of the expression

Fill in the blank(s) with correct word

Computer Science & Information Technology

____________________ time measures the amount of time it takes a storage device to locate an item on a storage medium or the time required to deliver an item from memory to the processor.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

What is the data rate of an OC-3 connection?

a. 51.84 Mbps b. 622 Mbps c. 155.52 Mbps d. 159.25 Gbps

Computer Science & Information Technology