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);}}chevron_rightfilter_noneOutput :

- 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);}}chevron_rightfilter_noneOutput :
- 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]);}}chevron_rightfilter_noneOutput :
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
Recommended Posts:
- Draw a Chessboard in Java Applet
- Draw a Smiley in Java Applet
- Draw an Olympic Symbol in Java Applet
- Draw a ellipse and a rectangle in Java Applet
- Java Applet | Draw a line using drawLine() method
- Java Applet Basics
- Java Applet | Digital Stopwatch
- Java Applet | How to display an Analog Clock
- Java Applet | How to display a Digital Clock
- Java Applet | Implementing Flood Fill algorithm
- JavaFX | Polygon with examples
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.function.DoublePredicate interface in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java.util.concurrent.Phaser class in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



