Polygon
A Polygon
is a collection of points used to create a series of line segments. Its primary purpose is to draw arbitrary shapes like triangles or pentagons. If the points are sufficiently close, you can create a curve. To display the Polygon
, call drawPolygon()
or fillPolygon()
.
Polygon Methods
VariablesThe collection of points maintained by Polygon
are stored in three variables:
- public int npoints
- The
npoints
variable stores the number of points. - public int xpoints[]
- The
xpoints
array holds the x component of each point. - public int ypoints[]
- The
ypoints
array holds the y component of each point.
You might expect the Polygon
class to use an array of points, rather than separate arrays of integers. More important, you might expect the instance variables to be private or protected, which would prevent them from being modified directly. Since the three instance variables are public, there is no guarantee that the array sizes are in sync with each other or with npoints
. To avoid trouble, always use addPoints()
to modify your polygons, and avoid modifying the instance variables directly. Constructors
- public Polygon ()
- This constructor creates an empty
Polygon
. - public Polygon (int xPoints[], int yPoints[], int numPoints)
- This constructor creates a
Polygon
that consists ofnumPoints
points. Those points are formed from the firstnumPoints
elements of thexPoints
andyPoints
arrays. If thexPoints
oryPoints
arrays are larger thannumPoints
, the additional entries are ignored. If thexPoints
oryPoints
arrays do not contain at leastnumPoints
elements, the constructor throws the run-time exceptionArrayIndexOutOfBoundsException
.
- public void addPoint (int x, int y)
- The
addPoint()
method adds the point (x
,y
) to thePolygon
as its last point. If you alter thexpoints
,ypoints
, andnpoints
instance variables directly,addPoint()
could add the new point at a place other than the end, or it could throw the run-time exceptionArrayIndexOutOfBoundsException
with a message showing the position at which it tried to add the point. Again, for safety, don't modify aPolygon
's instance variables yourself; always useaddPoint()
. - public Rectangle getBounds ()
public Rectangle getBoundingBox () - The
getBounds()
method returns thePolygon
's boundingRectangle
(i.e., the smallest rectangle that contains all the points within the polygon). Once you have the bounding box, it's easy to use methods likecopyArea()
to copy thePolygon
.getBoundingBox()
is the Java 1.0 name for this method. - public boolean contains (int x, int y)
public boolean inside (int x, int y) - The
contains()
method checks to see if the (x
,y
) point is within an area that would be filled if thePolygon
was drawn withGraphics.fillPolygon()
. A point may be within the bounding rectangle of the polygon, butcontains()
can still returnfalse
if not within a closed part of the polygon.inside()
is the Java 1.0 name for this method. - public boolean contains (Point p)
- The
contains()
method checks to see if the pointp
is within an area that would be filled if thePolygon
were drawn withGraphics.fillPolygon()
. - public void translate (int x, int y)
- The
translate()
method moves all thePolygon
's points by the amount (x
,y
). This allows you to alter the location of thePolygon
by shifting the points.