Polygonpublic class Polygon extends Object implements Serializable, ShapeThe Polygon class defines an closed area specified by n vertices and n edges.
The coordinates of the vertices are specified by x, y arrays. The edges are
the line segments from the point (x[i], y[i]) to the point (x[i+1], y[i+1]),
for -1 < i < (n-1) plus the line segment from the point (x[n-1], y[n-1]) to
the point (x[0], y[0]) point. The Polygon is empty if the number of vertices
is zero. |
Fields Summary |
---|
private static final long | serialVersionUIDThe Constant serialVersionUID. | private static final int | BUFFER_CAPACITYThe points buffer capacity. | public int | npointsThe number of Polygon vertices. | public int[] | xpointsThe array of X coordinates of the vertices. | public int[] | ypointsThe array of Y coordinates of the vertices. | protected Rectangle | boundsThe smallest Rectangle that completely contains this Polygon. |
Constructors Summary |
---|
public Polygon()Instantiates a new empty polygon.
xpoints = new int[BUFFER_CAPACITY];
ypoints = new int[BUFFER_CAPACITY];
| public Polygon(int[] xpoints, int[] ypoints, int npoints)Instantiates a new polygon with the specified number of vertices, and the
given arrays of x, y vertex coordinates. The length of each coordinate
array may not be less than the specified number of vertices but may be
greater. Only the first n elements are used from each coordinate array.
if (npoints > xpoints.length || npoints > ypoints.length) {
// awt.111=Parameter npoints is greater than array length
throw new IndexOutOfBoundsException(Messages.getString("awt.111")); //$NON-NLS-1$
}
if (npoints < 0) {
// awt.112=Negative number of points
throw new NegativeArraySizeException(Messages.getString("awt.112")); //$NON-NLS-1$
}
this.npoints = npoints;
this.xpoints = new int[npoints];
this.ypoints = new int[npoints];
System.arraycopy(xpoints, 0, this.xpoints, 0, npoints);
System.arraycopy(ypoints, 0, this.ypoints, 0, npoints);
|
Methods Summary |
---|
public void | addPoint(int px, int py)Adds the point to the Polygon and updates the bounding box accordingly.
if (npoints == xpoints.length) {
int[] tmp;
tmp = new int[xpoints.length + BUFFER_CAPACITY];
System.arraycopy(xpoints, 0, tmp, 0, xpoints.length);
xpoints = tmp;
tmp = new int[ypoints.length + BUFFER_CAPACITY];
System.arraycopy(ypoints, 0, tmp, 0, ypoints.length);
ypoints = tmp;
}
xpoints[npoints] = px;
ypoints[npoints] = py;
npoints++;
if (bounds != null) {
bounds.setFrameFromDiagonal(Math.min(bounds.getMinX(), px), Math.min(bounds.getMinY(),
py), Math.max(bounds.getMaxX(), px), Math.max(bounds.getMaxY(), py));
}
| public boolean | contains(int x, int y)Checks whether or not the point given by the coordinates x, y lies inside
the Polygon.
return contains((double)x, (double)y);
| public boolean | contains(double x, double y)Checks whether or not the point with specified double coordinates lies
inside the Polygon.
return Crossing.isInsideEvenOdd(Crossing.crossShape(this, x, y));
| public boolean | contains(double x, double y, double width, double height)Checks whether or not the rectangle determined by the parameters [x, y,
width, height] lies inside the Polygon.
int cross = Crossing.intersectShape(this, x, y, width, height);
return cross != Crossing.CROSSING && Crossing.isInsideEvenOdd(cross);
| public boolean | contains(java.awt.geom.Rectangle2D rect)Checks whether or not the specified rectangle lies inside the Polygon.
return contains(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
| public boolean | contains(java.awt.Point point)Checks whether or not the specified Point lies inside the Polygon.
return contains(point.getX(), point.getY());
| public boolean | contains(java.awt.geom.Point2D point)Checks whether or not the specified Point2D lies inside the Polygon.
return contains(point.getX(), point.getY());
| public java.awt.Rectangle | getBoundingBox()Gets the bounding rectangle of the Polygon. The bounding rectangle is the
smallest rectangle which contains the Polygon.
return getBounds();
| public java.awt.Rectangle | getBounds()Gets the bounding rectangle of the Polygon. The bounding rectangle is the
smallest rectangle which contains the Polygon.
if (bounds != null) {
return bounds;
}
if (npoints == 0) {
return new Rectangle();
}
int bx1 = xpoints[0];
int by1 = ypoints[0];
int bx2 = bx1;
int by2 = by1;
for (int i = 1; i < npoints; i++) {
int x = xpoints[i];
int y = ypoints[i];
if (x < bx1) {
bx1 = x;
} else if (x > bx2) {
bx2 = x;
}
if (y < by1) {
by1 = y;
} else if (y > by2) {
by2 = y;
}
}
return bounds = new Rectangle(bx1, by1, bx2 - bx1, by2 - by1);
| public java.awt.geom.Rectangle2D | getBounds2D()Gets the Rectangle2D which represents Polygon bounds. The bounding
rectangle is the smallest rectangle which contains the Polygon.
return getBounds().getBounds2D();
| public java.awt.geom.PathIterator | getPathIterator(java.awt.geom.AffineTransform t)Gets the PathIterator object which gives the coordinates of the polygon,
transformed according to the specified AffineTransform.
return new Iterator(t, this);
| public java.awt.geom.PathIterator | getPathIterator(java.awt.geom.AffineTransform t, double flatness)Gets the PathIterator object which gives the coordinates of the polygon,
transformed according to the specified AffineTransform. The flatness
parameter is ignored.
return new Iterator(t, this);
| public boolean | inside(int x, int y)Checks whether or not the point given by the coordinates x, y lies inside
the Polygon.
return contains((double)x, (double)y);
| public boolean | intersects(double x, double y, double width, double height)Checks whether or not the rectangle determined by the parameters [x, y,
width, height] intersects the interior of the Polygon.
int cross = Crossing.intersectShape(this, x, y, width, height);
return cross == Crossing.CROSSING || Crossing.isInsideEvenOdd(cross);
| public boolean | intersects(java.awt.geom.Rectangle2D rect)Checks whether or not the interior of rectangle specified by the
Rectangle2D object intersects the interior of the Polygon.
return intersects(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
| public void | invalidate()Invalidates the data that depends on the vertex coordinates. This method
should be called after direct manipulations of the x, y vertex
coordinates arrays to avoid unpredictable results of methods which rely
on the bounding box.
bounds = null;
| public void | reset()Resets the current Polygon to an empty Polygon. More precisely, the
number of Polygon vertices is set to zero, but x, y coordinates arrays
are not affected.
npoints = 0;
bounds = null;
| public void | translate(int mx, int my)Translates all vertices of Polygon the specified distances along X, Y
axis.
for (int i = 0; i < npoints; i++) {
xpoints[i] += mx;
ypoints[i] += my;
}
if (bounds != null) {
bounds.translate(mx, my);
}
|
|