java.awt
public class Polygon extends Object implements Shape, Serializable
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.
Modifier and Type | Field and Description |
---|---|
protected Rectangle |
bounds
The bounding box of this polygon.
|
int |
npoints
This total number of endpoints.
|
int[] |
xpoints
The array of X coordinates of endpoints.
|
int[] |
ypoints
The array of Y coordinates of endpoints.
|
Constructor and Description |
---|
Polygon()
Initializes an empty polygon.
|
Polygon(int[] xpoints,
int[] ypoints,
int npoints)
Create a new polygon with the specified endpoints.
|
Modifier and Type | Method and Description |
---|---|
void |
addPoint(int x,
int y)
Adds the specified endpoint to the polygon.
|
boolean |
contains(double x,
double y)
Tests whether or not the specified point is inside this polygon.
|
boolean |
contains(double x,
double y,
double w,
double h)
Test if a high-precision rectangle lies completely in the shape.
|
boolean |
contains(int x,
int y)
Tests whether or not the specified point is inside this polygon.
|
boolean |
contains(Point p)
Tests whether or not the specified point is inside this polygon.
|
boolean |
contains(Point2D p)
Tests whether or not the specified point is inside this polygon.
|
boolean |
contains(Rectangle2D r)
Test if a high-precision rectangle lies completely in the shape.
|
Rectangle |
getBoundingBox()
Deprecated.
use
getBounds() instead |
Rectangle |
getBounds()
Returns the bounding box of this polygon.
|
Rectangle2D |
getBounds2D()
Returns a high-precision bounding box of this polygon.
|
PathIterator |
getPathIterator(AffineTransform transform)
Return an iterator along the shape boundary.
|
PathIterator |
getPathIterator(AffineTransform transform,
double flatness)
Return an iterator along the flattened version of the shape boundary.
|
boolean |
inside(int x,
int y)
Deprecated.
use
contains(int, int) instead |
boolean |
intersects(double x,
double y,
double w,
double h)
Test if a high-precision rectangle intersects the shape.
|
boolean |
intersects(Rectangle2D r)
Test if a high-precision rectangle intersects the shape.
|
void |
invalidate()
Invalidate or flush all cached data.
|
void |
reset()
Reset the polygon to be empty.
|
void |
translate(int dx,
int dy)
Translates the polygon by adding the specified values to all X and Y
coordinates.
|
public int npoints
public int[] xpoints
addPoint(int, int)
public int[] ypoints
addPoint(int, int)
protected Rectangle bounds
getBounds()
public Polygon()
public Polygon(int[] xpoints, int[] ypoints, int npoints)
xpoints
- the array of X coordinates for this polygonypoints
- the array of Y coordinates for this polygonnpoints
- the total number of endpoints in this polygonNegativeArraySizeException
- if npoints is negativeIndexOutOfBoundsException
- if npoints exceeds either arrayNullPointerException
- if xpoints or ypoints is nullpublic void reset()
invalidate()
public void invalidate()
contains
.getBounds()
public void translate(int dx, int dy)
dx
- the amount to add to all X coordinatesdy
- the amount to add to all Y coordinatespublic void addPoint(int x, int y)
x
- the X coordinate of the point to addy
- the Y coordiante of the point to addpublic Rectangle getBounds()
getBounds
in interface Shape
getBounds2D()
public Rectangle getBoundingBox()
getBounds()
insteadgetBounds2D()
public boolean contains(Point p)
p
- the point to testNullPointerException
- if p is nullcontains(double, double)
public boolean contains(int x, int y)
x
- the X coordinate of the point to testy
- the Y coordinate of the point to testcontains(double, double)
public boolean inside(int x, int y)
contains(int, int)
insteadx
- the X coordinate of the point to testy
- the Y coordinate of the point to testcontains(double, double)
public Rectangle2D getBounds2D()
getBounds2D
in interface Shape
getBounds()
public boolean contains(double x, double y)
public boolean contains(Point2D p)
contains
in interface Shape
p
- the point to testNullPointerException
- if p is nullcontains(double, double)
public boolean intersects(double x, double y, double w, double h)
intersects
in interface Shape
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 negativeArea
public boolean intersects(Rectangle2D r)
intersects
in interface Shape
r
- the rectangleNullPointerException
- if r is nullintersects(double, double, double, double)
public boolean contains(double x, double y, double w, double h)
contains
in interface Shape
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 negativeArea
public boolean contains(Rectangle2D r)
contains
in interface Shape
r
- the rectangleNullPointerException
- if r is nullcontains(double, double, double, double)
public PathIterator getPathIterator(AffineTransform transform)
getPathIterator
in interface Shape
transform
- an optional transform to apply to the iteratorpublic PathIterator getPathIterator(AffineTransform transform, double flatness)
getPathIterator
in interface Shape
transform
- an optional transform to apply to the iteratorflatness
- the maximum distance for deviation from the real boundary