- All Implemented Interfaces:
Shape
,Serializable
Polygon
class 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. This Polygon
is defined with
an even-odd winding rule. See
WIND_EVEN_ODD
for a definition of the even-odd winding rule.
This class's hit-testing methods, which include the
contains
, intersects
and inside
methods, use the insideness definition described in the
Shape
class comments.- Since:
- 1.0
- See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addPoint
(int x, int y) Appends the specified coordinates to thisPolygon
.boolean
contains
(double x, double y) Tests if the specified coordinates are inside the boundary of theShape
, as described by the definition of insideness.boolean
contains
(double x, double y, double w, double h) Tests if the interior of theShape
entirely contains the specified rectangular area.boolean
contains
(int x, int y) Determines whether the specified coordinates are inside thisPolygon
.boolean
Tests if a specifiedPoint2D
is inside the boundary of theShape
, as described by the definition of insideness.boolean
Tests if the interior of theShape
entirely contains the specifiedRectangle2D
.boolean
Determines whether the specifiedPoint
is inside thisPolygon
.Deprecated.Gets the bounding box of thisPolygon
.Returns a high precision and more accurate bounding box of theShape
than thegetBounds
method.Returns an iterator object that iterates along the boundary of thisPolygon
and provides access to the geometry of the outline of thisPolygon
.getPathIterator
(AffineTransform at, double flatness) Returns an iterator object that iterates along the boundary of theShape
and provides access to the geometry of the outline of theShape
.boolean
inside
(int x, int y) Deprecated.As of JDK version 1.1, replaced bycontains(int, int)
.boolean
intersects
(double x, double y, double w, double h) Tests if the interior of theShape
intersects the interior of a specified rectangular area.boolean
Tests if the interior of theShape
intersects the interior of a specifiedRectangle2D
.void
Invalidates or flushes any internally-cached data that depends on the vertex coordinates of thisPolygon
.void
reset()
Resets thisPolygon
object to an empty polygon.void
translate
(int deltaX, int deltaY) Translates the vertices of thePolygon
bydeltaX
along the x axis and bydeltaY
along the y axis.
-
Field Details
-
npoints
-
xpoints
public int[] xpointsThe 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 thisPolygon
without re-creating this array. The value ofnpoints
is equal to the number of valid points in thisPolygon
.- Since:
- 1.0
- See Also:
-
ypoints
public int[] ypointsThe 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 thisPolygon
without re-creating this array. The value ofnpoints
is equal to the number of valid points in thisPolygon
.- Since:
- 1.0
- See Also:
-
bounds
-
-
Constructor Details
-
Polygon
public Polygon()Creates an empty polygon.- Since:
- 1.0
-
Polygon
public Polygon(int[] xpoints, int[] ypoints, int npoints) Constructs and initializes aPolygon
from the specified parameters.- Parameters:
xpoints
- an array of X coordinatesypoints
- an array of Y coordinatesnpoints
- the total number of points in thePolygon
- Throws:
NegativeArraySizeException
- if the value ofnpoints
is negative.IndexOutOfBoundsException
- ifnpoints
is greater than the length ofxpoints
or the length ofypoints
.NullPointerException
- ifxpoints
orypoints
isnull
.- Since:
- 1.0
-
-
Method Details
-
reset
public void reset()Resets thisPolygon
object 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 emptyPolygon
might 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
public 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 thexpoints
orypoints
arrays to avoid inconsistent results from methods such asgetBounds
orcontains
that might cache data from earlier computations relating to the vertex coordinates.- Since:
- 1.4
- See Also:
-
translate
public void translate(int deltaX, int deltaY) Translates the vertices of thePolygon
bydeltaX
along the x axis and bydeltaY
along the y axis.- Parameters:
deltaX
- the amount to translate along the X axisdeltaY
- the amount to translate along the Y axis- Since:
- 1.1
-
addPoint
public void addPoint(int x, int y) Appends the specified coordinates to thisPolygon
.If an operation that calculates the bounding box of this
Polygon
has already been performed, such asgetBounds
orcontains
, then this method updates the bounding box.- Parameters:
x
- the specified X coordinatey
- the specified Y coordinate- Since:
- 1.0
- See Also:
-
getBounds
Gets the bounding box of thisPolygon
. The bounding box is the smallestRectangle
whose sides are parallel to the x and y axes of the coordinate space, and can completely contain thePolygon
. -
getBoundingBox
Deprecated.As of JDK version 1.1, replaced bygetBounds()
.Returns the bounds of thisPolygon
.- Returns:
- the bounds of this
Polygon
. - Since:
- 1.0
-
contains
-
contains
public boolean contains(int x, int y) Determines whether the specified coordinates are inside thisPolygon
.- Parameters:
x
- the specified X coordinate to be testedy
- the specified Y coordinate to be tested- Returns:
true
if thisPolygon
contains the specified coordinates(x,y)
;false
otherwise.- Since:
- 1.1
- See Also:
-
inside
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 testedy
- the specified Y coordinate to be tested- Returns:
true
if thisPolygon
contains the specified coordinates(x,y)
;false
otherwise.- Since:
- 1.0
- See Also:
-
getBounds2D
Returns a high precision and more accurate bounding box of theShape
than thegetBounds
method. Note that there is no guarantee that the returnedRectangle2D
is the smallest bounding box that encloses theShape
, only that theShape
lies entirely within the indicatedRectangle2D
. The bounding box returned by this method is usually tighter than that returned by thegetBounds
method and never fails due to overflow problems since the return value can be an instance of theRectangle2D
that 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
shape
may not be considered contained in the returnedbounds
object, but only in cases where those points are also not considered contained in the originalshape
.If a
point
is inside theshape
according to thecontains(point)
method, then it must be inside the returnedRectangle2D
bounds object according to thecontains(point)
method of thebounds
. Specifically:shape.contains(p)
requiresbounds.contains(p)
If a
point
is not inside theshape
, then it might still be contained in thebounds
object:bounds.contains(p)
does not implyshape.contains(p)
- Specified by:
getBounds2D
in interfaceShape
- Returns:
- an instance of
Rectangle2D
that is a high-precision bounding box of theShape
. - Since:
- 1.2
- See Also:
-
contains
public boolean contains(double x, double y) Tests if the specified coordinates are inside the boundary of theShape
, as described by the definition of insideness. -
contains
Tests if a specifiedPoint2D
is inside the boundary of theShape
, as described by the definition of insideness. -
intersects
public boolean intersects(double x, double y, double w, double h) Tests if the interior of theShape
intersects the interior of a specified rectangular area. The rectangular area is considered to intersect theShape
if any point is contained in both the interior of theShape
and the specified rectangular area.The
Shape.intersects()
method allows aShape
implementation to conservatively returntrue
when:-
there is a high probability that the rectangular area and the
Shape
intersect, but - the calculations to accurately determine this intersection are prohibitively expensive.
Shapes
this method might returntrue
even though the rectangular area does not intersect theShape
. TheArea
class performs more accurate computations of geometric intersection than mostShape
objects and therefore can be used if a more precise answer is required.- Specified by:
intersects
in interfaceShape
- Parameters:
x
- the X coordinate of the upper-left corner of the specified rectangular areay
- the Y coordinate of the upper-left corner of the specified rectangular areaw
- the width of the specified rectangular areah
- the height of the specified rectangular area- Returns:
true
if the interior of theShape
and the interior of the rectangular area intersect, or are both highly likely to intersect and intersection calculations would be too expensive to perform;false
otherwise.- Since:
- 1.2
- See Also:
-
there is a high probability that the rectangular area and the
-
intersects
Tests if the interior of theShape
intersects the interior of a specifiedRectangle2D
. TheShape.intersects()
method allows aShape
implementation to conservatively returntrue
when:-
there is a high probability that the
Rectangle2D
and theShape
intersect, but - the calculations to accurately determine this intersection are prohibitively expensive.
Shapes
this method might returntrue
even though theRectangle2D
does not intersect theShape
. TheArea
class performs more accurate computations of geometric intersection than mostShape
objects and therefore can be used if a more precise answer is required.- Specified by:
intersects
in interfaceShape
- Parameters:
r
- the specifiedRectangle2D
- Returns:
true
if the interior of theShape
and the interior of the specifiedRectangle2D
intersect, or are both highly likely to intersect and intersection calculations would be too expensive to perform;false
otherwise.- Since:
- 1.2
- See Also:
-
there is a high probability that the
-
contains
public boolean contains(double x, double y, double w, double h) Tests if the interior of theShape
entirely contains the specified rectangular area. All coordinates that lie inside the rectangular area must lie within theShape
for the entire rectangular area to be considered contained within theShape
.The
Shape.contains()
method allows aShape
implementation to conservatively returnfalse
when:-
the
intersect
method returnstrue
and -
the calculations to determine whether or not the
Shape
entirely contains the rectangular area are prohibitively expensive.
Shapes
this method might returnfalse
even though theShape
contains the rectangular area. TheArea
class performs more accurate geometric computations than mostShape
objects and therefore can be used if a more precise answer is required.- Specified by:
contains
in interfaceShape
- Parameters:
x
- the X coordinate of the upper-left corner of the specified rectangular areay
- the Y coordinate of the upper-left corner of the specified rectangular areaw
- the width of the specified rectangular areah
- the height of the specified rectangular area- Returns:
true
if the interior of theShape
entirely contains the specified rectangular area;false
otherwise or, if theShape
contains the rectangular area and theintersects
method returnstrue
and the containment calculations would be too expensive to perform.- Since:
- 1.2
- See Also:
-
the
-
contains
Tests if the interior of theShape
entirely contains the specifiedRectangle2D
. TheShape.contains()
method allows aShape
implementation to conservatively returnfalse
when:-
the
intersect
method returnstrue
and -
the calculations to determine whether or not the
Shape
entirely contains theRectangle2D
are prohibitively expensive.
Shapes
this method might returnfalse
even though theShape
contains theRectangle2D
. TheArea
class performs more accurate geometric computations than mostShape
objects and therefore can be used if a more precise answer is required.- Specified by:
contains
in interfaceShape
- Parameters:
r
- The specifiedRectangle2D
- Returns:
true
if the interior of theShape
entirely contains theRectangle2D
;false
otherwise or, if theShape
contains theRectangle2D
and theintersects
method returnstrue
and the containment calculations would be too expensive to perform.- Since:
- 1.2
- See Also:
-
the
-
getPathIterator
Returns an iterator object that iterates along the boundary of thisPolygon
and provides access to the geometry of the outline of thisPolygon
. An optionalAffineTransform
can be specified so that the coordinates returned in the iteration are transformed accordingly.- Specified by:
getPathIterator
in interfaceShape
- Parameters:
at
- an optionalAffineTransform
to be applied to the coordinates as they are returned in the iteration, ornull
if untransformed coordinates are desired- Returns:
- a
PathIterator
object that provides access to the geometry of thisPolygon
. - Since:
- 1.2
-
getPathIterator
Returns an iterator object that iterates along the boundary of theShape
and 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, theflatness
parameter is ignored. An optionalAffineTransform
can be specified in which case the coordinates returned in the iteration are transformed accordingly.- Specified by:
getPathIterator
in interfaceShape
- Parameters:
at
- an optionalAffineTransform
to be applied to the coordinates as they are returned in the iteration, ornull
if untransformed coordinates are desiredflatness
- 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 theflatness
parameter is ignored.- Returns:
- a
PathIterator
object that provides access to theShape
object's geometry. - Since:
- 1.2
-
getBounds()
.