java.awt
Class Polygon
java.lang.Object
|
+--java.awt.Polygon
All Implemented Interfaces:
Shape, Serializable
This class represents a polygon, a closed, two-dimensional region in a
coordinate space. The region is bounded by an arbitrary number of line
segments, between (x,y) coordinate vertices. The polygon has even-odd
winding, meaning that a point is inside the shape if it crosses the
boundary an odd number of times on the way to infinity.
There are some public fields; if you mess with them in an inconsistent
manner, it is your own fault when you get NullPointerException,
ArrayIndexOutOfBoundsException, or invalid results. Also, this class is
not threadsafe.
Since:Authors:- Aaron M. Renn <arenn@urbanophile.com>
- Eric Blake <ebb9@email.byu.edu>
void | addPoint(int x, int y)
|
boolean | contains(java.awt.Point p)
|
boolean | contains(int x, int y)
|
boolean | contains(double x, double y)
|
boolean | contains(java.awt.geom.Point2D p)
|
boolean | contains(double x, double y, double w, double h)
|
boolean | contains(java.awt.geom.Rectangle2D r)
|
java.awt.Rectangle | getBoundingBox()
|
java.awt.Rectangle | getBounds()
|
java.awt.geom.Rectangle2D | getBounds2D()
|
java.awt.geom.PathIterator | getPathIterator(final AffineTransform transform)
|
java.awt.geom.PathIterator | getPathIterator(java.awt.geom.AffineTransform transform, double flatness)
|
boolean | inside(int x, int y)
|
boolean | intersects(double x, double y, double w, double h)
|
boolean | intersects(java.awt.geom.Rectangle2D r)
|
void | invalidate()
|
void | reset()
|
void | translate(int dx, int dy)
|
bounds
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:
npoints
public int npoints
This total number of endpoints.
xpoints
public int[] xpoints
The array of X coordinates of endpoints. This should not be null.
See Also:
ypoints
public int[] ypoints
The array of Y coordinates of endpoints. This should not be null.
See Also:
Polygon
public Polygon()
Initializes an empty polygon.
Polygon
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:
Throws:
addPoint
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:
contains
public boolean contains(double x, double y)
Tests whether or not the specified point is inside this polygon.
Since:Parameters:
Returns:
- true if the point is inside this polygon
contains
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.
Since:Parameters:
Returns:
- true if the rectangle is contained in this shape
contains
public boolean contains(int x, int y)
Tests whether or not the specified point is inside this polygon.
Since:Parameters:
Returns:
- true if the point is inside this polygon
See Also:
contains
public boolean contains(java.awt.Point p)
Tests whether or not the specified point is inside this polygon.
Parameters:
Returns:
- true if the point is inside this polygon
Throws:
See Also:
contains
public boolean contains(java.awt.geom.Point2D p)
Tests whether or not the specified point is inside this polygon.
Since:Parameters:
Returns:
- true if the point is inside this polygon
Throws:
See Also:
contains
public boolean contains(java.awt.geom.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.
Since:Parameters:
Returns:
- true if the rectangle is contained in this shape
Throws:
See Also:
getBoundingBox
public Rectangle getBoundingBox()
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
See Also:
getBounds
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.
Since:Returns:
- the bounding box for this polygon
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.
Since:Returns:
- the bounding box for this polygon
See Also:
getPathIterator
public PathIterator getPathIterator(final 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.
Since:Parameters:
Returns:
- a new iterator over the boundary
getPathIterator
public PathIterator getPathIterator(java.awt.geom.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.
Since:Parameters:
Returns:
- a new iterator over the boundary
inside
public boolean inside(int x, int y)
Tests whether or not the specified point is inside this polygon.
Parameters:
Returns:
- true if the point is inside this polygon
See Also:
intersects
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.
Since:Parameters:
Returns:
- true if the rectangle intersects this shape
intersects
public boolean intersects(java.awt.geom.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.
Since:Parameters:
Returns:
- true if the rectangle intersects this shape
Throws:
See Also:
invalidate
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 like contains
.
Since:See Also:
reset
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:See Also:
translate
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.
Since:Parameters:
There are some public fields; if you mess with them in an inconsistent manner, it is your own fault when you get NullPointerException, ArrayIndexOutOfBoundsException, or invalid results. Also, this class is not threadsafe.