Draw a Polygon in Java Applet

Polygon is a closed figure with 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.
We can draw Polygon in java applet by three ways :
- drawPolygon(int[] x, int[] y, int numberofpoints) : draws a polygon with the given set of x and y points.
// Java program to draw polygon using// drawPolygon(int[] x, int[] y, int numberofpoints)// functionimportjava.awt.*;importjavax.swing.*;ÂÂpublicclasspolyextendsJApplet {   Â// called when applet is started   Âpublicvoidinit()   Â{       Â// set the size of applet to 300, 300       ÂsetSize(200,200);       Âshow();   Â}   Â// invoked when applet is started   Âpublicvoidstart()   Â{   Â}   Â// invoked when applet is closed   Âpublicvoidstop()   Â{   Â}   Âpublicvoidpaint(Graphics g)   Â{       Â// x coordinates of vertices       Âintx[] = {10,30,40,50,110,140};       Â// y coordinates of vertices       Âinty[] = {140,110,50,40,30,10};       Â// number of vertices       Âintnumberofpoints =6;       Â// set the color of line drawn to blue       Âg.setColor(Color.blue);       Â// draw the polygon using drawPolygon function       Âg.drawPolygon(x, y, numberofpoints);   Â}}Output :
- drawPolygon(Polygon p) : draws a polygon with the given object of Polygon class.
// Java program to draw polygon// using drawPolygon(Polygon p)// functionimportjava.awt.*;importjavax.swing.*;ÂÂpublicclasspolyextendsJApplet {   Â// called when applet is started   Âpublicvoidinit()   Â{       Â// set the size of applet to 300, 300       ÂsetSize(200,200);       Âshow();   Â}   Â// invoked when applet is started   Âpublicvoidstart()   Â{   Â}   Â// invoked when applet is closed   Âpublicvoidstop()   Â{   Â}   Âpublicvoidpaint(Graphics g)   Â{       Â// x coordinates of vertices       Âintx[] = {10,30,40,50,110,140};       Â// y coordinates of vertices       Âinty[] = {140,110,50,40,30,10};       Â// number of vertices       Âintnumberofpoints =6;       Â// create a polygon with given x, y coordinates       ÂPolygon p =newPolygon(x, y, numberofpoints);       Â// set the color of line drawn to blue       Âg.setColor(Color.blue);       Â// draw the polygon using drawPolygon       Â// function using object of polygon class       Âg.drawPolygon(p);   Â}}Output :
- drawLine(int x, int y, int x1, int y1) : In this method we would connect adjacent vertices with a line segment and also connect the first and last vertex.
// Java code to draw a polygon// using drawLine(int x, int y, int x1, int y1)// functionimportjava.awt.*;importjavax.swing.*;ÂÂpublicclasspolyextendsJApplet {   Â// called when applet is started   Âpublicvoidinit()   Â{       Â// set the size of applet to 300, 300       ÂsetSize(200,200);       Âshow();   Â}   Â// invoked when applet is started   Âpublicvoidstart()   Â{   Â}   Â// invoked when applet is closed   Âpublicvoidstop()   Â{   Â}   Âpublicvoidpaint(Graphics g)   Â{       Â// x coordinates of vertices       Âintx[] = {10,30,40,50,110,140};       Â// y coordinates of vertices       Âinty[] = {140,110,50,40,30,10};       Â// number of vertices       Âintnumberofpoints =6;       Â// set the color of line drawn to blue       Âg.setColor(Color.blue);       Â// join the adjacent vertices       Âfor(inti =0; i < numberofpoints -1; i++)           Âg.drawLine(x[i], y[i], x[i +1], y[i +1]);       Â// join the first and last vertex       Âg.drawLine(x[0], y[0], x[numberofpoints -1], y[numberofpoints -1]);   Â}}Output :
Note: The above function are a part of java.awt package and belongs to java.awt.Graphics class. Also, these codes might not run in an online compiler please use an offline compiler. The x and y coordinates can be changed by the programmer according to their need




