FileDocCategorySizeDatePackage
Scribble4.javaAPI DocExample2119Mon May 19 18:23:14 BST 1997None

Scribble4

public class Scribble4 extends Applet

Fields Summary
private int
lastx
private int
lasty
Constructors Summary
Methods Summary
public voidinit()
Tell the system we're interested in mouse events, mouse motion events, and keyboard events. This is required or events won't be sent.

    this.enableEvents(AWTEvent.MOUSE_EVENT_MASK |
                      AWTEvent.MOUSE_MOTION_EVENT_MASK |
                      AWTEvent.KEY_EVENT_MASK);
    this.requestFocus();  // Ask for keyboard focus so we get key events
  
public voidprocessKeyEvent(java.awt.event.KeyEvent e)
Called on key events: clear the screen when 'c' is typed

    if ((e.getID() == KeyEvent.KEY_TYPED) && (e.getKeyChar() == 'c")) {
      Graphics g = this.getGraphics();
      g.setColor(this.getBackground());
      g.fillRect(0, 0, this.getSize().width, this.getSize().height);
    }
    else super.processKeyEvent(e);  // pass unhandled events to our superclass
  
public voidprocessMouseEvent(java.awt.event.MouseEvent e)
Invoked when a mouse event of some type occurs

    if (e.getID() == MouseEvent.MOUSE_PRESSED) {  // check the event type
      lastx = e.getX(); lasty = e.getY();
    }
    else super.processMouseEvent(e); // pass unhandled events to superclass
  
public voidprocessMouseMotionEvent(java.awt.event.MouseEvent e)
Invoked when a mouse motion event occurs

    if (e.getID() == MouseEvent.MOUSE_DRAGGED) {  // check type
      int x = e.getX(), y = e.getY();
      Graphics g = this.getGraphics();
      g.drawLine(lastx, lasty, x, y);
      lastx = x; lasty = y;
    }
    else super.processMouseMotionEvent(e);