Java Draw Polygon drawPolygon(final Point2D[] points, final Graphics2D g2d, final Paint paint, final boolean fill)

Here you can find the source of drawPolygon(final Point2D[] points, final Graphics2D g2d, final Paint paint, final boolean fill)

Description

Draws a polygon on screen using the points provided as vertices.

License

Open Source License

Parameter

Parameter Description
points vertices of the polygon
g2d graphics object to draw on
paint paint to be applied to the polygon
fill true if the polygon should be filled completely, false if only an outline should be drawn

Declaration

public static Polygon drawPolygon(final Point2D[] points, final Graphics2D g2d, final Paint paint,
        final boolean fill) 

Method Source Code

//package com.java2s;
/*//from  www .j a  va2s . c o m
 *  Copyright (C) 2011-2014 Brian Groenke
 *  All rights reserved.
 * 
 *  This file is part of the 2DX Graphics Library.
 *
 *  This Source Code Form is subject to the terms of the
 *  Mozilla Public License, v. 2.0. If a copy of the MPL 
 *  was not distributed with this file, You can obtain one at 
 *  http://mozilla.org/MPL/2.0/.
 */

import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Polygon;
import java.awt.geom.Point2D;

public class Main {
    /**
     * Draws a polygon on screen using the points provided as vertices.
     * 
     * @param points
     *            vertices of the polygon
     * @param g2d
     *            graphics object to draw on
     * @param paint
     *            paint to be applied to the polygon
     * @param fill
     *            true if the polygon should be filled completely, false if only
     *            an outline should be drawn
     * @return
     */
    public static Polygon drawPolygon(final Point2D[] points, final Graphics2D g2d, final Paint paint,
            final boolean fill) {

        Polygon poly = new Polygon();
        for (Point2D p : points) {
            poly.addPoint((int) p.getX(), (int) p.getY());
        }
        g2d.setPaint(paint);
        if (fill) {
            g2d.fillPolygon(poly);
        } else {
            g2d.drawPolygon(poly);
        }
        return poly;
    }
}

Related

  1. drawPolygons(final Graphics g, final Collection v)