FileDocCategorySizeDatePackage
Polygon.javaAPI DocAndroid 1.5 API16784Wed May 06 22:41:54 BST 2009java.awt

Polygon

public class Polygon extends Object implements Serializable, Shape
The 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.
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
The Constant serialVersionUID.
private static final int
BUFFER_CAPACITY
The points buffer capacity.
public int
npoints
The number of Polygon vertices.
public int[]
xpoints
The array of X coordinates of the vertices.
public int[]
ypoints
The array of Y coordinates of the vertices.
protected Rectangle
bounds
The 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.

param
xpoints the array of X vertex coordinates.
param
ypoints the array of Y vertex coordinates.
param
npoints the number vertices of the polygon.
throws
IndexOutOfBoundsException if the length of xpoints or ypoints is less than n.
throws
NegativeArraySizeException if n is negative.

        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 voidaddPoint(int px, int py)
Adds the point to the Polygon and updates the bounding box accordingly.

param
px the X coordinate of the added vertex.
param
py the Y coordinate of the added vertex.

        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 booleancontains(int x, int y)
Checks whether or not the point given by the coordinates x, y lies inside the Polygon.

param
x the X coordinate of the point to check.
param
y the Y coordinate of the point to check.
return
true, if the specified point lies inside the Polygon, false otherwise.

        return contains((double)x, (double)y);
    
public booleancontains(double x, double y)
Checks whether or not the point with specified double coordinates lies inside the Polygon.

param
x the X coordinate of the point to check.
param
y the Y coordinate of the point to check.
return
true, if the point given by the double coordinates lies inside the Polygon, false otherwise.
see
java.awt.Shape#contains(double, double)

        return Crossing.isInsideEvenOdd(Crossing.crossShape(this, x, y));
    
public booleancontains(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.

param
x the X coordinate of the rectangles's left upper corner as a double.
param
y the Y coordinate of the rectangles's left upper corner as a double.
param
width the width of rectangle as a double.
param
height the height of rectangle as a double.
return
true, if the specified rectangle lies inside the Polygon, false otherwise.
see
java.awt.Shape#contains(double, double, double, double)

        int cross = Crossing.intersectShape(this, x, y, width, height);
        return cross != Crossing.CROSSING && Crossing.isInsideEvenOdd(cross);
    
public booleancontains(java.awt.geom.Rectangle2D rect)
Checks whether or not the specified rectangle lies inside the Polygon.

param
rect the Rectangle2D object.
return
true, if the specified rectangle lies inside the Polygon, false otherwise.
see
java.awt.Shape#contains(java.awt.geom.Rectangle2D)

        return contains(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
    
public booleancontains(java.awt.Point point)
Checks whether or not the specified Point lies inside the Polygon.

param
point the Point object.
return
true, if the specified Point lies inside the Polygon, false otherwise.

        return contains(point.getX(), point.getY());
    
public booleancontains(java.awt.geom.Point2D point)
Checks whether or not the specified Point2D lies inside the Polygon.

param
point the Point2D object.
return
true, if the specified Point2D lies inside the Polygon, false otherwise.
see
java.awt.Shape#contains(java.awt.geom.Point2D)

        return contains(point.getX(), point.getY());
    
public java.awt.RectanglegetBoundingBox()
Gets the bounding rectangle of the Polygon. The bounding rectangle is the smallest rectangle which contains the Polygon.

return
the bounding rectangle of the Polygon.
deprecated
Use getBounds() method.

        return getBounds();
    
public java.awt.RectanglegetBounds()
Gets the bounding rectangle of the Polygon. The bounding rectangle is the smallest rectangle which contains the Polygon.

return
the bounding rectangle of the Polygon.
see
java.awt.Shape#getBounds()

        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.Rectangle2DgetBounds2D()
Gets the Rectangle2D which represents Polygon bounds. The bounding rectangle is the smallest rectangle which contains the Polygon.

return
the bounding rectangle of the Polygon.
see
java.awt.Shape#getBounds2D()

        return getBounds().getBounds2D();
    
public java.awt.geom.PathIteratorgetPathIterator(java.awt.geom.AffineTransform t)
Gets the PathIterator object which gives the coordinates of the polygon, transformed according to the specified AffineTransform.

param
t the specified AffineTransform object or null.
return
PathIterator object for the Polygon.
see
java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)

        return new Iterator(t, this);
    
public java.awt.geom.PathIteratorgetPathIterator(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.

param
t the specified AffineTransform object or null.
param
flatness the maximum number of the control points for a given curve which varies from colinear before a subdivided curve is replaced by a straight line connecting the endpoints. This parameter is ignored for the Polygon class.
return
PathIterator object for the Polygon.
see
java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform, double)

        return new Iterator(t, this);
    
public booleaninside(int x, int y)
Checks whether or not the point given by the coordinates x, y lies inside the Polygon.

param
x the X coordinate of the point to check.
param
y the Y coordinate of the point to check.
return
true, if the specified point lies inside the Polygon, false otherwise.
deprecated
Use contains(int, int) method.

        return contains((double)x, (double)y);
    
public booleanintersects(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.

param
x the X coordinate of the rectangles's left upper corner as a double.
param
y the Y coordinate of the rectangles's left upper corner as a double.
param
width the width of rectangle as a double.
param
height the height of rectangle as a double.
return
true, if the specified rectangle intersects the interior of the Polygon, false otherwise.
see
java.awt.Shape#intersects(double, double, double, double)

        int cross = Crossing.intersectShape(this, x, y, width, height);
        return cross == Crossing.CROSSING || Crossing.isInsideEvenOdd(cross);
    
public booleanintersects(java.awt.geom.Rectangle2D rect)
Checks whether or not the interior of rectangle specified by the Rectangle2D object intersects the interior of the Polygon.

param
rect the Rectangle2D object.
return
true, if the Rectangle2D intersects the interior of the Polygon, false otherwise.
see
java.awt.Shape#intersects(java.awt.geom.Rectangle2D)

        return intersects(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
    
public voidinvalidate()
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 voidreset()
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 voidtranslate(int mx, int my)
Translates all vertices of Polygon the specified distances along X, Y axis.

param
mx the distance to translate horizontally.
param
my the distance to translate vertically.

        for (int i = 0; i < npoints; i++) {
            xpoints[i] += mx;
            ypoints[i] += my;
        }
        if (bounds != null) {
            bounds.translate(mx, my);
        }