Polygon class in java

java.awt.Polygon is a class in java programming language. Inside of this class exists 18 public methods. Declaration of this class as follows.

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

Polygon public method

There are following useful methods which is define the inside of java.awt.Polygon class.

MethodDescription
void addPoint(int x, int y) It appends the specified coordinates to this .
boolean contains(double x, double y) It tests if the specified coordinates are inside the boundary of the , as described by the definition of insideness.
boolean contains(double x, double y, double w, double h) It tests if the interior of the entirely contains the specified rectangular area.
boolean contains(int x, int y) It determines whether the specified coordinates are inside this .
boolean contains(Point p) It determines whether the specified is inside this Polygon.
boolean contains(Point2D p) It tests if a specified is inside the boundary of the Shape, as described by the definition of insideness.
boolean contains(Rectangle2D r) It tests if the interior of the entirely contains the specified Rectangle2D.
Rectangle getBoundingBox() It deprecated. As of JDK version 1.1, replaced by .
Rectangle getBounds() It gets the bounding box of this .
Rectangle2D getBounds2D() It returns a high precision and more accurate bounding box of the than the getBounds method.
PathIterator getPathIterator(AffineTransform at) It returns an iterator object that iterates along the boundary of this and provides access to the geometry of the outline of this Polygon.
PathIterator getPathIterator(AffineTransform at, double flatness) It returns an iterator object that iterates along the boundary of the and provides access to the geometry of the outline of the Shape.
boolean inside(int x, int y) It deprecated. As of JDK version 1.1, replaced by .
boolean intersects(double x, double y, double w, double h) It tests if the interior of the intersects the interior of a specified rectangular area.
boolean intersects(Rectangle2D r) It tests if the interior of the intersects the interior of a specified Rectangle2D.
void invalidate() It invalidates or flushes any internally-cached data that depends on the vertex coordinates of this .
void reset() It resets this object to an empty polygon.
void translate(int deltaX, int deltaY) It translates the vertices of the by deltaX along the x axis and by deltaY along the y axis.

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.



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







© 2021, kalkicode.com, All rights reserved