
import java.awt.*;        // Java visual tools
import java.awt.event.*;  // Java visual events
import java.applet.*;     // Java applet classes

// This class demonstrates additional mouse events, and using
// the paint method to do all drawing for an applet.

// The applet draws a rectangle in the middle of the applet.
// The user can move the rectangle by dragging inside of it,
// or resize it by dragging an edge.

public class Rectangles extends Applet
                        implements MouseMotionListener,
                                   MouseListener {

  // These state varibales keep track of the current state
  // of the rectangle.

  private int top;
  private int left;
  private int height;
  private int width;

  // These state variables are used to keep track of the last
  // mouse location for dragging.

  private int lastX;
  private int lastY;

  // Thes stae variables are used to keep track of what kind
  // of change the user is currently making.

  private boolean moving = false;
  private boolean resizeLeft = false;
  private boolean resizeRight = false;
  private boolean resizeTop = false;
  private boolean resizeBottom = false;

  public void init() {

    // Initially, we draw a rectangle in the center of the
    // applet, at half the height and width of the applet.
    // Tis is done by using the getWidth and getHeight
    // methods of the applet class.

    int w = getWidth();
    int h = getHeight();
    height = (int)(h/2);
    width = (int)(w/2);
    top = (int)(h/4);
    left = (int)(w/4);

    addMouseListener(this);
    addMouseMotionListener(this);
    }

  // When the mouse is dragged, we check to see if a change is
  // currently being made. We then alter one or more properties
  // of the rectangle in response to the change.

  public void mouseDragged(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    int deltaX = x - lastX;
    int deltaY = y - lastY;

    if (moving) { // Moving rectangle, so change left and top position.
      left = left + deltaX;
      top = top + deltaY;
      repaint();
      }
    if (resizeLeft) { // Moving left edge, so change left and width.
      left = left + deltaX;
      width = width - deltaX;
      repaint();
      }
    if (resizeRight) { // Moving left edge, so change width.
      width = width + deltaX;
      repaint();
      }
    if (resizeTop) { // Moving top edge, so change top and height.
      top = top + deltaY;
      height = height - deltaY;
      repaint();
      }
    if (resizeBottom) { // Moving bootom edge, so change height.
      height = height + deltaY;
      repaint();
      }
    lastX = x;
    lastY = y;   
    }

  public void mouseMoved(MouseEvent e) {}

  // When the mouse is pressed (to begin a drag), check whether the
  // mouse is inside the rectangle or on one of the edges, and
  // set the state accordingly.

  public void mousePressed(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    lastX = x;
    lastY = y;

    // First check whether the mouse is in the area of the rectangle.
    // Note that we are leaving about 3 pixels of extra space around
    // the rectangle so the user does not have to click exactly on the
    // edge (good UI).

    if (x >= left-3 && x <= left+width+3 &&
        y >= top-3 && y <= top+height+3) {

      // Now check to see if the mouse is in the vicinity of an edge.

      if (x <= left+3) resizeLeft = true;
      else if (x >= left+width-3) resizeRight = true;
      else if (y <= top+3) resizeTop = true;
      else if (y >= top+height-3) resizeBottom = true;
      else moving = true;
      }
    }

  // When the mouse is released, stop whatever dragging process
  // that we have begun.

  public void mouseReleased(MouseEvent e) {
    moving = false;
    resizeLeft = false;
    resizeRight = false;
    resizeTop = false;
    resizeBottom = false;
    }

  public void mouseClicked(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}

  // The paint method draws a red rectangle based on the current
  // top, left, width, and height state variables. Note that this
  // is the only drawing done by this applet.

  public void paint(Graphics g) {

    // To draw in a particular color, set the graphics color before
    // drawing.

    g.setColor(Color.red);

    // To give the illusion of thickness, we actually draw 3 rectangles,
    // one inside the other.

    g.drawRect(left-1, top-1, width+2, height+2);
    g.drawRect(left, top, width, height);
    g.drawRect(left+1, top+1, width-2, height-2);
    }

  }

