- java.lang.Object
- 
- java.awt.Polygon
 
- 
- All Implemented Interfaces:
- Shape,- Serializable
 
 public class Polygon extends Object implements Shape, Serializable ThePolygonclass encapsulates a description of a closed, two-dimensional region within a coordinate space. This region is bounded by an arbitrary number of line segments, each of which is one side of the polygon. Internally, a polygon comprises of a list of(x,y)coordinate pairs, where each pair defines a vertex of the polygon, and two successive pairs are the endpoints of a line that is a side of the polygon. The first and final pairs of(x,y)points are joined by a line segment that closes the polygon. ThisPolygonis defined with an even-odd winding rule. SeeWIND_EVEN_ODDfor a definition of the even-odd winding rule. This class's hit-testing methods, which include thecontains,intersectsandinsidemethods, use the insideness definition described in theShapeclass comments.- Since:
- 1.0
- See Also:
- Shape, Serialized Form
 
- 
- 
Method SummaryAll Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddPoint(int x, int y)Appends the specified coordinates to thisPolygon.booleancontains(double x, double y)Tests if the specified coordinates are inside the boundary of theShape, as described by the definition of insideness.booleancontains(double x, double y, double w, double h)Tests if the interior of theShapeentirely contains the specified rectangular area.booleancontains(int x, int y)Determines whether the specified coordinates are inside thisPolygon.booleancontains(Point2D p)Tests if a specifiedPoint2Dis inside the boundary of theShape, as described by the definition of insideness.booleancontains(Rectangle2D r)Tests if the interior of theShapeentirely contains the specifiedRectangle2D.booleancontains(Point p)Determines whether the specifiedPointis inside thisPolygon.RectanglegetBoundingBox()Deprecated.As of JDK version 1.1, replaced bygetBounds().RectanglegetBounds()Gets the bounding box of thisPolygon.Rectangle2DgetBounds2D()Returns a high precision and more accurate bounding box of theShapethan thegetBoundsmethod.PathIteratorgetPathIterator(AffineTransform at)Returns an iterator object that iterates along the boundary of thisPolygonand provides access to the geometry of the outline of thisPolygon.PathIteratorgetPathIterator(AffineTransform at, double flatness)Returns an iterator object that iterates along the boundary of theShapeand provides access to the geometry of the outline of theShape.booleaninside(int x, int y)Deprecated.As of JDK version 1.1, replaced bycontains(int, int).booleanintersects(double x, double y, double w, double h)Tests if the interior of theShapeintersects the interior of a specified rectangular area.booleanintersects(Rectangle2D r)Tests if the interior of theShapeintersects the interior of a specifiedRectangle2D.voidinvalidate()Invalidates or flushes any internally-cached data that depends on the vertex coordinates of thisPolygon.voidreset()Resets thisPolygonobject to an empty polygon.voidtranslate(int deltaX, int deltaY)Translates the vertices of thePolygonbydeltaXalong the x axis and bydeltaYalong the y axis.
 
- 
- 
- 
Field Detail- 
npointspublic int npoints The total number of points. The value ofnpointsrepresents the number of valid points in thisPolygonand might be less than the number of elements inxpointsorypoints. This value can be 0.- Since:
- 1.0
- See Also:
- addPoint(int, int)
 
 - 
xpointspublic int[] xpoints The array of X coordinates. The number of elements in this array might be more than the number of X coordinates in thisPolygon. The extra elements allow new points to be added to thisPolygonwithout re-creating this array. The value ofnpointsis equal to the number of valid points in thisPolygon.- Since:
- 1.0
- See Also:
- addPoint(int, int)
 
 - 
ypointspublic int[] ypoints The array of Y coordinates. The number of elements in this array might be more than the number of Y coordinates in thisPolygon. The extra elements allow new points to be added to thisPolygonwithout re-creating this array. The value ofnpointsis equal to the number of valid points in thisPolygon.- Since:
- 1.0
- See Also:
- addPoint(int, int)
 
 - 
