Chapter 9
Test Your Thinking
In today's high tech world, people have come to expect a certain amount of flare and attractiveness in programs they use and from Web sites they visit. While images, sounds, colors, and fonts may not be vital to your program, they can enhance your program a great deal and encourage people to use it.
1) Why is it important that getCodeBase() return the applet's URL?
Answer:
When an applet (or any web page, for that matter) needs to access a file or another page, it useful if all of the references are relative to the main page. This way all of the pages are "relocatable," meaning that the pages and files can be moved without breaking the links.
Using getCodeBase() lets the applet know where it is in the World Wide Web. This allows the applet to build a relative path to any files or pages that the applet needs to reference. It doesn't matter if the applet is accessed on a remote system, or on your local computer. A common mistake made by web designers it to construct a page (or applet) that works on their own computer, but when they "publish" their pages to the production site, the links break. With a little forethought, you won't have to worry about making any changes to you code in order to publish.
2) You have probably seen Web pages that use animated images. Technically, these are called GIF89 images, or animated GIFs, and are a collection of multiple images that can be played in sequence to create a motion effect. Load an animated GIF into a button or label. What happens?
Answer:
Just like in a web page, the applet will also display the animated image. You must use the JButton or JLabel to get this behavior.
To load an animated GIF file, you load it just as you would any other image file. However, when it is displayed, the applet will cycle through the various images in the file to animate it.
Note: To create an animated GIF, you will need special software that is not provided at this web site, or in the book.
Click here for an animated GIF that you can save and use in your program.
A small word of warning: some animated images can be quite large, and some are just annoying, so use them sparingly and only when necessary!
3) Write a program with a button and three text fields where a user can enter values for red, green, and blue. Then, change the color of the button to the color defined by values in the text fields. If you feel brave, try using sliders to define the color values.
Answer:
This code will create the dynamically colored button.
Click here for the ColorerButton.java source code.
Click here to see the ColoredButton applet.
4) Add images to the buttons in your play/loop/stop audio program. Some appropriate audio control images can be found on this book's Web site. Then, modify your program so that it will allow you to select any audio file and play it.
Answer:
The following code will load and play a sound file. You must specify the sound file as a URL for the applet to be able to load it.
Click here for the SelectSound.java source code.
Click here for the SelectSound applet.
Note that this applet uses the Java 2 plugin, so you'll need that installed on your browser for it to work. Also, since we haven't covered Exceptions yet (Chapter 11), we couldn't use full URLs for the file location. This means that the audio files need to reside in the same directory as the class file, just like the example in the book.
5) For the following Question, use your program from Lab 7.1, Question b (the touch-tone phone pad) and the twelve "touchtone" audio files on the Web page.
Modify your touch-tone phone program to play the corresponding touchtone audio file when each button is pressed.
Answer:
The following code will simulate a touchtone phone.
Click here for the Phone.java source code.
Click here for the Phone applet.
As always, there is more than one way of writing this program. We cheated a little and used the final modifier for the clipIndex variable. This allowed us to be able to create the JButtons in a for loop. The tricky piece here was being able to reference enough data from within the inner class to be able to play the right clip. By making clipIndex a final variable, we are telling the Java compiler that clipIndex's value isn't going to change in a subclass. Inner classes can be confusing, so don't get discouraged if you don't understand them right away.
6) Create a font selector program. Display a list of font names, styles, and sizes and allow the user to select one of each. Then display some text using that font.
Answer:
Here is a program that allows the user to select and display fonts.
Note: We appear to have run into a small Java bug. The program works, but the bold and italics selection buttons don't take effect immediately, as they should. Read the applet's web page for more details. If we get a better answer later (like confirmation of a Java bug, or confirmation that we're idiots) we'll update these web pages.
Click here for the FontSelector.java source code.
Click here for the FontSelector applet.
Java bugs aside, this applet is a good exercise in GUI development. Creating pretty GUIs is nice, but it's important that they be functional. You should study the design of this GUI as an example of linking GUI controls to the final result.