[Chapter 19] Polygon
Author: John Zukowski
Date: Thu Jul 31 15:15:24 1997

#

| Chapter 19 java.awt Reference |

# 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` and `yPoints` arrays to use.

Throws

  `ArrayIndexOutOfBoundsException`

    If `numPoints` > `xPoints.length` or `numPoints` > `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 the `Polygon`.

### contains

#### public boolean contains (int x, int y)

Parameters

  x

    The x coordinate to test.

  y

    The y coordinate to test.

Returns

  `true` if the `Polygon` contains the point; `false` otherwise.

#### public boolean contains (Point p)

Parameters

  p

    The point to be tested.

Returns

  `true` if the `Polygon` 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 the `Polygon`. Replaced by `getBounds()`.

### 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 the `Polygon`.

### 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`) within `Polygon`, `false` otherwise.

Description

  Checks to see if the (`x`, `y`) point is within an area that would be filled if the `Polygon` was drawn with `Graphics.fillPolygon()`. Replaced by `contains(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`

Point |  | PopupMenu