diva.util.java2d
Class Polygon2D

java.lang.Object
  extended by diva.util.java2d.Polygon2D
All Implemented Interfaces:
java.awt.Shape
Direct Known Subclasses:
Polygon2D.Double, Polygon2D.Float

public abstract class Polygon2D
extends java.lang.Object
implements java.awt.Shape

A polygon shape. This class implements java.awt.Shape, and consists of a series of straight-line segments. This class should be used instead of GeneralPath for shapes that consist only of straight-line segments and are always closed. It is more efficient than GeneralPath, and allows the coordinates of vertices to be modified. Following the convention set by the Java2D shape classes, the Polygon class is an abstract class, which contains two concrete inner classes, one storing floats and one storing doubles.

Version:
$Id: Polygon2D.java 54721 2009-06-26 22:32:23Z cxh $
Author:
John Reekie

Nested Class Summary
static class Polygon2D.Double
          The concrete Polygon class that stores coordinates internally as doubles.
static class Polygon2D.Float
          The concrete Polygon class that stores coordinates internally as floats.
 
Field Summary
protected  boolean _closed
          The flag that says the the polygon has been closed
protected  int _coordCount
          The current number of coordinates
 
Constructor Summary
Polygon2D()
           
 
Method Summary
 void closePath()
          Close the polygon.
 boolean contains(double x, double y)
          Return true if the given point is inside the polygon.
 boolean contains(double x1, double y1, double w, double h)
          Return true if the given rectangle is entirely inside the polygon.
 boolean contains(java.awt.geom.Point2D p)
          Return true if the given point is inside the polygon.
 boolean contains(java.awt.geom.Rectangle2D r)
          Return true if the given rectangle is entirely inside the polygon.
 java.awt.Rectangle getBounds()
          Get the integer bounds of the polygon.
abstract  java.awt.geom.Rectangle2D getBounds2D()
          Get the floating-point bounds of the polygon.
 java.awt.geom.PathIterator getPathIterator(java.awt.geom.AffineTransform at)
          Get a path iterator over the object.
 java.awt.geom.PathIterator getPathIterator(java.awt.geom.AffineTransform at, double flatness)
          Get a path iterator over the object.
 int getVertexCount()
          Get the number of vertices
abstract  double getX(int index)
          Get the given X-coordinate
abstract  double getY(int index)
          Get the given Y-coordinate
 boolean intersects(double x1, double y1, double w, double h)
          Test if the polygon is intersected by the given rectangle.
 boolean intersects(java.awt.geom.Rectangle2D r)
          Test if the polygon is intersected by the given rectangle.
abstract  void lineTo(double x, double y)
          Add a new vertex to the end of the polygon.
abstract  void moveTo(double x, double y)
          Move the start point of the vertex to the given position.
 void reset()
          Reset the polygon back to empty.
abstract  void setX(int index, double x)
          Set the given X-coordinate.
abstract  void setY(int index, double y)
          Set the given Y-coordinate
 java.lang.String toString()
          Return a string representation of the polygon.
abstract  void transform(java.awt.geom.AffineTransform at)
          Transform the polygon with the given transform.
abstract  void translate(double x, double y)
          Translate the polygon the given distance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_coordCount

protected int _coordCount
The current number of coordinates


_closed

protected boolean _closed
The flag that says the the polygon has been closed

Constructor Detail

Polygon2D

public Polygon2D()
Method Detail

closePath

public void closePath()
Close the polygon. No further segments can be added. If this method not called, then the path iterators will treat the polygon as thought it were closed, and implicitly join the most recently added vertex to the first one. However, this method should generally be called, as if the last vertex is the same as the first vertex, then it merges them.


contains

public boolean contains(double x,
                        double y)
Return true if the given point is inside the polygon. This method uses a straight-forward algorithm, where a point is taken to be inside the polygon if a horizontal line extended from the point to infinity intersects an odd number of segments.

Specified by:
contains in interface java.awt.Shape

contains

public boolean contains(java.awt.geom.Point2D p)
Return true if the given point is inside the polygon.

Specified by:
contains in interface java.awt.Shape

contains

public boolean contains(java.awt.geom.Rectangle2D r)
Return true if the given rectangle is entirely inside the polygon. (Currently, this algorithm can be fooled by supplying a rectangle that has all four corners inside the polygon, but which intersects some edges.)

Specified by:
contains in interface java.awt.Shape

contains

public boolean contains(double x1,
                        double y1,
                        double w,
                        double h)
Return true if the given rectangle is entirely inside the polygon. (Currently, this algorithm can be fooled by supplying a rectangle that has all four corners inside the polygon, but which intersects some edges.)

Specified by:
contains in interface java.awt.Shape

getBounds

public java.awt.Rectangle getBounds()
Get the integer bounds of the polygon.

Specified by:
getBounds in interface java.awt.Shape

getBounds2D

public abstract java.awt.geom.Rectangle2D getBounds2D()
Get the floating-point bounds of the polygon.

Specified by:
getBounds2D in interface java.awt.Shape

getPathIterator

public java.awt.geom.PathIterator getPathIterator(java.awt.geom.AffineTransform at,
                                                  double flatness)
Get a path iterator over the object.

Specified by:
getPathIterator in interface java.awt.Shape

getPathIterator

public java.awt.geom.PathIterator getPathIterator(java.awt.geom.AffineTransform at)
Get a path iterator over the object.

Specified by:
getPathIterator in interface java.awt.Shape

getVertexCount

public int getVertexCount()
Get the number of vertices


getX

public abstract double getX(int index)
Get the given X-coordinate

Throws:
java.lang.IndexOutOfBoundsException - The index is out of bounds.

getY

public abstract double getY(int index)
Get the given Y-coordinate

Throws:
java.lang.IndexOutOfBoundsException - The index is out of bounds.

intersects

public boolean intersects(java.awt.geom.Rectangle2D r)
Test if the polygon is intersected by the given rectangle. (Currently, this algorithm can be fooled by supplying a rectangle that has no corners inside the polygon, and does not contain any vertex of the polygon, but which intersects some edges.)

Specified by:
intersects in interface java.awt.Shape

intersects

public boolean intersects(double x1,
                          double y1,
                          double w,
                          double h)
Test if the polygon is intersected by the given rectangle.

Specified by:
intersects in interface java.awt.Shape

lineTo

public abstract void lineTo(double x,
                            double y)
Add a new vertex to the end of the polygon. Throw an exception of the polygon has already been closed.


moveTo

public abstract void moveTo(double x,
                            double y)
Move the start point of the vertex to the given position. Throw an exception if the line already contains any vertices.


reset

public void reset()
Reset the polygon back to empty.


setX

public abstract void setX(int index,
                          double x)
Set the given X-coordinate.

Throws:
java.lang.IndexOutOfBoundsException - The index is out of bounds.

setY

public abstract void setY(int index,
                          double y)
Set the given Y-coordinate

Throws:
java.lang.IndexOutOfBoundsException - The index is out of bounds.

transform

public abstract void transform(java.awt.geom.AffineTransform at)
Transform the polygon with the given transform.


translate

public abstract void translate(double x,
                               double y)
Translate the polygon the given distance.


toString

public java.lang.String toString()
Return a string representation of the polygon.

Overrides:
toString in class java.lang.Object