// This example is from _Java Examples in a Nutshell_. (http://www.oreilly.com)
// Copyright (c) 1997 by David Flanagan
// This example is provided WITHOUT ANY WARRANTY either expressed or implied.
// You may study, use, modify, and distribute it for non-commercial purposes.
// For any commercial use, see http://www.davidflanagan.com/javaexamples
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/** A simple applet that uses the Java 1.1 event handling model */
public class Scribble2 extends Applet
implements MouseListener, MouseMotionListener {
private int last_x, last_y;
public void init() {
// Tell this applet what MouseListener and MouseMotionListener
// objects to notify when mouse and mouse motion events occur.
// Since we implement the interfaces ourself, our own methods are called.
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
// A method from the MouseListener interface. Invoked when the
// user presses a mouse button.
public void mousePressed(MouseEvent e) {
last_x = e.getX();
last_y = e.getY();
}
// A method from the MouseMotionListener interface. Invoked when the
// user drags the mouse with a button pressed.
public void mouseDragged(MouseEvent e) {
Graphics g = this.getGraphics();
int x = e.getX(), y = e.getY();
g.drawLine(last_x, last_y, x, y);
last_x = x; last_y = y;
}
// The other, unused methods of the MouseListener interface.
public void mouseReleased(MouseEvent e) {;}
public void mouseClicked(MouseEvent e) {;}
public void mouseEntered(MouseEvent e) {;}
public void mouseExited(MouseEvent e) {;}
// The other method of the MouseMotionListener interface.
public void mouseMoved(MouseEvent e) {;}
}
|