Scribble Applet : Applet « Swing JFC « Java






Scribble Applet

Scribble Applet
   

/*
 * This example is from the book "Java Foundation Classes in a Nutshell".
 * Written by David Flanagan. Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

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

/** A simple applet using the Java 1.0 event handling model */
public class Scribble extends Applet {
  private int lastx, lasty;    // Remember last mouse coordinates.
  Button erase_button;         // The Erase button.
  Graphics g;                  // A Graphics object for drawing.

  /** Initialize the button and the Graphics object */
  public void init() {
    erase_button = new Button("Erase");
    this.add(erase_button);
    g = this.getGraphics();
    this.requestFocus();  // Ask for keyboard focus so we get key events
  }
  /** Respond to mouse clicks */
  public boolean mouseDown(Event e, int x, int y) {
    lastx = x; lasty = y;             // Remember where the click was
    return true;
  }
  /** Respond to mouse drags */
  public boolean mouseDrag(Event e, int x, int y) {
    g.setColor(Color.black);
    g.drawLine(lastx, lasty, x, y);   // Draw from last position to here
    lastx = x; lasty = y;             // And remember new last position
    return true;
  }
  /** Respond to key presses: Erase drawing when user types 'e' */
  public boolean keyDown(Event e, int key) {
    if ((e.id == Event.KEY_PRESS) && (key == 'e')) {
      g.setColor(this.getBackground());   
      g.fillRect(0, 0, bounds().width, bounds().height);
      return true;
    }
    else return false;
  }
  /** Respond to Button clicks: erase drawing when user clicks button */
  public boolean action(Event e, Object arg) {
    if (e.target == erase_button) {
      g.setColor(this.getBackground());   
      g.fillRect(0, 0, bounds().width, bounds().height);
      return true;
    }
    else return false;
  }
}


           
         
    
    
  








Related examples in the same category

1.Icon Demo Applet
2.Socket Applet
3.Applet Socket Quote
4.Applet Print
5.Applet System Properties
6.Applet Sound
7.Show Document
8.Applet communication (talk to each other)
9.Get Applets
10.JDBC applet
11.An application and an appletAn application and an applet
12.Applet and Swing ComponentsApplet and Swing Components
13.Icon behavior in JbuttonsIcon behavior in Jbuttons
14.Signed Applet
15.Load resource in Applet
16.Toggle buttonToggle button
17.Bouncing CircleBouncing Circle
18.Applet Menu Bar Demo
19.Applet clock demoApplet clock demo
20.Applet event testerApplet event tester
21.Applet with parameters
22.First applet
23.AppletViewer - a simple Applet Viewer program
24.A simple loan calculator appletA simple loan calculator applet
25.URLButton -- a Button-like object that jumps to a URL
26.Get list of APPLET tags in one HTML file
27.Demonstration of Applet Methods
28.Demonstrates getParameterInfo() and getAppletInfo()
29.Walking Text Demo
30.Java Applets Can Run CGI's (on some browsers)
31.Demo Choice Applet
32.Demo Applet: Connect to Legacy System
33.ShowDocApplet: try out showDocument()
34.Bookmarks
35.Java Wave Applet Demo
36.Slide Puzzle
37.A class to allow use of the ncsa ISMAP format in java applets
38.Image Loader Applet
39.Thread Race Applet
40.Applet: Print from an Applet
41.Calling methods of an Applet from JavaScript code
42.Read an applet parameters
43.Change an applet background color
44.Passing Parameters to Java Applet
45.Display message in browser status bar
46.Your own Applet runtime environment
47.This applet is a simple button that plays an audio clip when pushed
48.EventQueue.invokeLater and JApplet
49.An applet component that draws a bar chart
50.This applet displays a greeting from the authors