/*
 * Course:       CSCI 215
 * Title:        Lab 1
 * Description:  DrawPad
 * Author:       Yu Chong
 * Copyright (c)2001 Yu Chong.  All Rights Reserved.
 */

import java.awt.Graphics;
import java.awt.Color;

/**
 * CLASS:    Line<BR>
 * PURPOSE:  This class represent one of the shapes that can be drawn -- Line<BR>
 * IMPORT:   java.awt.Graphics, java.awt.Color, DrawableShape, Point<BR>
 * EXPORT:   New Class|Line<BR>New Method| copyClone,
 *           hitTest, hitTestPointBox, paint<BR>
 */
public class Line extends DrawableShape
{
  /** <BR>
   *  PRE:  None<BR>
   *  POST: A new DrawableShape object is copy from the current instance and returned<BR>
   */
  public DrawableShape copyClone()
  {
    Line newLine = new Line();
    newLine.setColor(this.color);
    for(int i = 0; i < pointList.size(); i++)
    {
      newLine.addPoint((Point)pointList.elementAt(i));
    }
    return newLine;
  }

  /** <BR>
   *  PRE:  A valid Point Object<BR>
   *  POST: Check to see if the point hit the shape's PointBox if it has been
   *        selected<BR>
   */
  public boolean hitTestPointBox(Point p)
  {
    xPointChange = null;
    yPointChange = null;
    if(bSelected)
    {
      final int halfSquareSize = 3; // 6 pixels total;
      final int squareSize = halfSquareSize * 2;
      for(int i = 0; i < pointList.size(); i++)
      {
        Point currentPt = (Point)pointList.elementAt(i);
        if(p.x>=currentPt.x-halfSquareSize &&
           p.x<=currentPt.x+halfSquareSize &&
           p.y>=currentPt.y-halfSquareSize &&
           p.y<=currentPt.y+halfSquareSize)
        {
          xPointChange=currentPt;//save reference to point to move
          yPointChange=currentPt;
          return true;
        }
      }
    }
    return false;
  }

  /** <BR>
   *  PRE:  A valid Graphics object to paint on<BR>
   *  POST: Current object will be draw using the Graphics object<BR>
   */
  public void paint(Graphics g)
  {
    Point prevPt = new Point(0,0);
    Point nextPt;
    g.setColor(color);
    if(pointList.size() > 0)
    {
      prevPt = (Point)pointList.elementAt(0);
    }
    for(int i = 0; i < pointList.size(); i++)
    {
      nextPt = (Point)pointList.elementAt(i);
      g.drawLine(prevPt.x, prevPt.y, nextPt.x, nextPt.y);
      prevPt = nextPt;
    }
    if(bSelected) //draw square boxes for points
    {
      final int halfSquareSize = 3; // 6 pixels total;
      final int squareSize = halfSquareSize * 2;
      for(int i = 0; i < pointList.size(); i++)
      {
        Point pt = (Point)pointList.elementAt(i);
        g.setColor(Color.white);
        g.fillRect(pt.x-halfSquareSize, pt.y-halfSquareSize, squareSize, squareSize);
        g.setColor(Color.black);
        g.drawRect(pt.x-halfSquareSize, pt.y-halfSquareSize, squareSize, squareSize);
      }
    }
  }

  /** <BR>
   *  PRE:  A valid Point Object<BR>
   *  POST: Check to see if the point hit the shape<BR>
   */
  public boolean hitTest(Point p)
  {
    int leastX=0;
    int leastY=0;
    int largestX=0;
    int largestY=0;
    Point currentPt;
    if(pointList.size()>0)
    {
      currentPt = (Point)pointList.elementAt(0);
      leastX = currentPt.x;
      leastY = currentPt.y;
      largestX = currentPt.x;
      largestY = currentPt.y;
    }
    for(int i = 1; i < pointList.size(); i++)
    {
      currentPt = (Point)pointList.elementAt(i);
      if(currentPt.x < leastX)
      {
        leastX = currentPt.x;
      }
      else if(currentPt.x > largestX)
      {
        largestX = currentPt.x;
      }
      if(currentPt.y < leastY)
      {
        leastY = currentPt.y;
      }
      else if(currentPt.y > largestY)
      {
        largestY = currentPt.y;
      }
    }
    if(p.x <= largestX &&
       p.x >= leastX   &&
       p.y <= largestY &&
       p.y >= leastY)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
}