Open In App

Draw a Polygon in Java Applet

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A polygon is a closed figure with a finite set of line segments joining one vertex to the other. The polygon comprises of set of (x, y) coordinate pairs where each pair is the vertex of the polygon. The side of the polygon is the line drawn between two successive coordinate pairs, and a line segment is drawn from the first pair to the last pair.

Note: Java Applets are deprecated since Java 9 and removed in Java 11. Applets are no longer supported in modern browsers or Java versions. For modern applications, use Swing, JavaFX, or Java 2D API. This article is for educational purposes only.

Ways to Draw a Polygon in Java

There are three common ways to draw a polygon in Java applets or Swing components.

1. Using drawPolygon(int[] x, int[] y, int numberOfPoints):

This method is used to draw a polygon using arrays of x and y coordinates along with the number of vertices.

Example:

Java
import java.awt.*;
import javax.swing.*;

public class Geeks extends JApplet {
    
    // Called when the applet is started
    public void init() {
        
        // Set the size of the applet
        setSize(200, 200);
        setVisible(true);  
    }

    // Draw the polygon on the screen
    public void paint(Graphics g) {
        
        // x coordinates of the vertices
        int[] x = {10, 30, 40, 50, 110, 140};

        // y coordinates of the vertices
        int[] y = {140, 110, 50, 40, 30, 10};

        // Number of vertices
        int numberOfPoints = 6;

        // Set the color of the line to blue
        g.setColor(Color.blue);

        // Draw the polygon using drawPolygon function
        g.drawPolygon(x, y, numberOfPoints);
    }
}

Output:


2. Using drawPolygon(Polygon p)

This method draws a polygon with the given object of Polygon class.

Example:

Java
import java.awt.*;
import javax.swing.*;

public class Geeks extends JApplet {
    
    // Called when the applet is started
    public void init() {
        
        // Set the size of the applet
        setSize(200, 200);
        setVisible(true);  
    }

    // Draw the polygon on the screen
    public void paint(Graphics g) {
        
        // x coordinates of the vertices
        int[] x = {10, 30, 40, 50, 110, 140};

        // y coordinates of the vertices
        int[] y = {140, 110, 50, 40, 30, 10};

        // Number of vertices
        int numberOfPoints = 6;

        // Create a polygon object with the given x, y coordinates
        Polygon p = new Polygon(x, y, numberOfPoints);

        // Set the color of the line to blue
        g.setColor(Color.blue);

        // Draw the polygon using the Polygon object
        g.drawPolygon(p);
    }
}

Output:


3. Using drawLine(int x, int y, int x1, int y1)

This method manually connects each pair of vertices using lines and closes the shape by connecting the last and first points.

Example:

Java
import java.awt.*;
import javax.swing.*;

public class Geeks extends JApplet {
    
    // Called when the applet is started
    public void init() {
        
        // Set the size of the applet
        setSize(200, 200);
        setVisible(true);  
    }

    // Draw the polygon on the screen
    public void paint(Graphics g) {
        
        // x coordinates of the vertices
        int[] x = {10, 30, 40, 50, 110, 140};

        // y coordinates of the vertices
        int[] y = {140, 110, 50, 40, 30, 10};

        // Number of vertices
        int numberOfPoints = 6;

        // Set the color of the line to blue
        g.setColor(Color.blue);

        // Draw lines between adjacent vertices
        for (int i = 0; i < numberOfPoints - 1; i++) {
            g.drawLine(x[i], y[i], x[i + 1], y[i + 1]);
        }

        // Connect the first and last vertex
        g.drawLine(x[0], y[0], x[numberOfPoints - 1], 
        y[numberOfPoints - 1]);
    }
}

Output:


Practice Tags :

Similar Reads