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 cre- ateUserInterface. Add code to the instructionsJButtonActionPerformed method so that the speech synthesizer speaks the instructions of the game.

g) Adding code to


```
1 // CrapsGame.java
2 // This application plays a simple craps game
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.*;
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 {
```

Computer Science & Information Technology

You might also like to view...

Which statement would be used to declare a 10-element integer array c?

a. array c<12; b. array c; c. array<12>c d. arrayc;

Computer Science & Information Technology

In multiversion timestamp ordering, read operations can access tentative versions of objects. Give an example to show how cascading aborts can happen if all read operations are allowed to proceed immediately.

What will be an ideal response?

Computer Science & Information Technology

Use the Path Control to simulate the motion of text inside a MARQUEE tag.

What will be an ideal response?

Computer Science & Information Technology

If you want a macro to be available to all Word documents, you want to make sure it is save in the Normal template

Indicate whether the statement is true or false

Computer Science & Information Technology