Index Lesson One Lesson Two Lesson Three Lesson Four Lesson Five Lesson Six Lesson Seven

Lesson Five - ColorScribble

This applet is borrowed from Java in a Nutshell. It is derrived from the Scribble applet in lesson four. In this version, the foreground and background colors are settable by using applet parameters in the APPLET HTML tag.

The APPLET Tag

The HTML Appplet tag is:
   <APPLET code="ColorScribble.class" width=300 height=300>
   <PARAM name="foreground" value="0000FF">
   <PARAM name="background" value="FFCCCC">
   </APPLET>

Program Listing

The java program is:

// This example is from the book _Java in a Nutshell_ by David Flanagan.
// Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.

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

public class ColorScribble extends Scribble {
    // Read in two color parameters and set the colors.
    public void init() {
        super.init();
        Color foreground = getColorParameter("foreground");
        Color background = getColorParameter("background");
        if (foreground != null) this.setForeground(foreground);
        if (background != null) this.setBackground(background);
    }
    
    // Read the specified parameter.  Interpret it as a hexidecimal
    // number of the form RRGGBB and convert it to a color.
    protected Color getColorParameter(String name) {
        String value = this.getParameter(name);
        int intvalue;
        try { intvalue = Integer.parseInt(value, 16); }
        catch (NumberFormatException e) { return null; }
        return new Color(intvalue);
    }
    
    // Return info about the supported parameters.  Web browsers and applet
    // viewers should display this information, and may also allow users to
    // set the parameter values.
    public String[][] getParameterInfo() {
        String[][] info = {
            // Array of arrays of strings describing each parameter.
            // Format: parameter name, parameter type, parameter description
            {"foreground", "hexidecimal color value", "foreground color"},
            {"background", "hexidecimal color value", "background color"}
        };
        return info;
    }
    
    // Return information suitable for display in an About dialog box.
    public String getAppletInfo() {
        return "Scribble v. 0.02.\nWritten by David Flanagan.";
    }
}


Index Lesson One Lesson Two Lesson Three Lesson Four Lesson Five Lesson Six Lesson Seven