APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.awt

public Class Polygon

extends Object
implements Shape, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.awt.Shape
Known Direct Subclasses
javax.swing.text.html.Map.PolygonRegionContainment
Imports
java.awt.geom.AffineTransform, .PathIterator, .Point2D, .Rectangle2D, java.io.Serial, java.util.Arrays, sun.awt.geom.Crossings

The 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.
Authors
Sami Shaio, Herb Jellinek
Since
1.0
See Also
Shape

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
protected Rectangle
bounds

The bounds of this Polygon.

private static final int
public int
npoints

The total number of points.

private static final long
serialVersionUID

Use serialVersionUID from JDK 1.1 for interoperability.

public int[]
xpoints

The array of X coordinates.

public int[]
ypoints

The array of Y coordinates.

Constructor Summary

AccessConstructor and Description
public
Polygon()

Creates an empty polygon.

public
Polygon(int[]
an array of X coordinates
xpoints
,
int[]
an array of Y coordinates
ypoints
,
int
the total number of points in the Polygon
npoints
)

Constructs and initializes a Polygon from the specified parameters.

Method Summary

Modifier and TypeMethod and Description
public void
addPoint(int
the specified X coordinate
x
,
int
the specified Y coordinate
y
)

Appends the specified coordinates to this Polygon.

pack-priv void
calculateBounds(int[] xpoints, int[] ypoints, int npoints)

public boolean

Returns:

true if the Polygon contains the Point; false otherwise.
contains
(Point
the specified Point to be tested
p
)

Determines whether the specified Point is inside this Polygon.

public boolean

Returns:

true if this Polygon contains the specified coordinates (x,y); false otherwise.
contains
(int
the specified X coordinate to be tested
x
,
int
the specified Y coordinate to be tested
y
)

Determines whether the specified coordinates are inside this Polygon.

public boolean
contains(double x, double y)

Implements java.awt.Shape.contains.

Tests if the specified coordinates are inside the boundary of the Shape, as described by the definition of insideness.

public boolean
contains(Point2D p)

Implements java.awt.Shape.contains.

Tests if a specified Point2D is inside the boundary of the Shape, as described by the definition of insideness.

public boolean
contains(double x, double y, double w, double h)

Implements java.awt.Shape.contains.

Tests if the interior of the Shape entirely contains the specified rectangular area.

public boolean
contains(Rectangle2D r)

Implements java.awt.Shape.contains.

Tests if the interior of the Shape entirely contains the specified Rectangle2D.

public Rectangle

Returns:

the bounds of this Polygon.
getBoundingBox
()
Deprecated As of JDK version 1.1, replaced by getBounds().

Returns the bounds of this Polygon.

public Rectangle

Returns:

a Rectangle that defines the bounds of this Polygon.
getBounds
()

Implements java.awt.Shape.getBounds.

Gets the bounding box of this Polygon.

public Rectangle2D
getBounds2D()

Implements java.awt.Shape.getBounds2D.

Returns a high precision and more accurate bounding box of the Shape than the getBounds method.

private Crossings
getCrossings(double xlo, double ylo, double xhi, double yhi)

public PathIterator

Returns:

a PathIterator object that provides access to the geometry of this Polygon.
getPathIterator
(AffineTransform
an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired
at
)

Implements java.awt.Shape.getPathIterator.

Returns an iterator object that iterates along the boundary of this Polygon and provides access to the geometry of the outline of this Polygon.

public PathIterator

Returns:

a PathIterator object that provides access to the Shape object's geometry.
getPathIterator
(AffineTransform
an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired
at
,
double
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 flatness parameter is ignored.
flatness
)

Implements java.awt.Shape.getPathIterator.

Returns an iterator object that iterates along the boundary of the Shape and provides access to the geometry of the outline of the Shape.

public boolean

Returns:

true if this Polygon contains the specified coordinates (x,y); false otherwise.
inside
(int
the specified X coordinate to be tested
x
,
int
the specified Y coordinate to be tested
y
)
Deprecated As of JDK version 1.1, replaced by contains(int, int).

Determines whether the specified coordinates are contained in this Polygon.

public boolean
intersects(double x, double y, double w, double h)

Implements java.awt.Shape.intersects.

Tests if the interior of the Shape intersects the interior of a specified rectangular area.

public boolean
intersects(Rectangle2D r)

Implements java.awt.Shape.intersects.

Tests if the interior of the Shape intersects the interior of a specified Rectangle2D.

public void
invalidate()

Invalidates or flushes any internally-cached data that depends on the vertex coordinates of this Polygon.

public void
reset()

Resets this Polygon object to an empty polygon.

public void
translate(int
the amount to translate along the X axis
deltaX
,
int
the amount to translate along the Y axis
deltaY
)

