Java tutorial-Drawing polygon

Drawing polygon

You can draw a polygon shape on the window by using the drawPolygon(array_xpoint, array_ypoints, num_points)method of the Graphics class. To fill the oval with color you need to use the fillPolygon(array_xpoints, array_ypoints, num_points) method. The first argument is the array of x values(x-coordinate); the second argument is the array of y values(y-coordinate) and the last one is the number of points of the polygon shape.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Polygon extends Canvas{
public Polygon(){
setSize(500,400);//set the window size

public void paint(Graphics g){
  int[] xpoint=new int[]{10,100,150,300};
  int[] ypoint=new int[]{10,100,120,100};


public class DrawPolygon implements ActionListener{
public static void main(String args[]){
  Polygon p=new Polygon();
  JFrame frame=new JFrame("Polygons");
  frame.add(p); //show the drawing
  frame.pack();//make sure the window opening as expected
  frame.addWindowListener(new Listener());

public void actionPerformed(ActionEvent e){

static class Listener extends WindowAdapter{

public void windowClosing(WindowEvent event){






monish poande comment

 monish poande

we want gui base application not only java base


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.