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

Lesson Six - ClearableScribble

This applet is borrowed from Java in a Nutshell. It is a subclass of the ColorScribble in lesson Five. In addition to the stuff inherited from ColorScribble, ClearableScribble adds a button that clears the window when clicked.

The APPLET Tag

The HTML Appplet tag is:
   <APPLET code="ClearableScribble.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 ClearableScribble extends ColorScribble {
    private Button clear_button;
    
    public void init() {
        // do ColorScribble initialization first
        super.init();
        // Create a button and add it to the applet.
        clear_button = new Button("Clear");
        clear_button.setForeground(Color.black);
        clear_button.setBackground(Color.lightGray);
        this.add(clear_button);
    }
    
    public boolean action(Event event, Object arg) {
        // If our button was clicked on, handle it.
        // Otherwise, let our superclass handle it if it wants to.
        if (event.target == clear_button) {
            Graphics g = this.getGraphics();
            Rectangle r = this.bounds();
            g.setColor(this.getBackground());
            g.fillRect(r.x, r.y, r.width, r.height);
            g.setColor(this.getForeground());
            return true;
        }
        else return super.action(event, arg);
    }
}


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