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

Lesson Four - Scribble

This applet is borrowed from Java in a Nutshell. Because I havn't changed the default colors, it appears to blend into the page. See then next lesson for an example of how to change the colors. Try dragging your mouse around the blank area on the web page. The applet is really there.

The APPLET Tag

The HTML Appplet tag is:
   <APPLET code="Scribble.class" width=300 height=300></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 Scribble extends Applet {
    private int last_x = 0;
    private int last_y = 0;
    
    // called when the user clicks
    public boolean mouseDown(Event e, int x, int y)
    {
        last_x = x; last_y = y;
        return true;
    }
    
    // called when the mouse moves with the button down
    public boolean mouseDrag(Event e, int x, int y)
    {
        Graphics g = getGraphics();
        g.drawLine(last_x, last_y, x, y);
        last_x = x;
        last_y = y;
        return true;
    }
}


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