Draw a Polygon in Java Applet

Polygon:

Polygons are shapes with many sides. It may be considered a set of lines connected. The end of the first line is the beginning of the second line, the end of the second is the beginning of the third, and so on. It suggests that we can draw a polygon with n sides using the drawLine() method n times in succession.

We can draw polygons more conveniently using the drawPolygon() method of Graphics class. This method takes three arguments:

1. An array of integers containing x coordinates.
2. An array of integers containing y coordinates.
3. An integer for the total number of points.

Example:

import java.awt.*;
import java.applet.*;
public class polygon extends Applet
{
int a1[]={20, 120, 220, 20};
int b1[]={20, 120, 20, 20};
int n1=4;
int a2[]= {120, 220, 220, 120};
int b2[]= {120, 20, 220, 120};
int n2=4;
public void paint(Graphics g)
{
g.drawPolygon(a1,b1,n1);
g.fillPolygon(a2,b2,n2);
}
}

Output:
Draw a Polygon in Java Applet