Passing Parameters To An Applet

It is often useful to be able to pass parameters from a HTML page to an applet. This allows us to modify the action of an applet without actually having to re-code or re-compile the applet. For example earlier, we wrote an applet that displayed the string "Hello World!". We may wish to re-write that applet to display Hello Derek!, or whatever name we desire at a specific location that we can change within the HTML code.

To do this we use the <param> markup tag within the <applet> markup tag of the HTML page. So to re-write the code in the section called “A Simple Applet Example - Hello World!” to add the param tags to our HTML page, one for each parameter.

 1 
 2   
 3  <title> Test Applet Page </title>
 4  <hr>
 5  <applet code=HelloWorld2.class width=200 height=100>
 6    <param name=displayName value="John Smith">
 7    <param name=startx value="50" >
 8    <param name=starty value="20" >
 9  </applet>
10  <hr> 
11   
12 

This Applet now looks like this: The source code for HelloWorld2.java is:

 1 
 2   
 3 // The HelloWorld2 Applet - Allows the modification of the parameters
 4 // that are displayed in the applet.
 5 
 6 import java.awt.Graphics;
 7 import java.applet.Applet;
 8 
 9 public class HelloWorld2 extends Applet
10 {
11   // The class states - declared here so that the parameters can be passed 
12   // from the init() method to the paint() method. These states are given 
13   // default values of World to be displayed at (10,10)
14 	
15   private String theDisplayString = "World";
16   private int theStartx = 10;
17   private int theStarty = 10;
18 	
19   public void init()
20   {
21     // try to read in the parameters from the HTML file
22 	String tempName = this.getParameter("displayName"); 
23 	String tempXString = this.getParameter("startx");
24 	String tempYString = this.getParameter("starty");
25 		
26     // check to see if any of the parameters are blank
27 
28     if ((tempName!=null)&&(tempXString!=null)&&(tempYString!=null))
29     {
30       // OK - none of the parameters are blank 
31       // the HTML author did their job properly
32 
33       this.theDisplayString = tempName;
34 			
35       // convert the (X,Y) strings received into integer values.
36       this.theStartx = (new Integer(tempXString)).intValue();
37       this.theStarty = (new Integer(tempYString)).intValue();
38     }
39   }
40 	
41   public void paint(Graphics g)
42   {
43     g.drawString("Hello "+theDisplayString+"!",theStartx,theStarty);
44   }
45 }
46   
47 

This code will result in an applet that looks like Figure 7.5, “HelloWorld Applet With Parameter Passing”

Figure 7.5. HelloWorld Applet With Parameter Passing

HelloWorld Applet With Parameter Passing

See the Applet Working Here - HelloWorld With Parameters.

Bad Parameters: If we omit any of the parameters - In this case I have omitted starty (i.e. the line in the HTML page <param name=starty value="20"> ). This code will result in an applet that looks like Figure 7.6, “HelloWorld Applet With Bad Parameter Passing”. It will not display the name "John Smith" as one of the parameters is missing and so the code we have written returns to the default values of "World" for the name and (10,10) for the (X,Y) location.

Figure 7.6. HelloWorld Applet With Bad Parameter Passing

HelloWorld Applet With Bad Parameter Passing

See the Bad Applet Working Here - HelloWorld With Bad Parameters.