acm.graphics
Class GPolygon

java.lang.Object
  extended by acm.graphics.GObject
      extended by acm.graphics.GPolygon
All Implemented Interfaces:
GFillable, GScalable, Serializable, Cloneable

public class GPolygon
extends GObject
implements GFillable, GScalable

The GPolygon class is a graphical object whose appearance consists of a polygon.

See Also:
Serialized Form

Constructor Summary
GPolygon()
          Constructs a new empty polygon at the origin.
GPolygon(double x, double y)
          Constructs a new empty polygon at (x, y).
GPolygon(GPoint[] points)
          Constructs a new polygon from the specified array of GPoint objects.
 
Method Summary
 void addArc(double arcWidth, double arcHeight, double start, double sweep)
          Adds a series of edges to the polygon that simulates the arc specified by the parameters.
 void addEdge(double dx, double dy)
          Adds an edge to the polygon whose components are given by the displacements dx and dy from the last vertex.
 void addPolarEdge(double r, double theta)
          Adds an edge to the polygon specified in polar coordinates.
 void addVertex(double x, double y)
          Adds a vertex at (x, y) relative to the polygon origin.
protected  void clear()
          Calling this method deletes all vertices from the polygon and resets the scale and rotation factors to the their default values.
 Object clone()
          Overrides clone in Object to make sure that the vertex list is copied rather than shared.
 boolean contains(double x, double y)
          Checks to see whether a point is inside the object.
 GRectangle getBounds()
          Returns the bounding box of this object, which is defined to be the smallest rectangle that covers everything drawn by the figure.
 GPoint getCurrentPoint()
          Returns the coordinates of the last vertex added to the polygon, or null if the polygon is empty.
 Color getFillColor()
          Returns the color used to display the filled region of this object.
protected  Polygon getPolygon()
          Returns an AWT Polygon whose points are as close as possible to the ones in this GPolygon.
 boolean isFilled()
          Returns whether this object is filled.
protected  void markAsComplete()
          Calling this method makes it illegal to add or remove vertices from the polygon.
 void paint(Graphics g)
          Implements the paint operation for this graphical object.
 void recenter()
          Recalculates the vertices of the polygon so that they are positioned relative to the geometric center of the object.
protected  void repaint()
          Overrides repaint in GObject to invalidate the cached polygon.
 void rotate(double theta)
          Rotates the polygon around its origin by the angle theta, measured in degrees.
 void scale(double sf)
          Scales the object on the screen by the scale factor sf, which applies in both dimensions.
 void scale(double sx, double sy)
          Scales the polygon by the scale factors sx and sy.
 void setFillColor(Color color)
          Sets the color used to display the filled region of this object.
 void setFilled(boolean fill)
          Sets whether this object is filled.
 
Methods inherited from class acm.graphics.GObject
addActionListener, addMouseListener, addMouseMotionListener, areMouseListenersEnabled, colorName, contains, fireActionEvent, fireActionEvent, fireMouseListeners, getColor, getComponent, getHeight, getLocation, getObjectColor, getParent, getSize, getWidth, getX, getY, isVisible, move, movePolar, paintObject, paramString, pause, removeActionListener, removeMouseListener, removeMouseMotionListener, sendBackward, sendForward, sendToBack, sendToFront, setColor, setLocation, setLocation, setParent, setVisible, start, start, toString, updateEnabledList
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GPolygon

public GPolygon()
Constructs a new empty polygon at the origin.


GPolygon

public GPolygon(double x,
                double y)
Constructs a new empty polygon at (x, y).

Parameters:
x - The x-coordinate of the origin of the polygon
y - The y-coordinate of the origin of the polygon

GPolygon

public GPolygon(GPoint[] points)
Constructs a new polygon from the specified array of GPoint objects. The polygon is automatically marked as complete.

Parameters:
points - An array of GPoint objects specifying the vertices
Method Detail

addArc

public void addArc(double arcWidth,
                   double arcHeight,
                   double start,
                   double sweep)
Adds a series of edges to the polygon that simulates the arc specified by the parameters. The x and y parameters for the arc bounding box are computed implicitly by figuring out what values would place the current vertex at the starting position.

