IBM

Scribble applet

This example is an applet which lets you 'scribble' with the mouse in an applet window (based on the example in the book Java in a Nutshell, by David Flanagan).

This example demonstrates the Java 1.1 mouse and button event handling mechanism, and as such it requires a Java 1.1 appletviewer or browser (such as Netscape Communicator 4.02 with the JDK 1.1 patch) to run.

Here's the source, as a plain ASCII file.

Event handling in Java 1.1

The init method of the applet registers the Scribble class as a listener for mouse events (mouse button clicks, etc.) and mouse movements. It also adds a Button for clearing the applet window, and registers the Scribble class as a listener for the action events that occur when the button is pressed.

Once registered as a listener for events, appropriate methods are called whenever events occur. The Scribble applet only uses the methods mousePressed (called when the primary mouse button is depressed), mouseDragged (called repeatedly when the mouse moves over the window with the primary button held down), and actionPerformed (called when the "Clear" Button is clicked).

Other methods (described by the Listener interface classes listed after the implements keyword on the class instruction) receive other events (such as mouseReleased, called when the primary mouse button is released); these other methods are provided automatically by NetRexx since the class is indicated to be an adapter class.

Compiling the applet

The applet is compiled using the same steps as for the 'Hello World' applet.

To have the applet displayed in World Wide Web page, you then need to

  1. place the class file in the same directory as the page in which is is to appear
  2. refer to it, wherever you want it to appear on the page, with the <applet> tag:
    
      <applet code="Scribble.class" width=400 height=400>
    
      </applet>
    
    

    which also sets the size for the window in which the applet will appear.

You can then view the page with a Java-1.1-enabled browser, or with the 'applet viewer' that comes with the Java Development Kit. If you are using such program, you should see the applet below here.

For more details on running applets, see the Hello World applet page.


[ IBM | Help | Legal | Privacy ]