Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

Workshop: Making Your Computer Talk to You

At this point, you've seen how Java's audio capabilities can be used to either simulate hysterics or to promote them. Your workshop project this hour puts Java's aural features to more productive use by creating a simple math tutor.

The MathMan applet uses 12 sound files that are available from the book's Web site: plus.wav, equals.wav, and 10 numbered files from number0.wav to number10.wav. If you haven't retrieved these files yet, visit http://www.java24hours.com. All 12 files should be saved in the same folder you're going to use for this workshop's .java, .class, and .html files.

Each of these files contains a computer-synthesized voice speaking a specific word. See if you can guess what these words are as you enter the text of Listing 22.2 into your word processor.

Example 22.2. The Full Source Code of MathMan.java

 1: import java.awt.*; 
 2: import java.applet.AudioClip; 
 3: 
 4: public class MathMan extends javax.swing.JApplet 
 5:     implements Runnable { 
 6: 
 7:     AudioClip[] number = new AudioClip[11]; 
 8:     AudioClip plus; 
 9:     AudioClip equals; 
10:     int sum, num1, num2; 
11:     Thread runner; 
12: 
13:     public void start() { 
14:          if (runner == null) { 
15:              runner = new Thread(this); 
16:              runner.start(); 
17:          } 
18:     } 
19: 
20:     public void stop() { 
21:         if (runner != null) 
22:             runner = null; 
23:     } 
24: 
25:     public void init() { 
26:         equals = getAudioClip(getCodeBase(), "equals.wav"); 
27:         plus = getAudioClip(getCodeBase(), "plus.wav"); 
28:         for (int i = 0; i < number.length; i++) 
29:             number[i] = getAudioClip(getCodeBase(), 
30:                 "number" + i + ".wav"); 
31:     } 
32: 
33:     public void run() { 
34:         Thread thisThread = Thread.currentThread(); 
35:         while (runner == thisThread) { 
36:             sum = (int) Math.floor(Math.random() * 10 + 1);  
37:             num2 = (int) Math.floor(Math.random() * sum); 
38:             num1 = sum - num2; 
39:             repaint(); 
40:             number[num1].play(); 
41:             pause(600); 
42:             plus.play(); 
43:             pause(600); 
44:             number[num2].play(); 
45:             pause(600); 
46:             equals.play(); 
47:             pause(600); 
48:             number[sum].play(); 
49:             pause(5000); 
50:         } 
51:    } 
52: 
53:     public void paint(Graphics screen) { 
54:         super.paint(screen); 
55:         int width = getSize().width; 
56:         int height = getSize().height; 
57:         Graphics2D screen2D = (Graphics2D) screen; 
58:         screen2D.setColor(Color.white); 
59:         screen2D.fillRect(0, 0, width, height); 
60:         screen2D.setColor(Color.black); 
61:         Font dialog = new Font("Dialog", Font.BOLD, 48); 
62:         screen2D.setFont(dialog); 
63:         screen2D.drawString(num1 + " + " + num2 + " = " + sum, 
64:             width / 2 - 100, height / 2 - 25); 
65:     } 
66: 
67:     private void pause(int duration) { 
68:         try { 
69:             Thread.sleep(duration); 
70:         } catch (InterruptedException e) { } 
71:     } 
72: } 

When you're done, save the file as MathMan.java and create a new file. Enter the text of Listing 22.3 and save it as MathMan.html.

Example 22.3. The Full Text of MathMan.html

1: <applet code="MathMan.class" height=50 width=80> 
2: </applet> 

Compile the applet's class file and load the page Mathman.html in a Web browser. If you are using the SDK, you can compile and test this applet with the following command:

javac MathMan.java 
MathMan.html 

This applet will display a simple mathematical expression using addition, as shown in Figure 22.2.

22fig02.gif

Figure 22.2 Listening to the MathMan application.

Each of the 12 WAV files required by this applet are loaded into their own AudioClip objects. These files will be played with calls to the play() method so that the mathematical expression is read aloud by the program.

Although this math tutor is a little basic for most people outside of the legislative branch of government, it's a good demonstration of how sound can enhance an educational presentation.

Share ThisShare This

Informit Network