boundsprotected Rectangle bounds The bounds of thisPolygon. This value can be null.- Since:
- 1.0
- See Also:
- getBoundingBox(),- getBounds()
 
 
- 
 - 
Constructor Detail- 
Polygonpublic Polygon() Creates an empty polygon.- Since:
- 1.0
 
 - 
Polygonpublic Polygon(int[] xpoints, int[] ypoints, int npoints)Constructs and initializes aPolygonfrom the specified parameters.- Parameters:
- xpoints- an array of X coordinates
- ypoints- an array of Y coordinates
- npoints- the total number of points in the- Polygon
- Throws:
- NegativeArraySizeException- if the value of- npointsis negative.
- IndexOutOfBoundsException- if- npointsis greater than the length of- xpointsor the length of- ypoints.
- NullPointerException- if- xpointsor- ypointsis- null.
- Since:
- 1.0
 
 
- 
 - 
Method Detail- 
resetpublic void reset() Resets thisPolygonobject to an empty polygon. The coordinate arrays and the data in them are left untouched but the number of points is reset to zero to mark the old vertex data as invalid and to start accumulating new vertex data at the beginning. All internally-cached data relating to the old vertices are discarded. Note that since the coordinate arrays from before the reset are reused, creating a new emptyPolygonmight be more memory efficient than resetting the current one if the number of vertices in the new polygon data is significantly smaller than the number of vertices in the data from before the reset.- Since:
- 1.4
- See Also:
- invalidate()
 
 - 
invalidatepublic void invalidate() Invalidates or flushes any internally-cached data that depends on the vertex coordinates of thisPolygon. This method should be called after any direct manipulation of the coordinates in thexpointsorypointsarrays to avoid inconsistent results from methods such asgetBoundsorcontainsthat might cache data from earlier computations relating to the vertex coordinates.- Since:
- 1.4
- See Also:
- getBounds()
 
 - 
translatepublic void translate(int deltaX, int deltaY)Translates the vertices of thePolygonbydeltaXalong the x axis and bydeltaYalong the y axis.- Parameters:
- deltaX- the amount to translate along the X axis
- deltaY- the amount to translate along the Y axis
- Since:
- 1.1
 
 - 
addPointpublic void addPoint(int x, int y)Appends the specified coordinates to thisPolygon.If an operation that calculates the bounding box of this Polygonhas already been performed, such asgetBoundsorcontains, then this method updates the bounding box.- Parameters:
- x- the specified X coordinate
- y- the specified Y coordinate
- Since:
- 1.0
- See Also:
- getBounds(),- contains(java.awt.Point)
 
 - 
getBoundspublic Rectangle getBounds() Gets the bounding box of thisPolygon. The bounding box is the smallestRectanglewhose sides are parallel to the x and y axes of the coordinate space, and can completely contain thePolygon.- Specified by:
- getBoundsin interface- Shape
- Returns:
- a Rectanglethat defines the bounds of thisPolygon.
- Since:
- 1.1
- See Also:
- Shape.getBounds2D()
 
 - 
getBoundingBox@Deprecated public Rectangle getBoundingBox() Deprecated.As of JDK version 1.1, replaced bygetBounds().Returns the bounds of thisPolygon.- Returns:
- the bounds of this Polygon.
- Since:
- 1.0
 
 - 
containspublic boolean contains(Point p) Determines whether the specifiedPointis inside thisPolygon.- Parameters:
- p- the specified- Pointto be tested
- Returns:
- trueif the- Polygoncontains the- Point;- falseotherwise.
- Since:
- 1.0
- See Also:
- contains(double, double)
 
 - 
containspublic boolean contains(int x, int y)Determines whether the specified coordinates are inside thisPolygon.- Parameters:
- x- the specified X coordinate to be tested
- y- the specified Y coordinate to be tested
- Returns:
- trueif this- Polygoncontains the specified coordinates- (x,y);- falseotherwise.
- Since:
- 1.1
- See Also:
- contains(double, double)
 
 - 
