acm.graphics
Class GPolygon

java.lang.Object
  extended by acm.graphics.GObject
      extended by acm.graphics.GPolygon

public class GPolygon
extends GObject
implements GFillable, GScalable

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


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.
 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.
 boolean isFilled()
Returns whether this object is filled.
 void recenter()
Recalculates the vertices of the polygon so that they are positioned relative to the geometric center of the object.
 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.
 
Inherited Method Summary
voidaddMouseListener(MouseListener listener)
Adds a mouse listener to this graphical object.
voidaddMouseMotionListener(MouseMotionListener listener)
Adds a mouse motion listener to this graphical object.
booleancontains(GPoint pt)
Checks to see whether a point is inside the object.
ColorgetColor()
Returns the color used to display this object.
doublegetHeight()
Returns the height of this object, which is defined to be the height of the bounding box.
GPointgetLocation()
Returns the location of this object as a GPoint.
GDimensiongetSize()
Returns the size of the bounding box for this object.
doublegetWidth()
Returns the width of this object, which is defined to be the width of the bounding box.
doublegetX()
Returns the x-coordinate of the object.
doublegetY()
Returns the y-coordinate of the object.
booleanisVisible()
Checks to see whether this object is visible.
voidmove(double dx, double dy)
Moves the object on the screen using the displacements dx and dy.
voidmovePolar(double r, double theta)
Moves the object using displacements given in polar coordinates.
voidremoveMouseListener(MouseListener listener)
Removes a mouse listener from this graphical object.
voidremoveMouseMotionListener(MouseMotionListener listener)
Removes a mouse motion listener from this graphical object.
voidsendBackward()
Moves this object one step toward the back in the z dimension.
voidsendForward()
Moves this object one step toward the front in the z dimension.
voidsendToBack()
Moves this object to the back of the display in the z dimension.
voidsendToFront()
Moves this object to the front of the display in the z dimension.
voidsetColor(Color color)
Sets the color used to display this object.
voidsetLocation(GPoint pt)
Sets the location of this object to the specified point.
voidsetLocation(double x, double y)
Sets the location of this object to the point (x, y).
voidsetVisible(boolean visible)
Sets whether this object is visible.
 

Constructor Detail

public GPolygon()

Constructs a new empty polygon at the origin.

 
Usage: GPolygon gpoly = new GPolygon(); 
 


public GPolygon(double x, double y)

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

 
Usage: GPolygon gpoly = new GPolygon(x, y); 
Parameters: 
 The x-coordinate of the origin of the polygon
 The y-coordinate of the origin of the polygon
 


public GPolygon(GPoint[] points)

Constructs a new polygon from the specified array of GPoint objects. The polygon is automatically marked as complete.

 
Usage: GPolygon gpoly = new GPolygon(points); 
Parameter: 
points  An array of GPoint objects specifying the vertices
 

Method Detail

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.

 
Usage: gpoly.addArc(arcWidth, arcHeight, start, sweep); 
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
 


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.

 
Usage: gpoly.addEdge(dx, dy); 
Parameters: 
dx  The x displacement through which the edge moves
dy  The y displacement through which the edge moves
 


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.

 
Usage: gpoly.addPolarEdge(r, theta); 
Parameters: 
 The length of the edge
theta  The angle at which the edge extends measured in degrees
 


public void addVertex(double x, double y)

Adds a vertex at (x, y) relative to the polygon origin.

 
Usage: gpoly.addVertex(x, y); 
Parameters: 
 The x-coordinate of the vertex relative to the polygon origin
 The y-coordinate of the vertex relative to the polygon origin
 


public boolean contains(double x, double y)

Checks to see whether a point is inside the object.

 
Usage: if (gpoly.contains(x, y)) . . . 
Parameters: 
 The x-coordinate of the point being tested
 The y-coordinate of the point being tested
Returns: true if the point (xy) is inside the object, and false otherwise
 


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.

 
Usage: GRectangle bounds = gpoly.getBounds(); 
Returns: The bounding box for this object
 

Specified by:
getBounds in class GObject

public GPoint getCurrentPoint()

Returns the coordinates of the last vertex added to the polygon, or null if the polygon is empty.

 
Usage: GPoint vertex = gpoly.getCurrentPoint(); 
Returns: The last vertex added to the polygon, or null if empty
 


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.

 
Usage: Color color = gobj.getFillColor(); 
Returns: The color used to display the filled region of this object
 

Specified by:
getFillColor in interface GFillable

public boolean isFilled()

Returns whether this object is filled.

 
Usage: if (gobj.isFilled()) . . . 
Returns: The color used to display the object
 

Specified by:
isFilled in interface GFillable

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.

 
Usage: gpoly.recenter(); 
 


public void rotate(double theta)

Rotates the polygon around its origin by the angle theta, measured in degrees.

 
Usage: gpoly.rotate(theta); 
Parameter: 
theta  The angle of rotation in degrees counterclockwise
 


public final void scale(double sf)

Scales the object on the screen by the scale factor sf, which applies in both dimensions.

 
Usage: gobj.scale(sf); 
Parameter: 
sf  The factor used to scale all coordinates in both dimensions
 

Specified by:
scale in interface GScalable

public void scale(double sx, double sy)

Scales the polygon by the scale factors sx and sy.

 
Usage: gpoly.scale(sx, sy); 
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
 

Specified by:
scale in interface GScalable

public void setFillColor(Color color)

Sets the color used to display the filled region of this object.

 
Usage: gobj.setFillColor(color); 
Parameter: 
color  The color used to display the filled region of this object
 

Specified by:
setFillColor in interface GFillable

public void setFilled(boolean fill)

Sets whether this object is filled.

 
Usage: gobj.setFilled(fill); 
Parameter: 
fill  true if the object should be filled, false for an outline
 

Specified by:
setFilled in interface GFillable