Java applet example - get applet parameters from an HTML file

Introduction

Many times when you're developing a Java applet, you want to pass parameters from an HTML page to the applet you're invoking. For instance, you may want to tell the applet what background color it should use, or what font to use, to keep your Java applet consistent with the rest of your HTML-based web site.

A great benefit of passing applet parameters from HTML pages to the applets they call is portability. If you pass parameters into your Java applets, they can be portable from one web page to another, and from one web site to another. On the other hand, if you don't pass parameters into a Java applet, how will the applet know what font to use? Simple - you hardwire it into the Java source code! As I often say to my new puppy at home, "this is bad".

In this brief applet tutorial, we'll show you how to pass applet parameters from an HTML page to a Java applet. Hopefully you'll be able to use this same technique to make your own applets more flexible and portable.

The sample HTML file

The listing below shows the source code for the sample HTML file we created for this article, including the applet tag shown. We name this file AppletParameterTest.html.

<HTML> 
<HEAD> 
<TITLE>Java applet example - Passing applet parameters to Java applets</TITLE> 
</HEAD> 
<BODY> 
<APPLET CODE="AppletParameterTest.class" WIDTH="400" HEIGHT="50">
    <PARAM NAME="font"    VALUE="Dialog">
    <PARAM NAME="size"    VALUE="24">
    <PARAM NAME="string"  VALUE="Hello, world ... it's me.   :)">
</APPLET> 
</BODY> 
</HTML>

<!-- AppletParameterTest.html - an HTML file that 
     calls a Java applet with the applet tag, 
     and passes several parameters to the applet via 
     the <PARAM> tag. -->

As you can see, the HTML file identifies the Java applet that we want to load - AppletParameterTest.class using an APPLET tag. Then, the only thing we need to do to pass parameters to the applet are to include the name and value of the desired parameters in <PARAM> tags. These names and values can be whatever we want - whatever makes sense for your applet. As a final note, notice that the <PARAM> tags are enclosed within the <APPLET> tags.

The ParamTest.java file

So far we've seen to create the HTML code that (1) invokes a Java applet using an applet tag, and (2) passes parameters to the applet. Next, let's look at the Java applet code needed to read the parameters being passed to it. The next listing shows the Java code for the ParamTest.java file.

import java.applet.*;
import java.awt.*;

/**
 * A Java applet parameter test class.
 * Demonstrates how to read applet parameters.
 */
public class AppletParameterTest extends Applet {

   public void paint(Graphics g) {

      String myFont   = getParameter("font");
      String myString = getParameter("string");
      int mySize      = Integer.parseInt(getParameter("size"));

      Font f = new Font(myFont, Font.BOLD, mySize);
      g.setFont(f);
      g.setColor(Color.red);
      g.drawString(myString, 20, 20);

   }
}

As you can see, this is a fairly simple Java applet. It retrieves the applet parameters passed to it with the getParameter() method, then uses those parameters to construct its output. In this case the few parameters we've passed in deal with the font and the string to be printed. In a more elaborate example we might pass in more applet parameters to customize the applet's appearance.

Try the on-line demo, and download the source

Click this link to see the example AppletParameterTest applet in action. Once that page is loaded you can also save the HTML source code once it's loaded into your browser.

You can also view/download the example Java applet  ParamTest.java source code.