// 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. // This example was modified from Scribble2.java by David Flanagan. // The program was converted from an Applet to a standalone program. // Modified by Matt Mahoney, mmahoney@cs.fit.edu // No need to import java.applet import java.awt.*; import java.awt.event.*; // Extend Frame instead of Applet public class Scribble3 extends Frame implements MouseListener, MouseMotionListener { private int last_x, last_y; // Change init() to a constructor public Scribble3() { // 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. // (unchanged). this.addMouseListener(this); this.addMouseMotionListener(this); } // A method from the MouseListener interface. Invoked when the // user presses a mouse button (unchanged). 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 (unchanged). 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 (unchanged). 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 (unchanged). public void mouseMoved(MouseEvent e) {;} // Add main() to create a new Scribble3 and show it public static void main(String[] args) { // Create the top level window (still hidden) Scribble3 scribble3=new Scribble3(); // Add a handler to detect window closing (user clicks on [X]). // We could also use the this method to detect mouse events. scribble3.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { e.getWindow().dispose(); // close the window System.exit(0); // end the program } }); // Make the window visible scribble3.setSize(400, 300); scribble3.show(); } }