//********************************************************************
//    RubberLinesIC.java       Author:     Lewis and Loftus
//                             Updated:    Bary W Pollack
//
//    Demonstrates events, listeners and rubberbanding
//    Using inner classes
//********************************************************************

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class RubberLinesIC extends Applet {

    //-----------------------------------------------------------------
    //    Adds this class as a listener for all mouse related events
    //-----------------------------------------------------------------
    public void init() {
        addMouseListener(new MouseHandler());
        addMouseMotionListener(new MouseMotionHandler());

        setBackground(Color.gray);
        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.yellow);
        if (point1 != null && point2 != null)
            g.drawLine(point1.x, point1.y, point2.x, point2.y);
        showStatus("Click anywhere, and drag...");
    }

    class MouseHandler extends MouseAdapter {
        //-----------------------------------------------------------------
        //    Captures the position at which the mouse is initially pushed
        //-----------------------------------------------------------------
        public void mousePressed(MouseEvent event) {
            point1 = event.getPoint();
        }
    }

    class MouseMotionHandler extends MouseMotionAdapter {
        //-----------------------------------------------------------------
        //    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();
        }
    }

    private final int APPLET_WIDTH  = 350;
    private final int APPLET_HEIGHT = 200;

    private Point point1 = null;
    private Point point2 = null;
}
