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

  /** <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)
  {
    int size = pointList.size();
    int xPt[] = new int[size];
    int yPt[] = new int[size];
    for(int i = 0; i < size; i++)
    {
      xPt[i]=((Point)pointList.elementAt(i)).x;
      yPt[i]=((Point)pointList.elementAt(i)).y;
    }
    java.awt.Polygon tmpPoly = new java.awt.Polygon(xPt, yPt, size);
    g.setColor(color);
    if(bFill)
    {
      g.fillPolygon(tmpPoly);
    }
    else
    {
      g.drawPolygon(tmpPoly);
    }
    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 size = pointList.size();
    int xPt[] = new int[size];
    int yPt[] = new int[size];
    for(int i = 0; i < size; i++)
    {
      xPt[i]=((Point)pointList.elementAt(i)).x;
      yPt[i]=((Point)pointList.elementAt(i)).y;
    }
    java.awt.Polygon tmpPoly = new java.awt.Polygon(xPt, yPt, size);
    return tmpPoly.contains(p.x, p.y);
  }
}