Translates the vertices of the Polygon by deltaX along the x axis and by deltaY along the y axis.

pack-priv void
updateBounds(int x, int y)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

boundsback to summary
protected Rectangle bounds

The bounds of this Polygon. This value can be null.

Since
1.0
See Also
getBoundingBox(), getBounds()
MIN_LENGTHback to summary
private static final int MIN_LENGTH
npointsback to summary
public int npoints

The total number of points. The value of npoints represents the number of valid points in this Polygon and might be less than the number of elements in xpoints or ypoints. This value can be 0.

Since
1.0
See Also
addPoint(int, int)
serialVersionUIDback to summary
private static final long serialVersionUID

Use serialVersionUID from JDK 1.1 for interoperability.

Annotations
@Serial
xpointsback to summary
public int[] xpoints

The array of X coordinates. The number of elements in this array might be more than the number of X coordinates in this Polygon. The extra elements allow new points to be added to this Polygon without re-creating this array. The value of npoints is equal to the number of valid points in this Polygon.

Since
1.0
See Also
addPoint(int, int)
ypointsback to summary
public int[] ypoints

The array of Y coordinates. The number of elements in this array might be more than the number of Y coordinates in this Polygon. The extra elements allow new points to be added to this Polygon without re-creating this array. The value of npoints is equal to the number of valid points in this Polygon.

Since
1.0
See Also
addPoint(int, int)

Constructor Detail

Polygonback to summary
public Polygon()

Creates an empty polygon.

Since
1.0
Polygonback to summary
public Polygon(int[] xpoints, int[] ypoints, int npoints)

Constructs and initializes a Polygon from the specified parameters.

Parameters
xpoints:int[]

an array of X coordinates

ypoints:int[]

an array of Y coordinates

npoints:int

the total number of points in the Polygon

Exceptions
NegativeArraySizeException:
if the value of npoints is negative.
IndexOutOfBoundsException:
if npoints is greater than the length of xpoints or the length of ypoints.
NullPointerException:
if xpoints or ypoints is null.
Since
1.0

Method Detail

addPointback to summary
public void addPoint(int x, int y)

Appends the specified coordinates to this Polygon.

If an operation that calculates the bounding box of this Polygon has already been performed, such as getBounds or contains, then this method updates the bounding box.

Parameters
x:int

the specified X coordinate

y:int

the specified Y coordinate

Since
1.0
See Also
java.awt.Polygon#getBounds, java.awt.Polygon#contains
calculateBoundsback to summary
pack-priv void calculateBounds(int[] xpoints, int[] ypoints, int npoints)
containsback to summary
public boolean contains(Point p)

Determines whether the specified Point is inside this Polygon.

Parameters
p:Point

the specified Point to be tested

Returns:boolean

true if the Polygon contains the Point; false otherwise.

Since
1.0
See Also
contains(double, double)
containsback to summary
public boolean contains(int x, int y)

Determines whether the specified coordinates are inside this Polygon.

Parameters
x:int

the specified X coordinate to be tested

y:int

the specified Y coordinate to be tested

Returns:boolean

true if this Polygon contains the specified coordinates (x,y); false otherwise.

Since
1.1
See Also
contains(double, double)
containsback to summary
public boolean contains(double x, double y)

Implements java.awt.Shape.contains.

Doc from java.awt.Shape.contains.

Tests if the specified coordinates are inside the boundary of the Shape, as described by the definition of insideness.

Since
1.2
containsback to summary
public boolean contains(Point2D p)

Implements java.awt.Shape.contains.

Doc from java.awt.Shape.contains.

Tests if a specified Point2D is inside the boundary of the Shape, as described by the definition of insideness.

Since
1.2
containsback to summary
public boolean contains(double x, double y, double w, double h)

Implements java.awt.Shape.contains.

Doc from java.awt.Shape.contains.

Tests if the interior of the Shape entirely contains the specified rectangular area. All coordinates that lie inside the rectangular area must lie within the Shape for the entire rectangular area to be considered contained within the Shape.

The Shape.contains() method allows a Shape implementation to conservatively return false when:

  • the intersect method returns true and
  • the calculations to determine whether or not the Shape entirely contains the rectangular area are prohibitively expensive.
This means that for some Shapes this method might return false even though the Shape contains the rectangular area. The Area class performs more accurate geometric computations than most Shape objects and therefore can be used if a more precise answer is required.
Since
1.2
containsback to summary
public boolean contains(Rectangle2D r)

Implements java.awt.Shape.contains.

Doc from java.awt.Shape.contains.

Tests if the interior of the Shape entirely contains the specified Rectangle2D. The Shape.contains() method allows a Shape implementation to conservatively return false when:

  • the intersect method returns true and
  • the calculations to determine whether or not the Shape entirely contains the Rectangle2D are prohibitively expensive.
