| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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 {@code (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 {@code (x,y)} points are joined by a line segment
that closes the polygon. This Polygon
is defined with
an even-odd winding rule. See
{@link java.awt.geom.PathIterator#WIND_EVEN_ODD 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
{@link Shape} class comments.
Field Summary | |
---|---|
protected Rectangle |
bounds
The bounds of this . |
int |
npoints
The total number of points. |
int[] |
xpoints
The array of X coordinates. |
int[] |
ypoints
The array of Y coordinates. |
Constructor Summary | |
---|---|
Polygon() Creates an empty polygon. |
|
Polygon(int[] xpoints, int[] ypoints, int npoints) Constructs and initializes a Polygon from the specified
parameters.
|
Method Summary | |
---|---|
void |
addPoint(int x, int y) Appends the specified coordinates to this Polygon .
|
boolean |
contains(double x, double y) |
boolean |
contains(double x, double y, double w, double h) |
boolean |
contains(int x, int y) Determines whether the specified coordinates are inside this Polygon .
|
boolean |
Determines whether the specified java.awt.Point is inside this Polygon .
|
boolean |
|
boolean |
|
Rectangle |
Returns the bounds of this Polygon .
|
Rectangle |
Gets the bounding box of this Polygon .
|
Rectangle2D |
|
PathIterator |
Returns an iterator object that iterates along the boundary of this Polygon 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) Determines whether the specified coordinates are contained in this Polygon .
|
boolean |
intersects(double x, double y, double w, double h) |
boolean |
|
void |
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 by
deltaX along the x axis and by
deltaY along the y axis.
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected Rectangle bounds
public int npoints
npoints
represents the number of valid points in this Polygon
and might be less than the number of elements in
{@link #xpoints xpoints} or {@link #ypoints ypoints}.
This value can be NULL.
public int[] xpoints
Polygon
. The extra elements allow new points
to be added to this Polygon
without re-creating this
array. The value of {@link #npoints npoints} is equal to the
number of valid points in this Polygon
.
public int[] ypoints
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
.
Constructor Detail |
---|
public Polygon()
public Polygon(int[] xpoints, int[] ypoints, int npoints)
Polygon
from the specified
parameters.
xpoints
- an array of X coordinatesypoints
- an array of Y coordinatesnpoints
- the total number of points in the
Polygon
Method Detail |
---|
public void addPoint(int x, int y)
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.
x
- the specified X coordinatey
- the specified Y coordinatepublic boolean contains(double x, double y)
x
y
public boolean contains(double x, double y, double w, double h)
x
y
w
h
public boolean contains(int x, int y)
Polygon
.
x
- the specified X coordinate to be testedy
- the specified Y coordinate to be testedpublic boolean contains(Point p)
Polygon
.
p
- the specified Point
to be testedtrue
if the Polygon
contains the
Point
; false
otherwise.public boolean contains(Point2D p)
p
public boolean contains(Rectangle2D r)
r
public Rectangle getBoundingBox()
Polygon
.
Polygon
.public Rectangle getBounds()
Polygon
.
The bounding box is the smallest {@link Rectangle} whose
sides are parallel to the x and y axes of the
coordinate space, and can completely contain the Polygon
.
Rectangle
that defines the bounds of this
Polygon
.public Rectangle2D getBounds2D()
public PathIterator getPathIterator(AffineTransform at)
Polygon
and provides access to the geometry
of the outline of this Polygon
. An optional
{@link AffineTransform} can be specified so that the coordinates
returned in the iteration are transformed accordingly.
at
- an optional AffineTransform
to be applied to the
coordinates as they are returned in the iteration, or
null
if untransformed coordinates are desiredPolygon
.public PathIterator getPathIterator(AffineTransform at, double flatness)
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.
at
- an optional AffineTransform
to be applied to the
coordinates as they are returned in the iteration, or
null
if untransformed coordinates are desiredflatness
- 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.PathIterator
object that provides access to the
Shape
object's geometry.public boolean inside(int x, int y)
Polygon
.
x
- the specified X coordinate to be testedy
- the specified Y coordinate to be testedpublic boolean intersects(double x, double y, double w, double h)
x
y
w
h
public boolean intersects(Rectangle2D r)
r
public void invalidate()
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.
public void reset()
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.
public void translate(int deltaX, int deltaY)
Polygon
by
deltaX
along the x axis and by
deltaY
along the y axis.
deltaX
- the amount to translate along the X axisdeltaY
- the amount to translate along the Y axis
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |