
JSR 216 (Maintenance Release)  
PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 
java.lang.Object java.awt.Polygon
The Polygon
class encapsulates a description of a
closed, twodimensional 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 evenodd winding rule.
(The evenodd rule specifies that a point lies inside the path if a ray drawn in any direction from that point to infinity is crossed by path segments an odd number of times.)
This class's hittesting methods, which include the
contains
, intersects
and inside
methods, use the insideness definition described in the
Shape
class comments.
Shape
,
Serialized FormField Summary  
protected Rectangle 
bounds
Bounds of the polygon. 
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(int x,
int y)
Determines whether the specified coordinates are inside this Polygon . 
boolean 
contains(Point p)
Determines whether the specified Point is inside this
Polygon . 
Rectangle 
getBoundingBox()
Deprecated. As of JDK version 1.1, replaced by getBounds() . 
Rectangle 
getBounds()
Gets the bounding box of this Polygon . 
boolean 
inside(int x,
int y)
Deprecated. As of JDK version 1.1, replaced by contains(int, int) . 
void 
invalidate()
Invalidates or flushes any internallycached 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 
public int npoints
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 NULL.
addPoint(int, int)
public int[] xpoints
Polygon
. The extra elements allow new points
to be added to this Polygon
without recreating this
array. The value of npoints
is equal to the
number of valid points in this Polygon
.
addPoint(int, int)
public int[] ypoints
Polygon
. The extra elements allow new points
to be added to this Polygon
without recreating this
array. The value of npoints
is equal to the
number of valid points in this Polygon
.
addPoint(int, int)
protected Rectangle bounds
getBoundingBox()
,
getBounds()
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
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
.Method Detail 
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 internallycached 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.
invalidate()
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.
getBounds()
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 axispublic 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 coordinategetBounds()
,
contains(java.awt.Point)
public Rectangle getBounds()
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
.
getBounds
in interface Shape
Rectangle
that defines the bounds of this
Polygon
.public Rectangle getBoundingBox()
getBounds()
.
Polygon
.
Polygon
.public boolean contains(Point p)
Point
is inside this
Polygon
.
p
 the specified Point
to be tested
true
if the Polygon
contains the
Point
; false
otherwise.public boolean contains(int x, int y)
Polygon
.
x
 the specified x coordinate to be testedy
 the specified y coordinate to be tested
true
if this Polygon
contains
the specified coordinates, (x, y);
false
otherwise.public boolean inside(int x, int y)
contains(int, int)
.
Polygon
.
x
 the specified x coordinate to be testedy
 the specified y coordinate to be tested
true
if this Polygon
contains
the specified coordinates, (x, y);
false
otherwise.

JSR 216 (Maintenance Release)  
PREV CLASS NEXT CLASS  FRAMES NO FRAMES  
SUMMARY: NESTED  FIELD  CONSTR  METHOD  DETAIL: FIELD  CONSTR  METHOD 