import objectdraw.*;

/*
 * This program shows how to draw cross hairs focussing on 
 * the cursor when the mouse is dragged, as in a drawing program.
 *
 * It illustrates accessor methods on the canvas and points.
 * 
 * This version also illustrates building a string from numbers
 * and using moveTo with an offset from a point by showing
 * the coordinates of the cursor next to it.
 */

public class CrossHairs extends WindowController {    

  // the crosshair lines
  private Line vert, horiz;

  // the label next to the cross hairs
  private Text label;

  // displacement from the cursor to the coordinates label
  private static final int LABEL_X_OFFSET = 10;
  private static final int LABEL_Y_OFFSET = 5;

  /*
   * Load and display a picture as background
   */
  public void begin() {
    new VisibleImage(getImage("background.jpg"), 0, 0, canvas);
  }

  /*
   * Display the crosshairs as soon as the mouse button is pressed.
   */
  public void onMousePress(Location point) {
    vert = new Line(point.getX(), 0, point.getX(), canvas.getHeight(), canvas);
    horiz = new Line(0, point.getY(), canvas.getWidth(), point.getY(), canvas);

    label = new Text(point.getX() + ", " + point.getY(), 
      point.getX() + LABEL_X_OFFSET, 
      point.getY() + LABEL_Y_OFFSET, 
      canvas);
  }

  /*
   * Move the crosshairs as the mouse is dragged.
   */
  public void onMouseDrag(Location point) {

    // refocus the lines over the cursor
    vert.setEndPoints(point.getX(), 0, point.getX(), canvas.getHeight());                
    horiz.setEndPoints(0, point.getY(), canvas.getWidth(), point.getY());

    // create the new text for the label, and also move it.
    label.setText(point.getX() + ", " + point.getY());
    label.moveTo(point.getX() + LABEL_X_OFFSET, 
      point.getY() + LABEL_Y_OFFSET);
  }

  /*
   * Remove the crosshairs when the mouse is released.
   */
  public void onMouseRelease(Location point){
    vert.removeFromCanvas();
    horiz.removeFromCanvas();
    label.removeFromCanvas();
  }
}