inside@Deprecated public boolean inside(int x, int y) Deprecated.As of JDK version 1.1, replaced bycontains(int, int).Determines whether the specified coordinates are contained in thisPolygon.- Parameters:
- x- the specified X coordinate to be tested
- y- the specified Y coordinate to be tested
- Returns:
- trueif this- Polygoncontains the specified coordinates- (x,y);- falseotherwise.
- Since:
- 1.0
- See Also:
- contains(double, double)
 
 - 
getBounds2Dpublic Rectangle2D getBounds2D() Returns a high precision and more accurate bounding box of theShapethan thegetBoundsmethod. Note that there is no guarantee that the returnedRectangle2Dis the smallest bounding box that encloses theShape, only that theShapelies entirely within the indicatedRectangle2D. The bounding box returned by this method is usually tighter than that returned by thegetBoundsmethod and never fails due to overflow problems since the return value can be an instance of theRectangle2Dthat uses double precision values to store the dimensions.Note that the definition of insideness can lead to situations where points on the defining outline of the shapemay not be considered contained in the returnedboundsobject, but only in cases where those points are also not considered contained in the originalshape.If a pointis inside theshapeaccording to thecontains(point)method, then it must be inside the returnedRectangle2Dbounds object according to thecontains(point)method of thebounds. Specifically:shape.contains(p)requiresbounds.contains(p)If a pointis not inside theshape, then it might still be contained in theboundsobject:bounds.contains(p)does not implyshape.contains(p)- Specified by:
- getBounds2Din interface- Shape
- Returns:
- an instance of Rectangle2Dthat is a high-precision bounding box of theShape.
- Since:
- 1.2
- See Also:
- Shape.getBounds()
 
 - 
containspublic boolean contains(double x, double y)Tests if the specified coordinates are inside the boundary of theShape, as described by the definition of insideness.
 - 
containspublic boolean contains(Point2D p) Tests if a specifiedPoint2Dis inside the boundary of theShape, as described by the definition of insideness.
 - 
intersectspublic boolean intersects(double x, double y, double w, double h)Tests if the interior of theShapeintersects the interior of a specified rectangular area. The rectangular area is considered to intersect theShapeif any point is contained in both the interior of theShapeand the specified rectangular area.The Shape.intersects()method allows aShapeimplementation to conservatively returntruewhen:- 
 there is a high probability that the rectangular area and the
 Shapeintersect, but
- the calculations to accurately determine this intersection are prohibitively expensive.
 Shapesthis method might returntrueeven though the rectangular area does not intersect theShape. TheAreaclass performs more accurate computations of geometric intersection than mostShapeobjects and therefore can be used if a more precise answer is required.- Specified by:
- intersectsin interface- Shape
- Parameters:
- x- the X coordinate of the upper-left corner of the specified rectangular area
- y- the Y coordinate of the upper-left corner of the specified rectangular area
- w- the width of the specified rectangular area
- h- the height of the specified rectangular area
- Returns:
- trueif the interior of the- Shapeand the interior of the rectangular area intersect, or are both highly likely to intersect and intersection calculations would be too expensive to perform;- falseotherwise.
- Since:
- 1.2
- See Also:
- Area
 
- 
 there is a high probability that the rectangular area and the
 
 - 
intersectspublic boolean intersects(Rectangle2D r) Tests if the interior of theShapeintersects the interior of a specifiedRectangle2D. TheShape.intersects()method allows aShapeimplementation to conservatively returntruewhen:- 
 there is a high probability that the Rectangle2Dand theShapeintersect, but
- the calculations to accurately determine this intersection are prohibitively expensive.
 Shapesthis method might returntrueeven though theRectangle2Ddoes not intersect theShape. TheAreaclass performs more accurate computations of geometric intersection than mostShapeobjects and therefore can be used if a more precise answer is required.- Specified by:
- intersectsin interface- Shape
- Parameters:
- r- the specified- Rectangle2D
- Returns:
- trueif the interior of the- Shapeand the interior of the specified- Rectangle2Dintersect, or are both highly likely to intersect and intersection calculations would be too expensive to perform;- falseotherwise.
- Since:
- 1.2
- See Also:
- Shape.intersects(double, double, double, double)
 
