Tuesday, April 17, 2012

Drawing Polygon in Java

Examples The following program will present how to draw a polygon shape (in terms of many) in Java. To make it we use the Polygon class and method drawPolygon ().


Here is the display program:




And this example of the complete program:

import java.awt.*;

import javax.swing.*;

public class DrawPolygons extends JFrame {

    public DrawPolygons() {
        super ("Menggambar Polygon");
        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);
    }

}

Related Posts by Categories

No comments:

Post a Comment