/** Polygon.java models a closed polygon...
 * @author Joel Adams, for Alice+Java.
 */
import java.awt.Color;

public class Polygon extends Shape {
  public Polygon(Color aColor, int numPoints) {
     super(aColor);
     		if (numPoints < 1) {
        throw new IllegalArgumentException("Polygon(): "
                              + "numPoints must be >= 1");
     }
     myPoints = new Point[numPoints];
  }
  
  public Polygon(Color aColor, Point [] pointArray) {
	  super(aColor);
	  myPoints = pointArray;
  }
  
  protected void setPoint(int i, double x, double y) {
     myPoints[i] = new Point(x,y);
  }

  public Point getPoint(int i) { return myPoints[i]; }

  public void draw(Plotter plot) {
     plot.setPenColor( super.getColor() );
     Point p0 = myPoints[0], p1 = null;
     for (int i = 1; i < myPoints.length; i++) {
        p1 = myPoints[i];
        plot.drawLine( p0.getX(), p0.getY(), 
                       p1.getX(), p1.getY() );
        p0 = p1;
     }
//     p1 = myPoints[0]; // draw last line, that closes the figure
     plot.drawLine( p0.getX(), p0.getY(),
    		        myPoints[0].getX(), myPoints[0].getY() );
//                    p1.getX(), p1.getY() );
  }

  private Point [] myPoints = null;
}
