Handling Parameters in an Applet

The workshop for this hour 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, which are all displayed.

Create a new empty Java file called WeightScale, enter the text of Listing 17.3 into the file, and save the file.

Listing 17.3. The Full Text of WeightScale.java


 1: import java.awt.*;
 2:
 3: public class WeightScale extends javax.swing.JApplet {
 4:     float lbs = 0F;
 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:             lbs = Float.valueOf(lbsValue);
14:         }
15:         String personValue = getParameter("person");
16:         if (personValue != null) {
17:             name = personValue;
18:         }
19:         ozs = (float) (lbs * 16);
20:         kgs = (float) (lbs / 2.204623);
21:         metricTons = (float) (lbs / 2204.623);
22:     }
23:
24:     public void paint(Graphics screen) {
25:         Graphics2D screen2D = (Graphics2D) screen;
26:         screen2D.drawString("Studying the weight of " + name, 5, 30);
27:         screen2D.drawString("In pounds: " + lbs, 55, 50);
28:         screen2D.drawString("In ounces: " + ozs, 55, 70);
29:         screen2D.drawString("In kilograms: " + kgs, 55, 90);
30:         screen2D.drawString("In metric tons: " + metricTons, 55, 110);
31:     }
32: }


The init() method is where the two parameters are loaded into the applet. Because parameters come from the web page as strings, the weight parameter must be converted to a floating-point number to use it in mathematical expressions. The Float object class includes a valueOf(String) that returns a string’s value as a Float. This value is automatically unboxed to the float variable type in Line 13.

Lines 19–21 are used to convert the lbs variable into different units of measure. Each statement 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 drawString() to display a line of text. The paint() method has three arguments: the text to display, the x position, and the y position where the text should be shown.

Before you can test the WeightScale applet, you must create a web page that contains the applet. Create a new HTML file in NetBeans called WeightScale. Enter Listing 17.4 into the file, and then open the newly created web page in a browser—right-click WeightScale.html in the Project pane and choose View.

Listing 17.4. The Full Text of WeightScale.html


 1: <applet code="WeightScale.class" codebase="..\..\build\classes"
2:     height="170" width="210">
3:     <param name="person" value="Konishiki">
4:     <param name="weight" value="605">
5: </applet>


This demonstration uses Konishiki, an American-born sumo wrestling champion who weighed 605 pounds when he competed, making him the largest of the immodest, bikini-wearing behemoths. You can substitute anyone whose weight is either exemplary or well-known. Figure 17.2 shows an example of output from the applet.

Figure 17.2. The WeightScale applet loaded with Internet Explorer.

Image

To make the applet display a different name along with a different value for the weight parameter, you have to change the WeightScale.html file. The applet itself continues to work correctly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.110.155