import java.awt.*;        // Java visual tools
import java.awt.event.*;  // Java visual events
import java.applet.*;     // Java applet classes

// This class further demonstrates mouse events, as
// well as storing information to be redrawn when
// the window is manipulated.

// Note that this class now implements MouseListener
// in order to detect the mouse button being pressed
// or releases or entering the applet.

public class Sketch extends Applet
                    implements MouseMotionListener,
                               MouseListener {

  // The variables are global to all methods in the class.
  // They wil be used to keep track of what has been drawn
  // before, in case it needs to be redrawn.

  // In this case, we will draw by drawing a line between
  // the last location the mouse was, and the current mouse
  // location. We will store each of these "line segments"
  // from (X1, Y1) to (X2, Y2) in arrays, one for each of
  // those 4 coordinates.

  private int lines= 0; // The number of line segments so far.

  // We will create an array of integers for each of the 4
  // coordinates. The data for the ith line segment will be
  // stored in X1[i], Y1[i], X2[i], Y2[i].

  private int[] X1 = new int[1000];
  private int[] Y1 = new int[1000];
  private int[] X2 = new int[1000];
  private int[] Y2 = new int[1000];

  // We will also keep track of the last location of the mouse,
  // in order to draw a line segment from it to the current
  // mouse location.

  private int lastX = 0;
  private int lastY = 0;

  // We will listed for both MouseMotion events (specifically,
  // dragging the mouse) and Mouse events (specifically, entering
  // the applet or pressing the mouse to begin a drag).

  public void init() {
    addMouseMotionListener(this);
    addMouseListener(this);
    }

  // This method is automatically called when the applet is
  // redrawn for any reason (such as being resized, maximized,
  // etc.). If that happens, we will redraw all of the line
  // segments created so far (otherwise, they will not be
  // redrawn with the applet). Note that paint is automatically
  // passed the Graphics component, so we don't need to get it.

  // This is done with the drawLine method, which takes the start
  // X and Y coordinates and the end X and Y coordinates
  // as parameters.

  public void paint(Graphics g) {
    for (int i = 0; i < lines; i++) {
      g.drawLine(X1[i], Y1[i], X2[i], Y2[i]);
      }
    }

  // We begin the process of drawing when either the mouse is
  // pressed inside the applet (which causes a mousePressed
  // event) or the mouse is dragged into the applet (which
  // causes a mouseEntered event). In either case, we store the
  // current corrdinated of the mouse as the beginning of the
  // next line segment.

  public void mousePressed(MouseEvent e) {
    lastX = e.getX();
    lastY = e.getY();
    }
  public void mouseEntered(MouseEvent e) {
    lastX = e.getX();
    lastY = e.getY();
    }

  // Additional methods in the MouseListener interface that
  // must be implemented.
  public void mouseReleased(MouseEvent e) {}
  public void mouseClicked(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}


  // Whenever the mouse is dragged, we get the current location
  // of the mouse, and draw a line from the last location (in
  // lastX and lastY) to that current location.

  public void mouseDragged(MouseEvent e) {

    int x = e.getX(); // Get current coordinates
    int y = e.getY();

    Graphics g = getGraphics();     // Get the Graphics component
    g.drawLine(lastX, lastY, x, y); // and draw the line.

    // Store this data as the next line segment in the arrays, and
    // increment the number of lines.

    X1[lines] = lastX;
    Y1[lines] = lastY;
    X2[lines] = x;
    Y2[lines] = y;
    lines++;

    // Update the beginning of the next line to be the end of
    // this one.

    lastX = x;
    lastY = y;
    }

  public void mouseMoved(MouseEvent e) {}

  }

