Skip to main content

Polygon class in java

java.awt.Polygon is a class in the Java AWT (Abstract Window Toolkit) package that represents a closed polygonal shape. It is defined by a set of vertices, which are connected by line segments to form a closed shape.

The Polygon class provides methods to add vertices, remove vertices, get the number of vertices, and retrieve the coordinates of each vertex. It also provides methods to check whether a given point is inside the polygon or not.

The Polygon class can be used to draw and manipulate simple geometric shapes, such as triangles, rectangles, and polygons, on a graphics canvas. It is often used in graphical user interfaces (GUIs) and other types of graphics applications to represent shapes and regions of interest.

Here are some of the important methods of the java.awt.Polygon class:

  1. public void addPoint(int x, int y): Adds a new vertex to the polygon at the specified coordinates (x, y).

  2. public void addPoint(Point p): Adds a new vertex to the polygon at the specified point p.

  3. public void invalidate(): Invalidates the polygon's bounding box. This is called automatically when the polygon is modified.

  4. public void translate(int deltaX, int deltaY): Translates the polygon by the specified amount (deltaX, deltaY).

  5. public void reset(): Removes all vertices from the polygon.

  6. public int npoints(): Returns the number of vertices in the polygon.

  7. public int xpoints[] and public int ypoints[]: Returns the arrays containing the x-coordinates and y-coordinates of the vertices of the polygon.

  8. public boolean contains(int x, int y): Returns true if the specified point (x, y) is inside the polygon.

  9. public boolean contains(Point p): Returns true if the specified point p is inside the polygon.

  10. public Rectangle getBounds(): Returns the bounding rectangle of the polygon.

  11. public void draw(Graphics g): Draws the polygon using the specified graphics context g.

  12. public void fill(Graphics g): Fills the polygon using the specified graphics context g.

Note that java.awt.Polygon inherits several methods from its superclass, java.awt.Shape, such as contains(Rectangle2D) and intersects(Rectangle2D).

Declaration of this class as follows.

public class java.awt.Polygon 
implements java.awt.Shape,java.io.Serializable 

This reference is belong to javadoc

Public Constructors

There is 2 types of public constructor is defined inside the class, Whose syntax as follows.

public java.awt.Polygon()
public java.awt.Polygon(int[],int[],int)

Example of those constructors.

// Instance of java.awt.Polygon class 
// Useful packages
import java.awt.Polygon;
class Example {
    public static void main(String[] args) {
        

        int [] xpoints = {400, 400, 460, 460, 440, 440, 420, 420, 400};
        int [] ypoints = {400, 460, 460, 400, 400, 440, 440, 400, 400};
        // Create Instance
        Polygon  p1 = new Polygon();
        Polygon  p2 = new Polygon(xpoints,ypoints,xpoints.length);
    }
}
Instance of java.awt.Polygon class in java

java.awt.Polygon addPoint(int, int) method example

// addPoint(int, int) method in java.awt.Polygon class 
/*
  File : Demo.java
*/
import java.awt.Polygon;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
public class Demo extends JApplet
{
	public void init()
	{
		setSize(200, 200);
		show();
	}
	public void start()
	{}
	public void stop()
	{}
	public void paint(Graphics g)
	{
		Polygon p = new Polygon();
		// add point using
		// x and y coordinates 
		p.addPoint(120, 120);
		p.addPoint(120, 180);
		p.addPoint(180, 180);
		p.addPoint(180, 130);
		p.addPoint(80, 130);
		p.addPoint(80, 45);
		p.addPoint(15, 45);
		p.addPoint(15, 90);
		p.addPoint(120, 90);
		g.setColor(Color.pink);
		// print polygon
		g.drawPolygon(p);
	}
}
/*
applet File : Demo.html
<applet code="Demo" width=200 height=60>
</applet>
*/
// Compile : javac Demo.java
// Run     : appletviewer Demo.html
program result

java.awt.Polygon contains(double, double, double, double) method example

// contains(double, double, double, double) 
// method in java.awt.Polygon class 
// Useful packages
import java.awt.Polygon;
class Example {
    public static void main(String[] args) {
        
        Polygon p = new Polygon();

        /*
            Polygon Drow

            (20,20)      (80,20)
                A--------D
                |        |
                |        |
                B--------C   
            (20,80)      (80,80)
        */
        // add point using
        // x and y coordinates 
        p.addPoint(20, 20);
        p.addPoint(20, 80);
        p.addPoint(80, 80);
        p.addPoint(80, 20);
 
        // contains(double x, double y, double w, double h)
        if(p.contains(30.50,25.02,2,4))
        {
            System.out.println(" Test A Pass ");
        }
        else
        {
            System.out.println(" Test A Fail ");
        }
  
        // contains(double x, double y, double w, double h)
        if(p.contains(90.50,25.02,2,4))
        {
            System.out.println(" Test B Pass ");
        }
        else
        {
            System.out.println(" Test B Fail ");
        }
    }
}
 Test A Pass 
 Test B Fail

java.awt.Polygon contains(int, int) method example

// contains(int x, int y) 
// method in java.awt.Polygon class 
// Useful packages
import java.awt.Polygon;
class Example {
    public static void main(String[] args) {
        
        Polygon p = new Polygon();

        /*
            Polygon Drow

            (20,20)      (80,20)
                A————————D
                |        |
                |        |
                B————————C   
            (20,80)      (80,80)
        */
        // add point using
        // x and y coordinates 
        p.addPoint(20, 20);
        p.addPoint(20, 80);
        p.addPoint(80, 80);
        p.addPoint(80, 20);
 
        // contains(int x, int y)
        /*
            x = 10, y = 40

            (20,20)      (80,20)
                A————————D
            ဝ   |        |
        (10,40) |        |
                B————————C   
            (20,80)      (80,80)
        */
        if(p.contains(10,40))
        {
            System.out.println(" Test (10,40) Pass ");
        }
        else
        {
            System.out.println(" Test (10,40) Fail ");
        }

        /*
            x = 50, y = 50

            (20,20)      (80,20)
                A————————D
                |   ဝ    |
                |        |
                B————————C   
            (20,80)      (80,80)
        */
        // contains(int x, int y)
        if(p.contains(50,50))
        {
            System.out.println(" Test (50,50) Pass ");
        }
        else
        {
            System.out.println(" Test (50,50) Fail ");
        }
    }
}
 Test (10,40) Fail 
 Test (50,50) Pass 

Other methods are coming soon.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment