Drawing a polygon in Swing

Create a Polygon object that contains all corners of the polygon and use the method drawPolygon to render it.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
   }
 
   public void paint(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
 
      Polygon p = new Polygon();
      p.addPoint(110, 120);
      p.addPoint(120, 110);
      p.addPoint(130, 110);
      p.addPoint(140, 120);
      p.addPoint(140, 130);
      p.addPoint(130, 140);
      p.addPoint(120, 140);
      p.addPoint(110, 130);
      p.addPoint(110, 120);
 
      g2d.drawPolygon(p);
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 300);
      main.setVisible(true);
   }
}