java.awt
Class Polygon
- Serializable, Shape
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.
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.
|
Polygon() - Initializes an empty polygon.
|
Polygon(int[] xpoints, int[] ypoints, int npoints) - Create a new polygon with the specified endpoints.
|
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() - 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) - 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.
|
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
bounds
protected Rectangle bounds
The bounding box of this polygon. This is lazily created and cached, so
it must be invalidated after changing points.
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.
ypoints
public int[] ypoints
The array of Y coordinates of endpoints. This should not be null.
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.
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
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.
x
- the X coordinate of the point to addy
- the Y coordiante of the point to add
contains
public boolean contains(double x,
double y)
Tests whether or not the specified point is inside this polygon.
- contains in interface Shape
x
- the X coordinate of the point to testy
- the Y coordinate of the point to test
- 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.
- 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 negative
- 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.
x
- the X coordinate of the point to testy
- the Y coordinate of the point to test
- true if the point is inside this polygon
contains
public boolean contains(Point p)
Tests whether or not the specified point is inside this polygon.
- true if the point is inside this polygon
contains
public boolean contains(Point2D p)
Tests whether or not the specified point is inside this polygon.
- contains in interface Shape
- true if the point is inside this polygon
contains
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.
- contains in interface Shape
- true if the rectangle is contained in this shape
getBoundingBox
public Rectangle getBoundingBox()
use getBounds()
instead
Returns the bounding box of this polygon. This is the smallest
rectangle with sides parallel to the X axis that will contain this
polygon.
- the bounding box for this polygon
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.
- getBounds in interface Shape
- the bounding box for this polygon
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.
- getBounds2D in interface Shape
- the bounding box for this polygon
getPathIterator
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.
- getPathIterator in interface Shape
transform
- an optional transform to apply to the iterator
- a new iterator over the boundary
getPathIterator
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.
- getPathIterator in interface Shape
transform
- an optional transform to apply to the iteratorflatness
- the maximum distance for deviation from the real boundary
- a new iterator over the boundary
inside
public boolean inside(int x,
int y)
use contains(int,int)
instead
Tests whether or not the specified point is inside this polygon.
x
- the X coordinate of the point to testy
- the Y coordinate of the point to test
- true if the point is inside this polygon
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.
- 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 negative
- true if the rectangle intersects this shape
intersects
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.
- intersects in interface Shape
- true if the rectangle intersects this shape
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
.
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.
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.
dx
- the amount to add to all X coordinatesdy
- the amount to add to all Y coordinates
Polygon.java -- class representing a polygon
Copyright (C) 1999, 2002, 2004, 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.