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
- 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
Sending Parameters to Applets
Java applets are never run from the command line, so you can't specify arguments the way you can with applications. Applets use a different way to receive information at the time the program is run. This information is called parameters, and you can send parameters through the HTML page that runs the applet. You have to use a special HTML tag for parameters called <PARAM>.
To see how the Blanks application would be rewritten as an applet, open your word processor, enter the text of Listing 4.5, then save the file as BlanksApplet.java.
Example 4.5. The Full Text of BlanksApplet.java
1: import java.awt.*;
2:
3: public class BlanksApplet extends javax.swing.JApplet {
4: String parameter1;
5: String parameter2;
6: String parameter3;
7:
8: public void init() {
9: parameter1 = getParameter("adjective1");
10: parameter2 = getParameter("adjective2");
11: parameter3 = getParameter("adjective3");
12: }
13:
14: public void paint(Graphics screen) {
15: screen.drawString("The " + parameter1
16: + " " + parameter2 + " fox "
17: + "jumped over the "
18: + parameter3 + " dog.", 5, 50);
19: }
20: }
Save the file and then compile it. Using the SDK, you can accomplish this by typing the following at the command line:
javac BlanksApplet.java
Before you can try out this applet, you need to create a Web page that displays the BlanksApplet applet after sending it three adjectives as parameters. Open your word processor and enter Listing 4.6, saving it as BlanksApplet.html.
Example 4.6. The Full Text of BlanksApplet.html
1: <applet code="BlanksApplet.class" height=80 width=500> 2: <param name="adjective1" value="lachrymose"> 3: <param name="adjective2" value="magenta"> 4: <param name="adjective3" value="codependent"> 5: </applet>
Save the file when you're done, and load the page using Internet Explorer, the SDK's appletviewer, or another browser equipped with the Java Plug-in. The output should resemble Figure 4.2. Change the values of the value attribute in Lines 2–4 of the BlanksApplet.html file, replacing "lachrymose", "magenta", and "codependent" with your own adjectives, then save the file and run the program again. Try this several times, and you'll see that your program is now flexible enough to handle any adjectives, no matter how well or how poorly they describe the strange relationship between the fox and the dog.
Figure 4.2 The BlanksApplet program displayed with a Web browser.
You can use as many parameters as needed to customize the operation of an applet, as long as each has a different NAME attribute specified along with the <PARAM> tag.
Workshop: Viewing the Code Used to Run Applets | Next Section

Account Sign In
View your cart