This means that for some Shapes this method might return false even though the Shape contains the Rectangle2D. The Area class performs more accurate geometric computations than most Shape objects and therefore can be used if a more precise answer is required.
Since
1.2
getBoundingBoxback to summary
public Rectangle getBoundingBox()

Deprecated

As of JDK version 1.1, replaced by getBounds().

Returns the bounds of this Polygon.

Returns:Rectangle

the bounds of this Polygon.

Annotations
@Deprecated
Since
1.0
getBoundsback to summary
public Rectangle getBounds()

Implements java.awt.Shape.getBounds.

Gets the bounding box of this Polygon. The bounding box is the smallest Rectangle whose sides are parallel to the x and y axes of the coordinate space, and can completely contain the Polygon.

Returns:Rectangle

a Rectangle that defines the bounds of this Polygon.

Since
1.1
getBounds2Dback to summary
public Rectangle2D getBounds2D()

Implements java.awt.Shape.getBounds2D.

Doc from java.awt.Shape.getBounds2D.

Returns a high precision and more accurate bounding box of the Shape than the getBounds method. Note that there is no guarantee that the returned Rectangle2D is the smallest bounding box that encloses the Shape, only that the Shape lies entirely within the indicated Rectangle2D. The bounding box returned by this method is usually tighter than that returned by the getBounds method and never fails due to overflow problems since the return value can be an instance of the Rectangle2D 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 returned bounds object, but only in cases where those points are also not considered contained in the original shape.

If a point is inside the shape according to the contains(point) method, then it must be inside the returned Rectangle2D bounds object according to the contains(point) method of the bounds. Specifically:

shape.contains(p) requires bounds.contains(p)

If a point is not inside the shape, then it might still be contained in the bounds object:

bounds.contains(p) does not imply shape.contains(p)

Since
1.2
getCrossingsback to summary
private Crossings getCrossings(double xlo, double ylo, double xhi, double yhi)
getPathIteratorback to summary
public PathIterator getPathIterator(AffineTransform at)

Implements java.awt.Shape.getPathIterator.

Returns an iterator object that iterates along the boundary of this Polygon and provides access to the geometry of the outline of this Polygon. An optional AffineTransform can be specified so that the coordinates returned in the iteration are transformed accordingly.

Parameters
at:AffineTransform

an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired

Returns:PathIterator

a PathIterator object that provides access to the geometry of this Polygon.

Since
1.2
getPathIteratorback to summary
public PathIterator getPathIterator(AffineTransform at, double flatness)

Implements java.awt.Shape.getPathIterator.

Returns an iterator object that iterates along the boundary of the Shape and provides access to the geometry of the outline of the Shape. Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types are returned by the iterator. Since polygons are already flat, the flatness parameter is ignored. An optional AffineTransform can be specified in which case the coordinates returned in the iteration are transformed accordingly.

Parameters
at:AffineTransform

an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired

flatness:double

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 flatness parameter is ignored.

Returns:PathIterator

a PathIterator object that provides access to the Shape object's geometry.

Since
1.2
insideback to summary
public boolean inside(int x, int y)

Deprecated

As of JDK version 1.1, replaced by contains(int, int).

Determines whether the specified coordinates are contained in this Polygon.

Parameters
x:int

the specified X coordinate to be tested

y:int

the specified Y coordinate to be tested

Returns:boolean

true if this Polygon contains the specified coordinates (x,y); false otherwise.

Annotations
@Deprecated
Since
1.0
See Also
contains(double, double)
intersectsback to summary
public boolean intersects(double x, double y, double w, double h)

Implements java.awt.Shape.intersects.

Doc from java.awt.Shape.intersects.

Tests if the interior of the Shape intersects the interior of a specified rectangular area. The rectangular area is considered to intersect the Shape if any point is contained in both the interior of the Shape and the specified rectangular area.

The Shape.intersects() method allows a Shape implementation to conservatively return true 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.
This means that for some Shapes this method might return true even though the rectangular area does not intersect the Shape. The Area class performs more accurate computations of geometric intersection than most Shape objects and therefore can be used if a more precise answer is required.
Since
1.2
intersectsback to summary
public boolean intersects(Rectangle2D r)

Implements java.awt.Shape.intersects.

Doc from java.awt.Shape.intersects.

Tests if the interior of the Shape intersects the interior of a specified Rectangle2D. The Shape.intersects() method allows a Shape implementation to conservatively return true when:

  • there is a high probability that the Rectangle2D and the Shape intersect, but
  • the calculations to accurately determine this intersection are prohibitively expensive.
