Class Polygon2D

  • All Implemented Interfaces:
    java.awt.Shape, java.io.Serializable, java.lang.Cloneable

    public class Polygon2D
    extends java.lang.Object
    implements java.awt.Shape, java.lang.Cloneable, java.io.Serializable
    This class is a Polygon with float coordinates.
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.awt.geom.Rectangle2D bounds
      Bounds of the Polygon2D.
      int npoints
      The total number of points.
      float[] xpoints
      The array of x coordinates.
      float[] ypoints
      The array of x coordinates.
    • Constructor Summary

      Constructors 
      Constructor Description
      Polygon2D()
      Creates an empty Polygon2D.
      Polygon2D​(float[] xpoints, float[] ypoints, int npoints)
      Constructs and initializes a Polygon2D from the specified parameters.
      Polygon2D​(int[] xpoints, int[] ypoints, int npoints)
      Constructs and initializes a Polygon2D from the specified parameters.
      Polygon2D​(java.awt.geom.Rectangle2D rec)
      Constructs and initializes a Polygon2D from the specified Rectangle2D.
      Polygon2D​(java.awt.Polygon pol)
      Constructs and initializes a Polygon2D from the specified Polygon.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addPoint​(float x, float y)
      Appends the specified coordinates to this Polygon2D.
      void addPoint​(java.awt.geom.Point2D p)  
      java.lang.Object clone()  
      boolean contains​(double x, double y)
      Determines if the specified coordinates are inside this Polygon.
      boolean contains​(double x, double y, double w, double h)
      Tests if the interior of this Polygon entirely contains the specified set of rectangular coordinates.
      boolean contains​(int x, int y)
      Determines whether the specified coordinates are inside this Polygon.
      boolean contains​(java.awt.geom.Point2D p)
      Tests if a specified Point2D is inside the boundary of this Polygon.
      boolean contains​(java.awt.geom.Rectangle2D r)
      Tests if the interior of this Polygon entirely contains the specified Rectangle2D.
      boolean contains​(java.awt.Point p)
      Determines whether the specified Point is inside this Polygon.
      java.awt.Rectangle getBounds()  
      java.awt.geom.Rectangle2D getBounds2D()
      Returns the high precision bounding box of the Shape.
      java.awt.geom.PathIterator getPathIterator​(java.awt.geom.AffineTransform at)
      Returns an iterator object that iterates along the boundary of this Polygon and provides access to the geometry of the outline of this Polygon.
      java.awt.geom.PathIterator getPathIterator​(java.awt.geom.AffineTransform at, double flatness)
      Returns an iterator object that iterates along the boundary of the Polygon2D and provides access to the geometry of the outline of the Shape.
      java.awt.Polygon getPolygon()  
      Polyline2D getPolyline2D()  
      boolean intersects​(double x, double y, double w, double h)
      Tests if the interior of this Polygon intersects the interior of a specified set of rectangular coordinates.
      boolean intersects​(java.awt.geom.Rectangle2D r)
      Tests if the interior of this Polygon intersects the interior of a specified Rectangle2D.
      void reset()
      Resets this Polygon object to an empty polygon.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • npoints

        public int npoints
        The total number of points. The value of npoints represents the number of valid points in this Polygon.
      • xpoints

        public float[] xpoints
        The array of x coordinates. The value of npoints is equal to the number of points in this Polygon2D.
      • ypoints

        public float[] ypoints
        The array of x coordinates. The value of npoints is equal to the number of points in this Polygon2D.
      • bounds

        protected java.awt.geom.Rectangle2D bounds
        Bounds of the Polygon2D.
        See Also:
        getBounds()
    • Constructor Detail

      • Polygon2D

        public Polygon2D()
        Creates an empty Polygon2D.
      • Polygon2D

        public Polygon2D​(java.awt.geom.Rectangle2D rec)
        Constructs and initializes a Polygon2D from the specified Rectangle2D.
        Parameters:
        rec - the Rectangle2D
        Throws:
        java.lang.NullPointerException - rec is null.
      • Polygon2D

        public Polygon2D​(java.awt.Polygon pol)
        Constructs and initializes a Polygon2D from the specified Polygon.
        Parameters:
        pol - the Polygon
        Throws:
        java.lang.NullPointerException - pol is null.
      • Polygon2D

        public Polygon2D​(float[] xpoints,
                         float[] ypoints,
                         int npoints)
        Constructs and initializes a Polygon2D 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 Polygon2D
        Throws:
        java.lang.NegativeArraySizeException - if the value of npoints is negative.
        java.lang.IndexOutOfBoundsException - if npoints is greater than the length of xpoints or the length of ypoints.
        java.lang.NullPointerException - if xpoints or ypoints is null.
      • Polygon2D

        public Polygon2D​(int[] xpoints,
                         int[] ypoints,
                         int npoints)
        Constructs and initializes a Polygon2D 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 Polygon2D
        Throws:
        java.lang.NegativeArraySizeException - if the value of npoints is negative.
        java.lang.IndexOutOfBoundsException - if npoints is greater than the length of xpoints or the length of ypoints.
        java.lang.NullPointerException - if xpoints or ypoints is null.
    • Method Detail

      • reset

        public void reset()
        Resets this Polygon object to an empty polygon.
      • clone

        public java.lang.Object clone()
        Overrides:
        clone in class java.lang.Object
      • getPolyline2D

        public Polyline2D getPolyline2D()
      • getPolygon

        public java.awt.Polygon getPolygon()
      • addPoint

        public void addPoint​(java.awt.geom.Point2D p)
      • addPoint

        public void addPoint​(float x,
                             float y)
        Appends the specified coordinates to this Polygon2D.
        Parameters:
        x - the specified x coordinate
        y - the specified y coordinate
      • contains

        public boolean contains​(java.awt.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.
        See Also:
        contains(double, double)
      • contains

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

        Parameters:
        x - the specified x coordinate to be tested
        y - the specified y coordinate to be tested
        Returns:
        true if this Polygon contains the specified coordinates, (xy); false otherwise.
      • getBounds2D

        public java.awt.geom.Rectangle2D getBounds2D()
        Returns the high precision bounding box of the Shape.
        Specified by:
        getBounds2D in interface java.awt.Shape
        Returns:
        a Rectangle2D that precisely bounds the Shape.
      • getBounds

        public java.awt.Rectangle getBounds()
        Specified by:
        getBounds in interface java.awt.Shape
      • contains

        public boolean contains​(double x,
                                double y)
        Determines if the specified coordinates are inside this Polygon. For the definition of insideness, see the class comments of Shape.
        Specified by:
        contains in interface java.awt.Shape
        Parameters:
        x - the specified x coordinate
        y - the specified y coordinate
        Returns:
        true if the Polygon contains the specified coordinates; false otherwise.
      • contains

        public boolean contains​(java.awt.geom.Point2D p)
        Tests if a specified Point2D is inside the boundary of this Polygon.
        Specified by:
        contains in interface java.awt.Shape
        Parameters:
        p - a specified Point2D
        Returns:
        true if this Polygon contains the specified Point2D; false otherwise.
        See Also:
        contains(double, double)
      • intersects

        public boolean intersects​(double x,
                                  double y,
                                  double w,
                                  double h)
        Tests if the interior of this Polygon intersects the interior of a specified set of rectangular coordinates.
        Specified by:
        intersects in interface java.awt.Shape
        Parameters:
        x - the x coordinate of the specified rectangular shape's top-left corner
        y - the y coordinate of the specified rectangular shape's top-left corner
        w - the width of the specified rectangular shape
        h - the height of the specified rectangular shape
        Returns:
        true if the interior of this Polygon and the interior of the specified set of rectangular coordinates intersect each other; false otherwise.
      • intersects

        public boolean intersects​(java.awt.geom.Rectangle2D r)
        Tests if the interior of this Polygon intersects the interior of a specified Rectangle2D.
        Specified by:
        intersects in interface java.awt.Shape
        Parameters:
        r - a specified Rectangle2D
        Returns:
        true if this Polygon and the interior of the specified Rectangle2D intersect each other; false otherwise.
      • contains

        public boolean contains​(double x,
                                double y,
                                double w,
                                double h)
        Tests if the interior of this Polygon entirely contains the specified set of rectangular coordinates.
        Specified by:
        contains in interface java.awt.Shape
        Parameters:
        x - the x coordinate of the top-left corner of the specified set of rectangular coordinates
        y - the y coordinate of the top-left corner of the specified set of rectangular coordinates
        w - the width of the set of rectangular coordinates
        h - the height of the set of rectangular coordinates
        Returns:
        true if this Polygon entirely contains the specified set of rectangular coordinates; false otherwise.
      • contains

        public boolean contains​(java.awt.geom.Rectangle2D r)
        Tests if the interior of this Polygon entirely contains the specified Rectangle2D.
        Specified by:
        contains in interface java.awt.Shape
        Parameters:
        r - the specified Rectangle2D
        Returns:
        true if this Polygon entirely contains the specified Rectangle2D; false otherwise.
        See Also:
        contains(double, double, double, double)
      • getPathIterator

        public java.awt.geom.PathIterator getPathIterator​(java.awt.geom.AffineTransform at)
        Returns an iterator object that iterates along the boundary of this Polygon and provides access to the geometry of the outline of this Polygon. An optional AffineTransform can be specified so that the coordinates returned in the iteration are transformed accordingly.
        Specified by:
        getPathIterator in interface java.awt.Shape
        Parameters:
        at - an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired
        Returns:
        a PathIterator object that provides access to the geometry of this Polygon.
      • getPathIterator

        public java.awt.geom.PathIterator getPathIterator​(java.awt.geom.AffineTransform at,
                                                          double flatness)
        Returns an iterator object that iterates along the boundary of the Polygon2D and provides access to the geometry of the outline of the Shape. Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types are returned by the iterator. Since polygons are already flat, the flatness parameter is ignored.
        Specified by:
        getPathIterator in interface java.awt.Shape
        Parameters:
        at - an optional AffineTransform to be applied to the coordinates as they are returned in the iteration, or null if untransformed coordinates are desired
        flatness - the maximum amount that the control points for a given curve can vary from colinear before a subdivided curve is replaced by a straight line connecting the endpoints. Since polygons are already flat the flatness parameter is ignored.
        Returns:
        a PathIterator object that provides access to the Shape object's geometry.