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 startedpublicvoidinit(){// set the size of applet to 300, 300setSize(200,200);show();}// invoked when applet is startedpublicvoidstart(){}// invoked when applet is closedpublicvoidstop(){}publicvoidpaint(Graphics g){// x coordinates of verticesintx[] = {10,30,40,50,110,140};// y coordinates of verticesinty[] = {140,110,50,40,30,10};// number of verticesintnumberofpoints =6;// set the color of line drawn to blueg.setColor(Color.blue);// draw the polygon using drawPolygon functiong.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 startedpublicvoidinit(){// set the size of applet to 300, 300setSize(200,200);show();}// invoked when applet is startedpublicvoidstart(){}// invoked when applet is closedpublicvoidstop(){}publicvoidpaint(Graphics g){// x coordinates of verticesintx[] = {10,30,40,50,110,140};// y coordinates of verticesinty[] = {140,110,50,40,30,10};// number of verticesintnumberofpoints =6;// create a polygon with given x, y coordinatesPolygon p =newPolygon(x, y, numberofpoints);// set the color of line drawn to blueg.setColor(Color.blue);// draw the polygon using drawPolygon// function using object of polygon classg.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 startedpublicvoidinit(){// set the size of applet to 300, 300setSize(200,200);show();}// invoked when applet is startedpublicvoidstart(){}// invoked when applet is closedpublicvoidstop(){}publicvoidpaint(Graphics g){// x coordinates of verticesintx[] = {10,30,40,50,110,140};// y coordinates of verticesinty[] = {140,110,50,40,30,10};// number of verticesintnumberofpoints =6;// set the color of line drawn to blueg.setColor(Color.blue);// join the adjacent verticesfor(inti =0; i < numberofpoints -1; i++)g.drawLine(x[i], y[i], x[i +1], y[i +1]);// join the first and last vertexg.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




