//********************************************************************
//    RubberLines.java       Author:     Lewis and Loftus
//                           Updated:    Bary W Pollack
//
//    Demonstrates events, listeners and rubberbanding
//    No use of inner classes -- uses extends + implements + this
//********************************************************************

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class RubberLines extends Applet 
                         implements MouseListener, MouseMotionListener {

    //-----------------------------------------------------------------
    //    Adds this class as a listener for all mouse related events
    //-----------------------------------------------------------------
    public void init() {
        addMouseListener(this);
        addMouseMotionListener(this);

        setBackground(Color.black);
        setSize(APPLET_WIDTH, APPLET_HEIGHT);
    }

    //-----------------------------------------------------------------
    //    Draws the current line from the intial mouse down point to
    //    the current position of the mouse.
    //-----------------------------------------------------------------
    public void paint(Graphics g) {
        g.setColor(Color.green);
        if (point1 != null && point2 != null)
            g.drawLine(point1.x, point1.y, point2.x, point2.y);
        showStatus("Click anywhere, and drag...");
    }

    //-----------------------------------------------------------------
    //    Captures the position at which the mouse is initially pushed
    //-----------------------------------------------------------------
    public void mousePressed(MouseEvent event) {
        point1 = event.getPoint();
    }

    //-----------------------------------------------------------------
    //    Gets the current position of the mouse as it is dragged and
    //    draws the line to create the rubberband effect
    //-----------------------------------------------------------------
    public void mouseDragged(MouseEvent event) {
        point2 = event.getPoint();
        repaint();
    }

    //-----------------------------------------------------------------
    //    Provide empty definitions for unused event methods
    //-----------------------------------------------------------------
    public void mouseClicked(MouseEvent event) {}
    public void mouseReleased(MouseEvent event) {}
    public void mouseEntered(MouseEvent event) {}
    public void mouseExited(MouseEvent event) {}
    public void mouseMoved(MouseEvent event) {}

    private final int APPLET_WIDTH  = 350;
    private final int APPLET_HEIGHT = 200;

    private Point point1 = null;
    private Point point2 = null;
}
