A instance of Java's polygon class is a sequence of (x,y) coordinates that defines the sides of a geometric polygon. Each pair of consecutive coordinates in the sequence defines one side of the polygon. In order for the polygon to be closed, the final coordinate must be the same as the first.
A particular polygon can be constructed by creating an empty Polygon (via new Polygon()) and using the addPoint method to add points to this polygon. For example, here is code for creating a triangle named tri with coordinates (30, 10), (30, 20), and (20, 15):
Polygon tri = new Polygon(); tri.addPoint(30,10); tri.addPoint(30,20); tri.addPoint(20,15); tri.addPoint(30,10);
Below is a simple contract for the Polygon class. The Polygon class actually implements more constructors and methods than are described here. The full list of methods can be found in the Java API for the class java.awt.Polygon.
public Polygon()
public void addPoint(int x, int y)
x -
the x coordinate of the point
y -
the y coordinate of the point
public Rectangle getBoundingBox()
- Returns:
- the smallest rectangle than contains this polygon
public boolean inside(int x, int y)
x -
the xcoordinate of the point to be tested
y -
the y coordinate of the point to be tested
public int npoints