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.

Example:

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){
  g.setColor(Color.green);
  int[] xpoint=new int[]{10,100,150,300};
  int[] ypoint=new int[]{10,100,120,100};
  g.drawPolygon(xpoint,ypoint,4);
  g.fillPolygon(xpoint,ypoint,4);

   }
}

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());
  frame.setVisible(true);
}

public void actionPerformed(ActionEvent e){
}

static class Listener extends WindowAdapter{


public void windowClosing(WindowEvent event){

  System.exit(0);

        }

   }

}




Comments

monish poande comment

 monish poande

we want gui base application not only java base

03-25-2014
Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
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.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like