// This example is from the book "Java in a Nutshell, Second Edition". // Written by David Flanagan. Copyright (c) 1997 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. /* Modified by R. Brown 1/8/99: name change to Self, doc tweak This example demonstrates an Applet that serves as its own adapters for listening to MouseEvents and MouseMotionEvents */ import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Self extends JApplet implements MouseListener, MouseMotionListener { int lastX, lastY; public void init() { // Tell this applet which MouseListener and MouseMotionListener // objects to notify when mouse and mouse motion events occur. // We use arg (this) since Self implements those Listeners. 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) { lastX = e.getX(); lastY = 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(lastX, lastY, x, y); lastX = x; lastY = 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) {;} }