Class Polygon

java.lang.Object
armyc2.c2sd.graphics2d.Polygon

public class Polygon extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Rectangle
    The bounds of this Polygon.
    int
    The total number of points.
    int[]
    The array of X coordinates.
    int[]
    The array of Y coordinates.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty polygon.
    Polygon(int[] xpoints, int[] ypoints, int npoints)
    Constructs and initializes a Polygon from the specified parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addPoint(int x, int y)
    Appends the specified coordinates to this Polygon.
    boolean
    contains(double x, double y)
    boolean
    contains(double x, double y, double w, double h)
    boolean
    contains(int x, int y)
    Determines whether the specified coordinates are inside this Polygon.
    boolean
    Determines whether the specified Point is inside this Polygon.
    boolean
    boolean
    Deprecated.
    As of JDK version 1.1, replaced by getBounds().
    Gets the bounding box of this Polygon.
    Returns an iterator object that iterates along the boundary of this Polygon and provides access to the geometry of the outline of this Polygon.
    getPathIterator(AffineTransform at, double flatness)
    Returns an iterator object that iterates along the boundary of the Shape and provides access to the geometry of the outline of the Shape.
    boolean
    inside(int x, int y)
    Deprecated.
    As of JDK version 1.1, replaced by contains(int, int).
    boolean
    intersects(double x, double y, double w, double h)
    boolean
    void
    Invalidates or flushes any internally-cached data that depends on the vertex coordinates of this Polygon.
    void
    Resets this Polygon object to an empty polygon.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • npoints

      public int npoints
      The total number of points. The value of npoints represents the number of valid points in this Polygon and might be less than the number of elements in xpoints or ypoints. This value can be NULL.
      Since:
      1.0
      See Also:
    • xpoints

      public int[] xpoints
      The array of X coordinates. The number of elements in this array might be more than the number of X coordinates in this Polygon. The extra elements allow new points to be added to this Polygon without re-creating this array. The value of npoints is equal to the number of valid points in this Polygon.
      Since:
      1.0
      See Also:
    • ypoints

      public int[] ypoints
      The array of Y coordinates. The number of elements in this array might be more than the number of Y coordinates in this Polygon. The extra elements allow new points to be added to this Polygon without re-creating this array. The value of npoints is equal to the number of valid points in this Polygon.
      Since:
      1.0
      See Also:
    • bounds

      protected Rectangle bounds
      The bounds of this Polygon. This value can be null.
      Since:
      1.0
      See Also:
  • Constructor Details

    • Polygon

      public Polygon()
      Creates an empty polygon.
      Since:
      1.0
    • 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.
      Since:
      1.0
  • Method Details

    • reset

      public void reset()
      Resets this Polygon object to an empty polygon. The coordinate arrays and the data in them are left untouched but the number of points is reset to zero to mark the old vertex data as invalid and to start accumulating new vertex data at the beginning. All internally-cached data relating to the old vertices are discarded. Note that since the coordinate arrays from before the reset are reused, creating a new empty Polygon might be more memory efficient than resetting the current one if the number of vertices in the new polygon data is significantly smaller than the number of vertices in the data from before the reset.
      Since:
      1.4
    • invalidate

      public void invalidate()
      Invalidates or flushes any internally-cached data that depends on the vertex coordinates of this Polygon. This method should be called after any direct manipulation of the coordinates in the xpoints or ypoints arrays to avoid inconsistent results from methods such as getBounds or contains that might cache data from earlier computations relating to the vertex coordinates.
      Since:
      1.4
    • 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.

      Parameters:
      x - the specified X coordinate
      y - the specified Y coordinate
      Since:
      1.0
    • 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.
      Returns:
      a Rectangle that defines the bounds of this Polygon.
      Since:
      1.1
    • getBoundingBox

      Deprecated.
      As of JDK version 1.1, replaced by getBounds().
      Returns the bounds of this Polygon.
      Returns:
      the bounds of this Polygon.
      Since:
      1.0
    • 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.
      Since:
      1.0
      See Also:
    • 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 (x,y); false otherwise.
      Since:
      1.1
      See Also:
    • inside

      @Deprecated 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.
      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 (x,y); false otherwise.
      Since:
      1.0
      See Also:
    • getBounds2D

      Since:
      1.2
    • contains

      public boolean contains(double x, double y)
      Since:
      1.2
    • contains

      public boolean contains(Point2D p)
      Since:
      1.2
    • intersects

      public boolean intersects(double x, double y, double w, double h)
      Since:
      1.2
    • intersects

      public boolean intersects(Rectangle2D r)
      Since:
      1.2
    • contains

      public boolean contains(double x, double y, double w, double h)
      Since:
      1.2
    • contains

      public boolean contains(Rectangle2D r)
      Since:
      1.2
    • getPathIterator

      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.
      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.
      Since:
      1.2
    • getPathIterator

      public PathIterator getPathIterator(AffineTransform at, double flatness)
      Returns an iterator object that iterates along the boundary of the Shape 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. An optional AffineTransform can be specified in which case the coordinates returned in the iteration are transformed accordingly.
      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.
      Since:
      1.2