Parameters:
arcWidth - The width of the oval from which the arc is taken
arcHeight - The height of the oval from which the arc is taken
start - The angle at which the arc begins
sweep - The extent of the arc

addEdge

public void addEdge(double dx,
                    double dy)
Adds an edge to the polygon whose components are given by the displacements dx and dy from the last vertex.

Parameters:
dx - The x displacement through which the edge moves
dy - The y displacement through which the edge moves

addPolarEdge

public final void addPolarEdge(double r,
                               double theta)
Adds an edge to the polygon specified in polar coordinates. The length of the edge is given by r, and the edge extends in direction theta, measured in degrees counterclockwise from the +x axis.

Parameters:
r - The length of the edge
theta - The angle at which the edge extends measured in degrees

addVertex

public void addVertex(double x,
                      double y)
Adds a vertex at (x, y) relative to the polygon origin.

Parameters:
x - The x-coordinate of the vertex relative to the polygon origin
y - The y-coordinate of the vertex relative to the polygon origin

clear

protected void clear()
Calling this method deletes all vertices from the polygon and resets the scale and rotation factors to the their default values. Subclasses can use this method to reconstruct a polygon.


clone

public Object clone()
Overrides clone in Object to make sure that the vertex list is copied rather than shared.

Overrides:
clone in class Object

contains

public boolean contains(double x,
                        double y)
Checks to see whether a point is inside the object.

Overrides:
contains in class GObject
Parameters:
x - The x-coordinate of the point being tested
y - The y-coordinate of the point being tested
Returns:
true if the point (xy) is inside the object, and false otherwise

getBounds

public GRectangle getBounds()
Returns the bounding box of this object, which is defined to be the smallest rectangle that covers everything drawn by the figure.

Specified by:
getBounds in class GObject
Returns:
The bounding box for this object

getCurrentPoint

public GPoint getCurrentPoint()
Returns the coordinates of the last vertex added to the polygon, or null if the polygon is empty.

Returns:
The last vertex added to the polygon, or null if empty

getFillColor

public Color getFillColor()
Returns the color used to display the filled region of this object. If none has been set, getFillColor returns the color of the object.

Specified by:
getFillColor in interface GFillable
Returns:
The color used to display the filled region of this object

getPolygon

protected Polygon getPolygon()
Returns an AWT Polygon whose points are as close as possible to the ones in this GPolygon.

Returns:
An AWT polygon corresponding to this object

isFilled

public boolean isFilled()
Returns whether this object is filled.

Specified by:
isFilled in interface GFillable
Returns:
The color used to display the object

markAsComplete

protected void markAsComplete()
Calling this method makes it illegal to add or remove vertices from the polygon. Subclasses can invoke this method to protect the integrity of the structure from changes by the client.


paint

public void paint(Graphics g)
Implements the paint operation for this graphical object. This method is not called directly by clients.

Specified by:
paint in class GObject
Parameters:
g - The graphics context into which the painting is done

recenter

public void recenter()
Recalculates the vertices of the polygon so that they are positioned relative to the geometric center of the object. This method allows clients to take a polygon drawn using mouse clicks on the screen and then to reformulate it so that it can be displayed relative to its center.


repaint

protected void repaint()
Overrides repaint in GObject to invalidate the cached polygon.

Overrides:
repaint in class GObject

rotate

public void rotate(double theta)
Rotates the polygon around its origin by the angle theta, measured in degrees.

Parameters:
theta - The angle of rotation in degrees counterclockwise

scale

public final void scale(double sf)
Scales the object on the screen by the scale factor sf, which applies in both dimensions.

Specified by:
scale in interface GScalable
Parameters:
sf - The factor used to scale all coordinates in both dimensions

scale

public void scale(double sx,
                  double sy)
Scales the polygon by the scale factors sx and sy.

Specified by:
scale in interface GScalable
Parameters:
sx - The factor used to scale all coordinates in the x direction
sy - The factor used to scale all coordinates in the y direction

setFillColor

public void setFillColor(Color color)
Sets the color used to display the filled region of this object.

Specified by:
setFillColor in interface GFillable
Parameters:
color - The color used to display the filled region of this object

setFilled

public void setFilled(boolean fill)
Sets whether this object is filled.

Specified by:
setFilled in interface GFillable
Parameters:
fill - true if the object should be filled, false for an outline