Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Retrieving and Using Sounds
- Java Archives
- Workshop: Making Your Computer Talk to You
- Summary
- Q&A
- Quiz
- Activities
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
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.
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.
Summary | Next Section

Account Sign In
View your cart