JSR-62 (Final)

java.awt
Class Polygon

java.lang.Object
  |
  +--java.awt.Polygon
All Implemented Interfaces:
Serializable, Shape

public class Polygon
extends Object
implements Shape, Serializable

The Polygon class encapsulates a description of a closed, two-dimensional 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 (xy) 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 (xy) points are joined by a line segment that closes the polygon. This Polygon is defined with an even-odd winding rule. This class's hit-testing methods, which include the contains, intersects and inside methods, use the insideness definition described in the Shape class comments.

Since:
JDK1.0
See Also:
Shape, Serialized Form

Field 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 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

npoints

public int npoints
The total number of points. This value can be NULL.

See Also:
addPoint(int, int)

xpoints

public int[] xpoints
The array of x coordinates.

See Also:
addPoint(int, int)

ypoints

public int[] ypoints
The array of y coordinates.

See Also:
addPoint(int, int)

bounds

protected Rectangle bounds
Bounds of the polygon. This value can be NULL. Please see the javadoc comments getBounds().

See Also:
getBoundingBox(), getBounds()
Constructor Detail

Polygon

public Polygon()
Creates an empty polygon.


Polygon

public Polygon(int[] xpoints,
               int[] ypoints,
               int npoints)
Constructs and initializes a Polygon from the specified parameters.

Parameters:
xpoints - an array of x coordinates
ypoints - an array of y coordinates
npoints - the total number of points in the Polygon
Throws:
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

translate

public 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.

Parameters:
deltaX - the amount to translate along the x axis
deltaY - the amount to translate along the y axis
Since:
JDK1.1

addPoint

public void addPoint(int x,
                     int y)
Appends the specified coordinates to this 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.

See Also:
getBounds(), contains(java.awt.Point)

getBounds

public Rectangle getBounds()
Gets the bounding box of this 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.

Specified by:
getBounds in interface Shape
Returns:
a Rectangle that defines the bounds of this Polygon.
Since:
JDK1.1

getBoundingBox

public Rectangle getBoundingBox()
Deprecated. As of JDK version 1.1, replaced by getBounds().

Returns the bounds of this Polygon.

Returns:
the bounds of this Polygon.

contains

public boolean contains(Point p)
Determines whether the specified Point is inside this Polygon.

Parameters:
p - the specified Point to be tested
Returns:
true if the Polygon contains the Point; false otherwise.

contains

public boolean contains(int x,
                        int y)
Determines whether the specified coordinates are inside this Polygon.

Returns:
true if this Polygon contains the specified coordinates, (xy); false otherwise.
Since:
JDK1.1

inside

public boolean inside(int x,
                      int y)
Deprecated. As of JDK version 1.1, replaced by contains(int, int).

Determines whether the specified coordinates are contained in this Polygon.

Returns:
true if this Polygon contains the specified coordinates, (xy); false otherwise.

JSR-62 (Final)

Java and Java 2D are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
Copyright 1993 - 2002 Sun Microsystems, Inc., 901 San Antonio Road, Palo Alto, California 94303, U.S.A. All Rights Reserved.
Use of this specification is subject to this license.