Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Draw Polygon in Graphics 
 

A polygon is a closed path or circuit which is made by joining line segments. In polygon, each line segment intersects exactly two others line segments.

 

Draw Polygon in Graphics

                         

In this section, you will learn how to draw a polygon in Graphics.

A polygon is a closed path or circuit which is made by joining line segments. In polygon, each line segment intersects exactly two others line segments. To draw a polygon, we have used the Polygon class.

 

 

 

 

Following code draws the polygon of three sides:

for (int i = 0; i < 3; i++){
polygon1.addPoint((int) (40 + 50 * Math.cos(i * 2 * Math.PI / 3)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 3)));}
g.drawPolygon(polygon1);

Following code draws the polygon of six sides:

for (int i = 0; i < 6; i++){
polygon2.addPoint((int) (160 + 50 * Math.cos(i * 2 * Math.PI / 6)),
(int) (150 + 50 * Math.sin(i * 2 * Math.PI / 6)));}
g.drawPolygon(polygon2);

Following code draws the spiral polygon:

for (int i = 0; i < 360; i++) {
double value = i / 360.0;
polygon3.addPoint((int) (290 + 50 * value * Math.cos(8 * value * Math.PI)),
(int) (150 + 50 * value * Math.sin(8 * value * Math.PI)));}
g.drawPolygon(polygon3);}

The method addPoint() adds the coordinates to the polygon. The method drawPolygon() draws the polygon. The method setColor() sets the color to paint the polygon.

Here is the code of DrawPolygon.java

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

public class DrawPolygon extends JPanel {
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
  g.setColor(Color.red);
     Polygon polygon1= new Polygon();
     for (int i = 0; i < 3; i++){
      polygon1.addPoint((int) (40 50 * Math.cos(i * * Math.PI / 3)),
          (int) (150 50 * Math.sin(i * * Math.PI / 3)));
   }
   g.drawPolygon(polygon1);

     Polygon polygon2= new Polygon();
     for (int i = 0; i < 6; i++){
      polygon2.addPoint((int) (160 50 * Math.cos(i * * Math.PI / 6)),
          (int) (150 50 * Math.sin(i * * Math.PI / 6)));
   }
   g.drawPolygon(polygon2);

    Polygon polygon3 = new Polygon();
   
     for (int i = 0; i < 360; i++) {
      double value = i / 360.0;
      polygon3.addPoint((int) (290 50 * value * Math.cos(* value * Math.PI)),
          (int) (150 50 * value * Math.sin(* value * Math.PI)));
   }
      g.drawPolygon(polygon3);
   }
   
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("Show Different Polygons");
    frame.setSize(350250);
    Container contentPane = frame.getContentPane();
    contentPane.add(new DrawPolygon());
    frame.show();
  }
}

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: c class orm form io method sed format 2d get ip translation return instance transform show for transformation to draw

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.