Polygon (Java Platform SE 8 ) (original) (raw)
- java.awt.Polygon
All Implemented Interfaces:
Shape, Serializable
public class Polygon
extends Object
implements Shape, Serializable
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. SeeWIND_EVEN_ODD for a definition of the even-odd winding rule. This class's hit-testing methods, which include thecontains
, intersects
and inside
methods, use the insideness definition described in theShape class comments.
Since:
1.0
See Also:
Shape, Serialized Form
Field Summary
Fields
Modifier and Type Field Description protected Rectangle bounds The bounds of this Polygon. int npoints The total number of points. int[] xpoints The array of X coordinates. int[] ypoints The array of Y coordinates. Constructor Summary
Constructors
Constructor Description Polygon() Creates an empty polygon. Polygon(int[] xpoints, int[] ypoints, int npoints) Constructs and initializes a Polygon from the specified parameters. Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods
Modifier and Type Method Description void addPoint(int x, int y) Appends the specified coordinates to this Polygon. 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 the Shape entirely contains the specified rectangular area. boolean contains(int x, int y) Determines whether the specified coordinates are inside thisPolygon. boolean contains(Point p) Determines whether the specified Point is inside thisPolygon. boolean contains(Point2D p) Tests if a specified Point2D is inside the boundary of the Shape, as described by the definition of insideness. boolean contains(Rectangle2D r) Tests if the interior of the Shape entirely contains the specified Rectangle2D. Rectangle getBoundingBox() Deprecated. Rectangle getBounds() Gets the bounding box of this Polygon. Rectangle2D getBounds2D() Returns a high precision and more accurate bounding box of the Shape than the getBounds method. PathIterator getPathIterator(AffineTransform at) Returns an iterator object that iterates along the boundary of thisPolygon and provides access to the geometry of the outline of this Polygon. PathIterator getPathIterator(AffineTransform at, double flatness) Returns an iterator object that iterates along the boundary of the Shape and provides access to the geometry of the outline of the Shape. boolean inside(int x, int y) Deprecated. boolean intersects(double x, double y, double w, double h) Tests if the interior of the Shape intersects the interior of a specified rectangular area. boolean intersects(Rectangle2D r) Tests if the interior of the Shape intersects the interior of a specified Rectangle2D. void invalidate() Invalidates or flushes any internally-cached data that depends on the vertex coordinates of this Polygon. void reset() Resets this Polygon object to an empty polygon. void translate(int deltaX, int deltaY) Translates the vertices of the Polygon bydeltaX along the x axis and bydeltaY along the y axis. * ### Methods inherited from class java.lang.[Object](../../java/lang/Object.html "class in java.lang") `[clone](../../java/lang/Object.html#clone--), [equals](../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../java/lang/Object.html#finalize--), [getClass](../../java/lang/Object.html#getClass--), [hashCode](../../java/lang/Object.html#hashCode--), [notify](../../java/lang/Object.html#notify--), [notifyAll](../../java/lang/Object.html#notifyAll--), [toString](../../java/lang/Object.html#toString--), [wait](../../java/lang/Object.html#wait--), [wait](../../java/lang/Object.html#wait-long-), [wait](../../java/lang/Object.html#wait-long-int-)`
Field Detail
* #### npoints 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](../../java/awt/Polygon.html#xpoints) or [ypoints](../../java/awt/Polygon.html#ypoints). This value can be NULL. Since: 1.0 See Also: [addPoint(int, int)](../../java/awt/Polygon.html#addPoint-int-int-) * #### xpoints 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](../../java/awt/Polygon.html#npoints) is equal to the number of valid points in this `Polygon`. Since: 1.0 See Also: [addPoint(int, int)](../../java/awt/Polygon.html#addPoint-int-int-) * #### ypoints 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)](../../java/awt/Polygon.html#addPoint-int-int-) * #### bounds protected [Rectangle](../../java/awt/Rectangle.html "class in java.awt") bounds The bounds of this `Polygon`. This value can be null. Since: 1.0 See Also: [getBoundingBox()](../../java/awt/Polygon.html#getBoundingBox--), [getBounds()](../../java/awt/Polygon.html#getBounds--)
Constructor Detail
* #### Polygon public Polygon() Creates an empty polygon. Since: 1.0 * #### Polygon public Polygon(int[] xpoints, int[] ypoints, int npoints) Constructs and initializes a `Polygon` from 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](../../java/lang/NegativeArraySizeException.html "class in java.lang")` \- if the value of`npoints` is negative. `[IndexOutOfBoundsException](../../java/lang/IndexOutOfBoundsException.html "class in java.lang")` \- if `npoints` is greater than the length of `xpoints` or the length of `ypoints`. `[NullPointerException](../../java/lang/NullPointerException.html "class in java.lang")` \- if `xpoints` or`ypoints` is `null`. Since: 1.0
Method Detail
* #### reset 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: [invalidate()](../../java/awt/Polygon.html#invalidate--) * #### invalidate 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: [getBounds()](../../java/awt/Polygon.html#getBounds--) * #### translate 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` \- the amount to translate along the X axis `deltaY` \- the amount to translate along the Y axis Since: 1.1 * #### addPoint 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` \- the specified X coordinate `y` \- the specified Y coordinate Since: 1.0 See Also: [getBounds()](../../java/awt/Polygon.html#getBounds--), [contains(java.awt.Point)](../../java/awt/Polygon.html#contains-java.awt.Point-) * #### getBounds public [Rectangle](../../java/awt/Rectangle.html "class in java.awt") getBounds() Gets the bounding box of this `Polygon`. The bounding box is the smallest [Rectangle](../../java/awt/Rectangle.html "class in java.awt") whose sides are parallel to the x and y axes of the coordinate space, and can completely contain the `Polygon`. Specified by: `[getBounds](../../java/awt/Shape.html#getBounds--)` in interface `[Shape](../../java/awt/Shape.html "interface in java.awt")` Returns: a `Rectangle` that defines the bounds of this`Polygon`. Since: 1.1 See Also: [Shape.getBounds2D()](../../java/awt/Shape.html#getBounds2D--) * #### getBoundingBox [@Deprecated](../../java/lang/Deprecated.html "annotation in java.lang") public [Rectangle](../../java/awt/Rectangle.html "class in java.awt") getBoundingBox() Deprecated. Returns the bounds of this `Polygon`. Returns: the bounds of this `Polygon`. Since: 1.0 * #### contains public boolean contains([Point](../../java/awt/Point.html "class in java.awt") p) Determines whether the specified [Point](../../java/awt/Point.html "class in java.awt") is inside this`Polygon`. Parameters: `p` \- the specified `Point` to be tested Returns: `true` if the `Polygon` contains the`Point`; `false` otherwise. Since: 1.0 See Also: [contains(double, double)](../../java/awt/Polygon.html#contains-double-double-) * #### contains public boolean contains(int x, int y) Determines whether the specified coordinates are inside this`Polygon`. Parameters: `x` \- the specified X coordinate to be tested `y` \- the specified Y coordinate to be tested Returns: `true` if this `Polygon` contains the specified coordinates `(x,y)`;`false` otherwise. Since: 1.1 See Also: [contains(double, double)](../../java/awt/Polygon.html#contains-double-double-) * #### inside [@Deprecated](../../java/lang/Deprecated.html "annotation in java.lang") public boolean inside(int x, int y) Deprecated. Determines whether the specified coordinates are contained in this`Polygon`. Parameters: `x` \- the specified X coordinate to be tested `y` \- the specified Y coordinate to be tested Returns: `true` if this `Polygon` contains the specified coordinates `(x,y)`;`false` otherwise. Since: 1.0 See Also: [contains(double, double)](../../java/awt/Polygon.html#contains-double-double-) * #### getBounds2D public [Rectangle2D](../../java/awt/geom/Rectangle2D.html "class in java.awt.geom") 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](../../java/awt/geom/Rectangle2D.html "class in java.awt.geom") 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](../../java/awt/Shape.html#def%5Finsideness) 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)](../../java/awt/Shape.html#contains-java.awt.geom.Point2D-) method, then it must be inside the returned `Rectangle2D` bounds object according to the [contains(point)](../../java/awt/Shape.html#contains-java.awt.geom.Point2D-) 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)` Specified by: `[getBounds2D](../../java/awt/Shape.html#getBounds2D--)` in interface `[Shape](../../java/awt/Shape.html "interface in java.awt")` Returns: an instance of `Rectangle2D` that is a high-precision bounding box of the `Shape`. Since: 1.2 See Also: [Shape.getBounds()](../../java/awt/Shape.html#getBounds--) * #### contains public boolean contains(double x, double y) Tests if the specified coordinates are inside the boundary of the`Shape`, as described by the[ definition of insideness](../../java/awt/Shape.html#def%5Finsideness). Specified by: `[contains](../../java/awt/Shape.html#contains-double-double-)` in interface `[Shape](../../java/awt/Shape.html "interface in java.awt")` Parameters: `x` \- the specified X coordinate to be tested `y` \- the specified Y coordinate to be tested Returns: `true` if the specified coordinates are inside the `Shape` boundary; `false` otherwise. Since: 1.2 * #### contains public boolean contains([Point2D](../../java/awt/geom/Point2D.html "class in java.awt.geom") p) Specified by: `[contains](../../java/awt/Shape.html#contains-java.awt.geom.Point2D-)` in interface `[Shape](../../java/awt/Shape.html "interface in java.awt")` Parameters: `p` \- the specified `Point2D` to be tested Returns: `true` if the specified `Point2D` is inside the boundary of the `Shape`;`false` otherwise. Since: 1.2 * #### intersects public boolean intersects(double x, double y, double w, double h) 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](../../java/awt/geom/Area.html "class in java.awt.geom") class performs more accurate computations of geometric intersection than most`Shape` objects and therefore can be used if a more precise answer is required. Specified by: `[intersects](../../java/awt/Shape.html#intersects-double-double-double-double-)` in interface `[Shape](../../java/awt/Shape.html "interface in java.awt")` 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: `true` if the interior of the `Shape` 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: [Area](../../java/awt/geom/Area.html "class in java.awt.geom") * #### intersects public boolean intersects([Rectangle2D](../../java/awt/geom/Rectangle2D.html "class in java.awt.geom") r) 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](../../java/awt/geom/Area.html "class in java.awt.geom") class performs more accurate computations of geometric intersection than most`Shape` objects and therefore can be used if a more precise answer is required. Specified by: `[intersects](../../java/awt/Shape.html#intersects-java.awt.geom.Rectangle2D-)` in interface `[Shape](../../java/awt/Shape.html "interface in java.awt")` Parameters: `r` \- the specified `Rectangle2D` Returns: `true` if the interior of the `Shape` and the interior of the specified `Rectangle2D` intersect, or are both highly likely to intersect and intersection calculations would be too expensive to perform; `false` otherwise. Since: 1.2 See Also: [Shape.intersects(double, double, double, double)](../../java/awt/Shape.html#intersects-double-double-double-double-) * #### contains public boolean contains(double x, double y, double w, double h) 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](../../java/awt/geom/Area.html "class in java.awt.geom") class performs more accurate geometric computations than most`Shape` objects and therefore can be used if a more precise answer is required. Specified by: `[contains](../../java/awt/Shape.html#contains-double-double-double-double-)` in interface `[Shape](../../java/awt/Shape.html "interface in java.awt")` 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: `true` if the interior of the `Shape` entirely contains the specified rectangular area;`false` otherwise or, if the `Shape` contains the rectangular area and the`intersects` method returns `true` and the containment calculations would be too expensive to perform. Since: 1.2 See Also: [Area](../../java/awt/geom/Area.html "class in java.awt.geom"), [Shape.intersects(double, double, double, double)](../../java/awt/Shape.html#intersects-double-double-double-double-) * #### contains public boolean contains([Rectangle2D](../../java/awt/geom/Rectangle2D.html "class in java.awt.geom") r) 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](../../java/awt/geom/Area.html "class in java.awt.geom") class performs more accurate geometric computations than most`Shape` objects and therefore can be used if a more precise answer is required. Specified by: `[contains](../../java/awt/Shape.html#contains-java.awt.geom.Rectangle2D-)` in interface `[Shape](../../java/awt/Shape.html "interface in java.awt")` Parameters: `r` \- The specified `Rectangle2D` Returns: `true` if the interior of the `Shape` entirely contains the `Rectangle2D`;`false` otherwise or, if the `Shape` contains the `Rectangle2D` and the`intersects` method returns `true` and the containment calculations would be too expensive to perform. Since: 1.2 See Also: [Shape.contains(double, double, double, double)](../../java/awt/Shape.html#contains-double-double-double-double-) * #### getPathIterator public [PathIterator](../../java/awt/geom/PathIterator.html "interface in java.awt.geom") getPathIterator([AffineTransform](../../java/awt/geom/AffineTransform.html "class in java.awt.geom") at) 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](../../java/awt/geom/AffineTransform.html "class in java.awt.geom") can be specified so that the coordinates returned in the iteration are transformed accordingly. Specified by: `[getPathIterator](../../java/awt/Shape.html#getPathIterator-java.awt.geom.AffineTransform-)` in interface `[Shape](../../java/awt/Shape.html "interface in java.awt")` Parameters: `at` \- an optional `AffineTransform` to be applied to the coordinates as they are returned in the iteration, or`null` if untransformed coordinates are desired Returns: a [PathIterator](../../java/awt/geom/PathIterator.html "interface in java.awt.geom") object that provides access to the geometry of this `Polygon`. Since: 1.2 * #### getPathIterator public [PathIterator](../../java/awt/geom/PathIterator.html "interface in java.awt.geom") getPathIterator([AffineTransform](../../java/awt/geom/AffineTransform.html "class in java.awt.geom") at, double flatness) 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. Specified by: `[getPathIterator](../../java/awt/Shape.html#getPathIterator-java.awt.geom.AffineTransform-double-)` in interface `[Shape](../../java/awt/Shape.html "interface in java.awt")` Parameters: `at` \- an optional `AffineTransform` to be applied to the coordinates as they are returned in the iteration, or`null` if untransformed coordinates are desired `flatness` \- the maximum amount that the control points for a given curve can vary from colinear before a subdivided curve is replaced by a straight line connecting the endpoints. Since polygons are already flat the`flatness` parameter is ignored. Returns: a `PathIterator` object that provides access to the`Shape` object's geometry. Since: 1.2
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.