This means that for some Shapes this method might return true even though the Rectangle2D does not intersect the Shape. The Area class performs more accurate computations of geometric intersection than most Shape objects and therefore can be used if a more precise answer is required.
Since
1.2
invalidateback to summary
public void invalidate()

Invalidates or flushes any internally-cached data that depends on the vertex coordinates of this Polygon. This method should be called after any direct manipulation of the coordinates in the xpoints or ypoints arrays to avoid inconsistent results from methods such as getBounds or contains that might cache data from earlier computations relating to the vertex coordinates.

Since
1.4
See Also
java.awt.Polygon#getBounds
resetback to summary
public void reset()

Resets this Polygon 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 empty Polygon 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
java.awt.Polygon#invalidate
translateback to summary
public void translate(int deltaX, int deltaY)

Translates the vertices of the Polygon by deltaX along the x axis and by deltaY along the y axis.

Parameters
deltaX:int

the amount to translate along the X axis

deltaY:int

the amount to translate along the Y axis

Since
1.1
updateBoundsback to summary
pack-priv void updateBounds(int x, int y)
java.awt back to summary

pack-priv Class Polygon.PolygonPathIterator

extends Object
implements PathIterator
Class Inheritance
All Implemented Interfaces
java.awt.geom.PathIterator

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv Polygon
pack-priv AffineTransform

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

an integer representing the type and coordinates of the current path segment.
currentSegment
(float[]
a float array that specifies the coordinates of the point(s)
coords
)

Implements java.awt.geom.PathIterator.currentSegment.

Returns the coordinates and type of the current path segment in the iteration.

public int

Returns:

an integer representing the type and coordinates of the current path segment.
currentSegment
(double[]
a double array that specifies the coordinates of the point(s)
coords
)

Implements java.awt.geom.PathIterator.currentSegment.

Returns the coordinates and type of the current path segment in the iteration.

public int

Returns:

an integer representing the current winding rule.
getWindingRule
()

Implements java.awt.geom.PathIterator.getWindingRule.

Returns the winding rule for determining the interior of the path.

public boolean

Returns:

true if there are more points to read; false otherwise.
isDone
()

Implements java.awt.geom.PathIterator.isDone.

Tests if there are more points to read.

public void
next()

Implements java.awt.geom.PathIterator.next.

Moves the iterator forwards, along the primary direction of traversal, to the next segment of the path when there are more points in that direction.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

indexback to summary
pack-priv int index
polyback to summary
pack-priv Polygon poly
transformback to summary
pack-priv AffineTransform transform

Constructor Detail

PolygonPathIteratorback to summary
public PolygonPathIterator(Polygon pg, AffineTransform at)

Method Detail

currentSegmentback to summary
public int currentSegment(float[] coords)

Implements java.awt.geom.PathIterator.currentSegment.

Returns the coordinates and type of the current path segment in the iteration. The return value is the path segment type: SEG_MOVETO, SEG_LINETO, or SEG_CLOSE. A float array of length 2 must be passed in and can be used to store the coordinates of the point(s). Each point is stored as a pair of float x, y coordinates. SEG_MOVETO and SEG_LINETO types return one point, and SEG_CLOSE does not return any points.

Parameters
coords:float[]

a float array that specifies the coordinates of the point(s)

Returns:int

an integer representing the type and coordinates of the current path segment.

See Also
PathIterator#SEG_MOVETO, PathIterator#SEG_LINETO, PathIterator#SEG_CLOSE
currentSegmentback to summary
public int currentSegment(double[] coords)

Implements java.awt.geom.PathIterator.currentSegment.

Returns the coordinates and type of the current path segment in the iteration. The return value is the path segment type: SEG_MOVETO, SEG_LINETO, or SEG_CLOSE. A double array of length 2 must be passed in and can be used to store the coordinates of the point(s). Each point is stored as a pair of double x, y coordinates. SEG_MOVETO and SEG_LINETO types return one point, and SEG_CLOSE does not return any points.

Parameters
coords:double[]

a double array that specifies the coordinates of the point(s)

Returns:int

an integer representing the type and coordinates of the current path segment.

See Also
PathIterator#SEG_MOVETO, PathIterator#SEG_LINETO, PathIterator#SEG_CLOSE
getWindingRuleback to summary
public int getWindingRule()

Implements java.awt.geom.PathIterator.getWindingRule.

Returns the winding rule for determining the interior of the path.

Returns:int

an integer representing the current winding rule.

See Also
PathIterator#WIND_NON_ZERO
isDoneback to summary
public boolean isDone()

Implements java.awt.geom.PathIterator.isDone.

Tests if there are more points to read.

Returns:boolean

true if there are more points to read; false otherwise.

nextback to summary
public void next()

Implements java.awt.geom.PathIterator.next.

Moves the iterator forwards, along the primary direction of traversal, to the next segment of the path when there are more points in that direction.