- 
 there is a high probability that the 
 - 
containspublic boolean contains(double x, double y, double w, double h)Tests if the interior of theShapeentirely contains the specified rectangular area. All coordinates that lie inside the rectangular area must lie within theShapefor the entire rectangular area to be considered contained within theShape.The Shape.contains()method allows aShapeimplementation to conservatively returnfalsewhen:- 
 the intersectmethod returnstrueand
- 
 the calculations to determine whether or not the
 Shapeentirely contains the rectangular area are prohibitively expensive.
 Shapesthis method might returnfalseeven though theShapecontains the rectangular area. TheAreaclass performs more accurate geometric computations than mostShapeobjects and therefore can be used if a more precise answer is required.- Specified by:
- containsin interface- Shape
- Parameters:
- x- the X coordinate of the upper-left corner of the specified rectangular area
- y- the Y coordinate of the upper-left corner of the specified rectangular area
- w- the width of the specified rectangular area
- h- the height of the specified rectangular area
- Returns:
- trueif the interior of the- Shapeentirely contains the specified rectangular area;- falseotherwise or, if the- Shapecontains the rectangular area and the- intersectsmethod returns- trueand the containment calculations would be too expensive to perform.
- Since:
- 1.2
- See Also:
- Area,- Shape.intersects(double, double, double, double)
 
- 
 the 
 - 
containspublic boolean contains(Rectangle2D r) Tests if the interior of theShapeentirely contains the specifiedRectangle2D. TheShape.contains()method allows aShapeimplementation to conservatively returnfalsewhen:- 
 the intersectmethod returnstrueand
- 
 the calculations to determine whether or not the
 Shapeentirely contains theRectangle2Dare prohibitively expensive.
 Shapesthis method might returnfalseeven though theShapecontains theRectangle2D. TheAreaclass performs more accurate geometric computations than mostShapeobjects and therefore can be used if a more precise answer is required.- Specified by:
- containsin interface- Shape
- Parameters:
- r- The specified- Rectangle2D
- Returns:
- trueif the interior of the- Shapeentirely contains the- Rectangle2D;- falseotherwise or, if the- Shapecontains the- Rectangle2Dand the- intersectsmethod returns- trueand the containment calculations would be too expensive to perform.
- Since:
- 1.2
- See Also:
- Shape.contains(double, double, double, double)
 
- 
 the 
 - 
getPathIteratorpublic PathIterator getPathIterator(AffineTransform at) Returns an iterator object that iterates along the boundary of thisPolygonand provides access to the geometry of the outline of thisPolygon. An optionalAffineTransformcan be specified so that the coordinates returned in the iteration are transformed accordingly.- Specified by:
- getPathIteratorin interface- Shape
- Parameters:
- at- an optional- AffineTransformto be applied to the coordinates as they are returned in the iteration, or- nullif untransformed coordinates are desired
- Returns:
- a PathIteratorobject that provides access to the geometry of thisPolygon.
- Since:
- 1.2
 
 - 
getPathIteratorpublic PathIterator getPathIterator(AffineTransform at, double flatness) Returns an iterator object that iterates along the boundary of theShapeand provides access to the geometry of the outline of theShape. Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types are returned by the iterator. Since polygons are already flat, theflatnessparameter is ignored. An optionalAffineTransformcan be specified in which case the coordinates returned in the iteration are transformed accordingly.- Specified by:
- getPathIteratorin interface- Shape
- Parameters:
- at- an optional- AffineTransformto be applied to the coordinates as they are returned in the iteration, or- nullif untransformed coordinates are desired
- flatness- the maximum amount that the control points for a given curve can vary from collinear before a subdivided curve is replaced by a straight line connecting the endpoints. Since polygons are already flat the- flatnessparameter is ignored.
- Returns:
- a PathIteratorobject that provides access to theShapeobject's geometry.
- Since:
- 1.2
 
 
- 
 
-