Wednesday, October 20, 2010

Workshop: Handling Parameters in an Applet

Workshop: Handling Parameters in an Applet
This hour's workshop project 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, and all of these are displayed.
Create a new file with your word processor and give it the name Enter Listing 15.1 into the file. Then save and compile the file.
Listing 15.1. The full text of

 1: import java.awt.*;
 3: public class ShowWeight extends java.applet.Applet {
 4:     float lbs = (float)0;
 5:     float ozs;
 6:     float kgs;
 7:     float metricTons;
 8:     String name = "somebody";
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;
20:         ozs = (float)(lbs * 16);
21:         kgs = (float)(lbs / 2.204623);
22:         metricTons = (float)(lbs / 2204.623);
23:     }
25:     public void paint(Graphics screen) {
26:         screen.drawString("Studying the weight of " + name, 5, 30);
27:         screen.drawString("In pounds: " + lbs, 55, 50);
28:         screen.drawString("In ounces: " + ozs, 55, 70);
29:         screen.drawString("In kilograms: " + kgs, 55, 90);
30:         screen.drawString("In metric tons: " + metricTons, 55, 110);
31:     }
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.
As you learned with strings, objects and variables are treated differently in Java programs, and there are different things you can do with them. The reason there is a Float object and a float variable type is so you can use a floating-point number as either an object or a variable. The Float object class also has useful methods such as valueOf() and floatValue() that you can use to convert floating-point numbers into different types of variables.
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 convert the result of the equation into a floating-point number. You can use this structure to convert variables of one type to another. Put a variable type in parentheses in front of an equation that produces a result, and it will convert the result to that type.
The paint() method of the applet uses the drawString() method of the Graphics class to display a line of text on-screen. 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 15.2 and save it when you're done.
Listing 15.2. The full text of ShowWeight.html.

1: <--applet code="ShowWeight.class" height=170 width=200>
2: <--param name="person" value="Konishiki">
3: <--param name="weight" value=605>

Use the appletviewer tool 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 the bikini-wearing behemoths. You can substitute anyone whose weight is either exemplary or well-known. Figure 15.1 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 15.1. <../art/15/15tja01.jpg> The output of the ShowWeight applet.
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.
If you visit a site such as Gamelan on the World Wide Web (), you'll see links to numerous applets that have been made available for public use, including some that offer the source file of the program. These applets often use parameters, especially for animation or scrolling text programs. During this hour you covered all aspects of parameter use in your applets. Parameters can greatly improve the usefulness and performance of applets.
Even if you're the paragon of programming patience and you can wait out the slowest compiler, you'll be using parameters in your Java applets. Like arguments for Java applications, parameters are a useful way to make one program perform in a variety of ways.

No comments:

Post a Comment