Web | This Site

Java Basics Examples
Java Tutorial
Java Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# Java Examples - display polygon using GUI.

## Problem Description:

How to draw a polygon using GUI?

## Solution:

Following example demonstrates how to draw a polygon by creating Polygon() object. addPoint() & drawPolygon() method is used to draw the Polygon.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Main extends JPanel {
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Polygon p = new Polygon();
          for (int i = 0; i < 5; i++)
          p.addPoint((int)
          (100 + 50 * Math.cos(i * 2 * Math.PI / 5)),
          (int) (100 + 50 * Math.sin(i * 2 * Math.PI / 5)));
          g.drawPolygon(p);
       }
       public static void main(String[] args) {
          JFrame frame = new JFrame();
          frame.setTitle("Polygon");
          frame.setSize(350, 250);
          frame.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {
                System.exit(0);
             }
          });
          Container contentPane = frame.getContentPane();
          contentPane.add(new Main());
          frame.setVisible(true);
       }
    }

## Result:

The above code sample will produce the following result.

    Polygon is displayed in a frame.

Advertisement

Online Image Processing

Indian Baby Names