Polygon
Name
Polygon
Description
The Polygon
class encapsulates a collection of points used to create a series of line segments.
Class Definition
public class java.awt.Polygon extends java.lang.Object implements java.awt.Shape, java.io.Serializable { // Variables protected Rectangle bounds; public int npoints; public int xpoints[]; public int ypoints[]; // Constructors public Polygon(); public Polygon (int xpoints[], int ypoints, int npoints); // Instance Methods public void addPoint (int x, int y); public boolean contains (int x, int y); public boolean contains (Point p); public Rectangle getBoundingBox();public Rectangle getBounds(); public boolean inside (int x,int y);
public void translate (int deltaX, int deltaY); }
Variables
bounds
protected Rectangle bounds
The rectangle that describes the boundaries of the Polygon
.
npoints
public int npoints
The number of elements to use in the xpoints
and ypoints
arrays.
xpoints
public int xpoints[]
The array of x coordinates for each point.
ypoints
public int ypoints[]
The array of y coordinates for each point.
Constructors
Polygon
public Polygon()
- Description
- Constructs an empty
Polygon
object with no points.
public Polygon (int xPoints[], int yPoints[], int numPoints)
- Parameters
-
- xPoints[]
- The initial array of x coordinates for each point.
- yPoints[]
- The initial array of y coordinates for each point.
- numPoints
- The number of elements in both
xPoints
andyPoints
arrays to use.
- Throws
-
ArrayIndexOutOfBoundsException
- If
numPoints
>xPoints.length
ornumPoints
>yPoints.length
.
- Description
- Constructs a
Polygon
object with the set of points provided.
Instance Methods
addPoint
public void addPoint (int x, int y)
- Parameters
-
- x
- The x coordinate of the point to be added.
- y
- The y coordinate of the point to be added.
- Description
- Adds the point (
x
,y
) to the end of the list of points for thePolygon
.
contains
public boolean contains (int x, int y)
- Parameters
-
- x
- The x coordinate to test.
- y
- The y coordinate to test.
- Returns
true
if thePolygon
contains the point;false
otherwise.
public boolean contains (Point p)
- Parameters
-
- p
- The point to be tested.
- Returns
true
if thePolygon
contains the point;false
otherwise.
getBoundingBox
public Rectangle getBoundingBox() 
- Returns
- Bounding Rectangle of the points within the
Polygon
. - Description
- Returns the smallest
Rectangle
that contains all the points within thePolygon
. Replaced bygetBounds()
.
getBounds
public Rectangle getBounds()
- Implements
Shape.getBounds()
- Returns
- Bounding Rectangle of the points within the
Polygon
. - Description
- Returns the smallest
Rectangle
that contains all the points within thePolygon
.
inside
public boolean inside (int x,int y) 
- Parameters
-
- x
- The x coordinate of the point to be checked.
- y
- The y coordinate of the point to be checked.
- Returns
true
if (x
,y
) withinPolygon
,false
otherwise.- Description
- Checks to see if the (
x
,y
) point is within an area that would be filled if thePolygon
was drawn withGraphics.fillPolygon()
. Replaced bycontains(int, int)
.
translate
public void translate (int deltaX, int deltaY)
- Parameters
-
- deltaX
- Amount to move horizontally.
- deltaY
- Amount to move vertically.
- Description
- Moves the
Polygon
to the location (x+deltaX
,y+deltaY
).
See Also
Graphics
, Object
, Rectangle