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
Workshop: Handling Parameters in an Applet
The next project you'll undertake has little practical value, except perhaps as a taunting device. The ShowWeight applet takes a person's weight and displays it under several different units. The applet takes two parameters: a weight in pounds, and the name of the person who weighs that amount. The weight is used to figure out the person's weight in ounces, kilograms, and metric tons, all of which are displayed.
Create a new file with your word processor and give it the name ShowWeight.java. Enter Listing 17.4 into the file. Then save and compile the file.
Example 17.4. The Full Text of ShowWeight.java
1: import java.awt.*;
2:
3: public class ShowWeight extends javax.swing.JApplet {
4: float lbs = (float)0;
5: float ozs;
6: float kgs;
7: float metricTons;
8: String name = "somebody";
9:
10: public void init() {
11: String lbsValue = getParameter("weight");
12: if (lbsValue != null) {
13: Float lbsTemp = Float.valueOf(lbsValue);
14: lbs = lbsTemp.floatValue();
15: }
16: String personValue = getParameter("person");
17: if (personValue != null)
18: name = personValue;
19:
20: ozs = (float)(lbs * 16);
21: kgs = (float)(lbs / 2.204623);
22: metricTons = (float)(lbs / 2204.623);
23: }
24:
25: public void paint(Graphics screen) {
26: Graphics2D screen2D = (Graphics2D) screen;
27: screen2D.drawString("Studying the weight of " + name, 5, 30);
28: screen2D.drawString("In pounds: " + lbs, 55, 50);
29: screen2D.drawString("In ounces: " + ozs, 55, 70);
30: screen2D.drawString("In kilograms: " + kgs, 55, 90);
31: screen2D.drawString("In metric tons: " + metricTons, 55, 110);
32: }
33: }
The init() method is where the two parameters are loaded into the applet. Because they come from the Web page as strings, they must be converted into the form you need: a floating-point number for the lbs variable, and a string for name. Converting a string to a floating-point number requires two steps: converting the string to a Float object and then converting that object to a variable of the type float.
Lines 20–22 are used to convert the lbs variable into different units of measure. Each of these statements has (float) in front of the conversion equation. This is used to cast the result of the equation into a floating-point number.
The paint() method of the applet uses the drawString() method of the Graphics2D class to display a line of text onscreen. The paint() method has three arguments: the text to display, and the x and y positions where the text should be shown.
Before you can test the ShowWeight applet, you need to create a Web page that contains the applet. Open up a new file on your word processor and name it ShowWeight.html. Enter Listing 17.5 and save it when you're done.
Example 17.5. The Full Text of ShowWeight.html
1: <applet code="ShowWeight.class" height=170 width=210> 2: <param name="person" value="Konishiki"> 3: <param name="weight" value=605> 4: </applet>
Use a Web browser equipped with the Java Plug-in to see the ShowWeight applet. This demonstration uses Konishiki as its example because the American-born sumo wrestling champion weighs in at more than 605 pounds, making him the largest of these immodest, bikini-wearing behemoths. You can substitute anyone whose weight is either exemplary or well-known. Figure 17.4 shows an example of output from the applet. As you can see, Konishiki's workout regimen doesn't include a lot of fat-free SnackWell's Devil's Food Cakes.
Figure 17.4 The ShowWeight applet loaded with Internet Explorer.
To make the applet display a different name along with a different value for the "weight" parameter, all you have to change is the ShowWeight.html file. The applet itself will continue to work correctly.
Summary | Next Section

Account Sign In
View your cart