java.lang.Object java.awt.Polygon int npoints int[] xpoints int[] ypoints Polygon()Polygon(int[] xpoints, int[] ypoints, int npoints) void addPoint(int x, int y) boolean contains(double x, double y) boolean contains(double x, double y, double w, double h) boolean contains(int x, int y) boolean contains(Rectangle2D r) Rectangle getBoundingBox()getBounds() instead Rectangle2D getBounds2D() PathIterator getPathIterator(AffineTransform transform) PathIterator getPathIterator(AffineTransform transform, double flatness) boolean inside(int x, int y)contains(int,int) instead boolean intersects(double x, double y, double w, double h) boolean intersects(Rectangle2D r) void invalidate() void reset() void translate(int dx, int dy)protected Rectangle bounds
The bounding box of this polygon. This is lazily created and cached, so it must be invalidated after changing points.
- See Also:
getBounds()
public int[] xpoints
The array of X coordinates of endpoints. This should not be null.
- See Also:
addPoint(int,int)
public int[] ypoints
The array of Y coordinates of endpoints. This should not be null.
- See Also:
addPoint(int,int)
public Polygon(int[] xpoints, int[] ypoints, int npoints)
Create a new polygon with the specified endpoints. The arrays are copied, so that future modifications to the parameters do not affect the polygon.
- Parameters:
xpoints- the array of X coordinates for this polygonypoints- the array of Y coordinates for this polygonnpoints- the total number of endpoints in this polygon
- Throws:
NegativeArraySizeException- if npoints is negativeIndexOutOfBoundsException- if npoints exceeds either arrayNullPointerException- if xpoints or ypoints is null
public void addPoint(int x, int y)
Adds the specified endpoint to the polygon. This updates the bounding box, if it has been created.
- Parameters:
x- the X coordinate of the point to addy- the Y coordiante of the point to add
public boolean contains(double x, double y)
Tests whether or not the specified point is inside this polygon.
- Parameters:
x- the X coordinate of the point to testy- the Y coordinate of the point to test
- Returns:
- true if the point is inside this polygon
- Since:
- 1.2
public boolean contains(double x, double y, double w, double h)
Test if a high-precision rectangle lies completely in the shape. This is true if all points in the rectangle are in the shape. This implementation is precise.
- Parameters:
x- the x coordinate of the rectangley- the y coordinate of the rectanglew- the width of the rectangle, treated as point if negativeh- the height of the rectangle, treated as point if negative
- Returns:
- true if the rectangle is contained in this shape
- Since:
- 1.2
public boolean contains(int x, int y)
Tests whether or not the specified point is inside this polygon.
- Parameters:
x- the X coordinate of the point to testy- the Y coordinate of the point to test
- Returns:
- true if the point is inside this polygon
- Since:
- 1.1
- See Also:
contains(double,double)
public boolean contains(Point p)
Tests whether or not the specified point is inside this polygon.
- Parameters:
p- the point to test
- Returns:
- true if the point is inside this polygon
- Throws:
NullPointerException- if p is null
- See Also:
contains(double,double)
public boolean contains(Point2D p)
Tests whether or not the specified point is inside this polygon.
- Parameters:
p- the point to test
- Returns:
- true if the point is inside this polygon
- Throws:
NullPointerException- if p is null
- Since:
- 1.2
- See Also:
contains(double,double)
public boolean contains(Rectangle2D r)
Test if a high-precision rectangle lies completely in the shape. This is true if all points in the rectangle are in the shape. This implementation is precise.
- Parameters:
r- the rectangle
- Returns:
- true if the rectangle is contained in this shape
- Throws:
NullPointerException- if r is null
- Since:
- 1.2
public Rectangle getBoundingBox()
Deprecated. use
getBounds()insteadReturns the bounding box of this polygon. This is the smallest rectangle with sides parallel to the X axis that will contain this polygon.
- Returns:
- the bounding box for this polygon
- See Also:
getBounds2D()
public Rectangle getBounds()
Returns the bounding box of this polygon. This is the smallest rectangle with sides parallel to the X axis that will contain this polygon.
- Returns:
- the bounding box for this polygon
- Since:
- 1.1
- See Also:
getBounds2D()
public Rectangle2D getBounds2D()
Returns a high-precision bounding box of this polygon. This is the smallest rectangle with sides parallel to the X axis that will contain this polygon.
- Specified by:
- getBounds2D in interface Shape
- Returns:
- the bounding box for this polygon
- Since:
- 1.2
- See Also:
getBounds()
public PathIterator getPathIterator(AffineTransform transform)
Return an iterator along the shape boundary. If the optional transform is provided, the iterator is transformed accordingly. Each call returns a new object, independent from others in use. This class is not threadsafe to begin with, so the path iterator is not either.
- Specified by:
- getPathIterator in interface Shape
- Parameters:
transform- an optional transform to apply to the iterator
- Returns:
- a new iterator over the boundary
- Since:
- 1.2
public PathIterator getPathIterator(AffineTransform transform, double flatness)
Return an iterator along the flattened version of the shape boundary. Since polygons are already flat, the flatness parameter is ignored, and the resulting iterator only has SEG_MOVETO, SEG_LINETO and SEG_CLOSE points. If the optional transform is provided, the iterator is transformed accordingly. Each call returns a new object, independent from others in use. This class is not threadsafe to begin with, so the path iterator is not either.
- Specified by:
- getPathIterator in interface Shape
- Parameters:
transform- an optional transform to apply to the iteratorflatness- the maximum distance for deviation from the real boundary
- Returns:
- a new iterator over the boundary
- Since:
- 1.2
public boolean inside(int x, int y)
Deprecated. use
contains(int,int)insteadTests whether or not the specified point is inside this polygon.
- Parameters:
x- the X coordinate of the point to testy- the Y coordinate of the point to test
- Returns:
- true if the point is inside this polygon
- See Also:
contains(double,double)
public boolean intersects(double x, double y, double w, double h)
Test if a high-precision rectangle intersects the shape. This is true if any point in the rectangle is in the shape. This implementation is precise.
- Specified by:
- intersects in interface Shape
- Parameters:
x- the x coordinate of the rectangley- the y coordinate of the rectanglew- the width of the rectangle, treated as point if negativeh- the height of the rectangle, treated as point if negative
- Returns:
- true if the rectangle intersects this shape
- Since:
- 1.2
public boolean intersects(Rectangle2D r)
Test if a high-precision rectangle intersects the shape. This is true if any point in the rectangle is in the shape. This implementation is precise.
- Specified by:
- intersects in interface Shape
- Parameters:
r- the rectangle
- Returns:
- true if the rectangle intersects this shape
- Throws:
NullPointerException- if r is null
- Since:
- 1.2
public void invalidate()
Invalidate or flush all cached data. After direct manipulation of the public member fields, this is necessary to avoid inconsistent results in methods likecontains.
- Since:
- 1.4
- See Also:
getBounds()
public void reset()
Reset the polygon to be empty. The arrays are left alone, to avoid object allocation, but the number of points is set to 0, and all cached data is discarded. If you are discarding a huge number of points, it may be more efficient to just create a new Polygon.
- Since:
- 1.4
- See Also:
invalidate()
public void translate(int dx, int dy)
Translates the polygon by adding the specified values to all X and Y coordinates. This updates the bounding box, if it has been calculated.
- Parameters:
dx- the amount to add to all X coordinatesdy- the amount to add to all Y coordinates
- Since:
- 1.1