import objectdraw.*;

import java.awt.*;

/**
 * Program to demonstrate dragging different kinds
 * of objects by using an interface.
 */
public class CircleSquare extends WindowController {
  public static final int SQUARE_LEFT = 100;
  public static final int SQUARE_TOP = 100;
  public static final int SIZE = 50;
  public static final int CIRCLE_LEFT = 200;
  public static final int CIRCLE_TOP = 200;

  // The object being dragged
  private DrawableInterface draggedItem;

  // Are we dragging anything?
  private boolean dragging;

  private Location lastPoint;

  // Two objects to drag around
  private FilledRect blueItem;
  private FilledOval redItem;

  public void begin() {
    blueItem = new FilledRect(SQUARE_LEFT, SQUARE_TOP, SIZE, SIZE, canvas);
    blueItem.setColor(Color.blue);
    redItem = new FilledOval(CIRCLE_LEFT, CIRCLE_TOP, SIZE, SIZE, canvas);
    redItem.setColor(Color.red);
  }

  public void onMousePress(Location point) {
    lastPoint = point;

    if (blueItem.contains(point)) {
      draggedItem = blueItem;
      dragging = true;
    } else if (redItem.contains(point)) {
      draggedItem = redItem;
      dragging = true;
    } else {
      dragging = false;
    }
  }

  public void onMouseDrag(Location point) {
    if (dragging) {
      draggedItem.move(point.getX() - lastPoint.getX(),
        point.getY() - lastPoint.getY());
      lastPoint = point;
    }
  }

}
