Student Of Fortune

Draw Polygon in Java

Share on :
Example The following program will present how to draw a polygon (polygon) in Java. To make it we use the classes and methods drawPolygon Polygon ().

Here is the view the program:

menggambar-poligon-java

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

public class DrawPolygons extends JFrame {

    public DrawPolygons() {
        super ("Menggambar Polygon"); // you can change
Polygon Drawing
        setSize (400,300);
        show();
    }

    public void paint(Graphics g) {
        super.paint (g);

        int xValues[] = {20,40,50,30,20,15};
        int yValues[] = {50,50,60,80,80,60};
        Polygon poly1 = new Polygon (xValues, yValues, 6); //(arrX, arrY, jumTitik)

        g.drawPolygon (poly1);
        int xValues2[] = {70,90,100,80,70,65,60};
        int yValues2[] = {100,100,110,110,130,110,90};
        g.drawPolyline (xValues2, yValues2, 7);       

        int xValues3[] = {120,140,150,190};
        int yValues3[] = {40,70,80,60};
        g.fillPolygon (xValues3, yValues3, 4);

        Polygon poly2 = new Polygon();
        poly2.addPoint (220,100);
        poly2.addPoint (175,150);
        poly2.addPoint (270,150);
        g.fillPolygon (poly2);
    }

    public static void main (String args[]) {

        DrawPolygons test = new DrawPolygons();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

1 comments:

Unknown said... November 16, 2013 at 2:41 AM

can you explain the above in detail..?

Post a Comment and Don't Spam!

Dont Spam please

 
Recommended Post Slide Out For Blogger

Recent Comments

My Rank