Applet Parameters

Every value on PaintApplet so far - the title text, the background color - has been hardcoded inside the class. Applet parameters let the HTML page that embeds the applet hand it configuration values from the outside, without touching or recompiling the Java code.

Note: This page's code is a preview, not something you can run right on this website - this server runs PHP, not a Java runtime. As covered in Embedding in HTML, all of this uses the old, deprecated <applet> HTML tag itself.

The <param> Tag

Inside the <applet>...</applet> tags, add one <param> element per value, each with a name and a value:

<applet code="PaintApplet.class" width="400" height="300">
  <param name="title" value="My Custom Canvas">
  <param name="bgcolor" value="#FFFACD">
  Your browser cannot run Java applets.
</applet>

The fallback text between the tags is shown by browsers old enough to understand the <applet> tag but unable to run Java - it's ignored otherwise.

Reading Parameters with getParameter()

Inside the applet, getParameter("name") (inherited from Applet) looks up a <param> by name and returns its value as a String - or null if no such parameter was supplied. This only works from init() onward, since that's the first point where the applet is actually attached to its HTML context.

Always defend against a missing or malformed value: a page embedding your applet might omit a parameter entirely, misspell its name, or pass a value that doesn't parse the way you expect. Always null-check getParameter()'s result and fall back to a sensible default, and wrap any parsing (like turning a hex string into a Color) in a try/catch.

Extending PaintApplet: Reading title and bgcolor

Add a String titleText field, and populate it (along with the background) inside init() by reading the two parameters shown above. The rest of the class - drawShapes(), the mouse and keyboard listeners, cycleColor() - stays exactly as it was in Handling Keyboard Events; only init() gains logic, a new field is added, and paint()'s drawString call swaps the hardcoded literal for the field:

private String titleText = "Welcome to PaintApplet!";  // new field, with a default

public void init() {
    // Title: fall back to the default if the parameter is missing
    String titleParam = getParameter("title");
    if (titleParam != null && !titleParam.trim().isEmpty()) {
        titleText = titleParam;
    }

    // Background: fall back to white if missing or not a valid hex color
    String bgParam = getParameter("bgcolor");
    Color background = Color.WHITE;
    if (bgParam != null) {
        try {
            background = Color.decode(bgParam);
        } catch (NumberFormatException ex) {
            // malformed value (e.g. "not-a-color") - keep the white default
        }
    }
    setBackground(background);

    setFocusable(true);
    // ... addMouseMotionListener(...) and addKeyListener(...) unchanged from the previous lesson
}

And paint() now draws the field instead of the string literal:

g.drawString(titleText, 20, 30);  // was: g.drawString("Welcome to PaintApplet!", 20, 30);
ParameterRead viaFallback if missing/invalid
titlegetParameter("title")"Welcome to PaintApplet!"
bgcolorgetParameter("bgcolor"), parsed with Color.decode()Color.WHITE

With the HTML at the top of this lesson embedding the applet, it now shows "My Custom Canvas" on a pale yellow background - all without changing a single line inside drawShapes(), the listeners, or cycleColor().

Next steps: Text and shapes only get you so far. Head to Displaying Images to load and draw an actual image file inside an applet.