Example usage for java.awt Graphics2D drawPolyline

List of usage examples for java.awt Graphics2D drawPolyline

Introduction

In this page you can find the example usage for java.awt Graphics2D drawPolyline.

Prototype

public abstract void drawPolyline(int[] xPoints, int[] yPoints, int nPoints);

Source Link

Document

Draws a sequence of connected lines defined by arrays of x and y coordinates.

Usage

From source file:Main.java

public static BufferedImage drawtrajectory(BufferedImage canvas, int parabola[][], Color bgColour) {
    Graphics2D g2d = canvas.createGraphics();
    g2d.setColor(bgColour);/*from   w  ww.  ja v  a2 s .c  om*/
    g2d.drawPolyline(parabola[0], parabola[1], parabola[0].length);
    return canvas;
}

From source file:com.ricemap.spateDB.operations.Plot.java

/**
 * Plots a Geometry from the library JTS into the given image.
 * @param graphics//  www . j  a  v  a  2s . com
 * @param geom
 * @param fileMbr
 * @param imageWidth
 * @param imageHeight
 * @param scale
 * @param shape_color
 */
private static void drawJTSShape(Graphics2D graphics, Geometry geom, Prism fileMbr, int imageWidth,
        int imageHeight, double scale, Color shape_color) {
    if (geom instanceof GeometryCollection) {
        GeometryCollection geom_coll = (GeometryCollection) geom;
        for (int i = 0; i < geom_coll.getNumGeometries(); i++) {
            Geometry sub_geom = geom_coll.getGeometryN(i);
            // Recursive call to draw each geometry
            drawJTSShape(graphics, sub_geom, fileMbr, imageWidth, imageHeight, scale, shape_color);
        }
    } else if (geom instanceof com.vividsolutions.jts.geom.Polygon) {
        com.vividsolutions.jts.geom.Polygon poly = (com.vividsolutions.jts.geom.Polygon) geom;

        for (int i = 0; i < poly.getNumInteriorRing(); i++) {
            LineString ring = poly.getInteriorRingN(i);
            drawJTSShape(graphics, ring, fileMbr, imageWidth, imageHeight, scale, shape_color);
        }

        drawJTSShape(graphics, poly.getExteriorRing(), fileMbr, imageWidth, imageHeight, scale, shape_color);
    } else if (geom instanceof LineString) {
        LineString line = (LineString) geom;
        double geom_alpha = line.getLength() * scale;
        int color_alpha = geom_alpha > 1.0 ? 255 : (int) Math.round(geom_alpha * 255);
        if (color_alpha == 0)
            return;

        int[] xpoints = new int[line.getNumPoints()];
        int[] ypoints = new int[line.getNumPoints()];

        for (int i = 0; i < xpoints.length; i++) {
            double px = line.getPointN(i).getX();
            double py = line.getPointN(i).getY();

            // Transform a point in the polygon to image coordinates
            xpoints[i] = (int) Math.round((px - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1));
            ypoints[i] = (int) Math.round((py - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1));
        }

        // Draw the polygon
        graphics.setColor(new Color((shape_color.getRGB() & 0x00FFFFFF) | (color_alpha << 24), true));
        graphics.drawPolyline(xpoints, ypoints, xpoints.length);
    }
}

From source file:ucar.unidata.idv.control.chart.MyTimeSeriesPlot.java

/**
 * draw//  w  w  w .  ja v  a  2 s  .c o  m
 *
 * @param g2 the graphics
 * @param dataArea where the data area is
 * @param index which data set
 * @param info info
 * @param crosshairState crosshairState
 *
 * @return any drawn
 */
public boolean render(Graphics2D g2, Rectangle2D dataArea, int index, PlotRenderingInfo info,
        CrosshairState crosshairState) {

    XYDataset dataset = getDataset(index);
    if (DatasetUtilities.isEmptyOrNull(dataset)) {
        return false;
    }

    ValueAxis rangeAxis = getRangeAxisForDataset(index);
    ValueAxis domainAxis = getDomainAxisForDataset(index);
    AxisLocation rangeAxisLocation = getRangeAxisLocation(index);
    AxisLocation domainAxisLocation = getDomainAxisLocation(index);
    RectangleEdge rangeEdge = getRangeAxisEdge();
    RectangleEdge domainEdge = getDomainAxisEdge();
    int seriesCount = dataset.getSeriesCount();
    //        System.out.println ("********************************");
    for (int series = seriesCount - 1; series >= 0; series--) {
        int itemCount = dataset.getItemCount(series);
        int[] xs = new int[itemCount];
        int[] ys = new int[itemCount];
        g2.setStroke(getRendererForDataset(dataset).getSeriesStroke(series));
        g2.setPaint(getRendererForDataset(dataset).getSeriesPaint(series));
        int pointCnt = 0;
        for (int item = 0; item < itemCount; item++) {
            double x1 = dataset.getXValue(series, item);
            double y1 = dataset.getYValue(series, item);
            if (!timeseries.valuesOk(index, x1, y1)) {
                if (pointCnt > 0) {
                    g2.drawPolyline(xs, ys, pointCnt);
                    pointCnt = 0;
                }
                continue;
            }
            double transX = domainAxis.valueToJava2D(x1, dataArea, domainEdge);
            double transY = rangeAxis.valueToJava2D(y1, dataArea, rangeEdge);
            if (!dataArea.contains(transX, transY)) {
                continue;
            }

            xs[pointCnt] = (int) (transX + 0.5);
            ys[pointCnt] = (int) (transY + 0.5);
            pointCnt++;
            if (pointCnt > 10) {
                g2.drawPolyline(xs, ys, pointCnt);
                xs[0] = xs[pointCnt - 1];
                ys[0] = ys[pointCnt - 1];
                pointCnt = 1;
            }

        }
        if (pointCnt > 1) {
            g2.drawPolyline(xs, ys, pointCnt);
        }
        long t2 = System.currentTimeMillis();
        //            System.err.println("time:" + (t2 - t1) + "ms #" + itemCount);
    }
    return true;
}

From source file:edu.umn.cs.spatialHadoop.operations.Plot.java

/**
 * Plots a Geometry from the library JTS into the given image.
 * @param graphics//from  w w w .  j  a v a 2  s  . com
 * @param geom
 * @param fileMbr
 * @param imageWidth
 * @param imageHeight
 * @param scale
 * @param shape_color
 */
private static void drawJTSShape(Graphics2D graphics, Geometry geom, Rectangle fileMbr, int imageWidth,
        int imageHeight, double scale, Color shape_color) {
    if (geom instanceof GeometryCollection) {
        GeometryCollection geom_coll = (GeometryCollection) geom;
        for (int i = 0; i < geom_coll.getNumGeometries(); i++) {
            Geometry sub_geom = geom_coll.getGeometryN(i);
            // Recursive call to draw each geometry
            drawJTSShape(graphics, sub_geom, fileMbr, imageWidth, imageHeight, scale, shape_color);
        }
    } else if (geom instanceof com.vividsolutions.jts.geom.Polygon) {
        com.vividsolutions.jts.geom.Polygon poly = (com.vividsolutions.jts.geom.Polygon) geom;

        for (int i = 0; i < poly.getNumInteriorRing(); i++) {
            LineString ring = poly.getInteriorRingN(i);
            drawJTSShape(graphics, ring, fileMbr, imageWidth, imageHeight, scale, shape_color);
        }

        drawJTSShape(graphics, poly.getExteriorRing(), fileMbr, imageWidth, imageHeight, scale, shape_color);
    } else if (geom instanceof LineString) {
        LineString line = (LineString) geom;
        double geom_alpha = line.getLength() * scale;
        int color_alpha = geom_alpha > 1.0 ? 255 : (int) Math.round(geom_alpha * 255);
        if (color_alpha == 0)
            return;

        int[] xpoints = new int[line.getNumPoints()];
        int[] ypoints = new int[line.getNumPoints()];

        for (int i = 0; i < xpoints.length; i++) {
            double px = line.getPointN(i).getX();
            double py = line.getPointN(i).getY();

            // Transform a point in the polygon to image coordinates
            xpoints[i] = (int) Math.round((px - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1));
            ypoints[i] = (int) Math.round((py - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1));
        }

        // Draw the polygon
        graphics.setColor(new Color((shape_color.getRGB() & 0x00FFFFFF) | (color_alpha << 24), true));
        graphics.drawPolyline(xpoints, ypoints, xpoints.length);
    }
}

From source file:edu.umn.cs.spatialHadoop.operations.Plot.java

public static void drawShape(Graphics2D graphics, Shape s, Rectangle fileMbr, int imageWidth, int imageHeight,
        double scale) {
    if (s instanceof NASAPoint) {
        final int MinValue = 7500;
        final int MaxValue = 16000;
        NASAPoint pt = (NASAPoint) s;/*from   w  ww  .j a v  a  2s . co  m*/
        int x = (int) ((pt.x - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1));
        int y = (int) ((pt.y - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1));
        int value = pt.value;

        if (value < min_value && value > 1000)
            min_value = value;
        if (value > max_value)
            max_value = value;

        if (value > 0 && x >= 0 && x < imageWidth && y >= 0 && y < imageHeight) {
            Color color;
            if (value < MinValue) {
                color = Color.BLACK;
            } else if (value < MaxValue) {
                float ratio = 0.78f - 0.78f * (value - MinValue) / (MaxValue - MinValue);
                color = Color.getHSBColor(ratio, 0.5f, 1.0f);
            } else {
                color = Color.WHITE;
            }
            graphics.setColor(color);
            graphics.fillRect(x, y, 1, 1);
        }
    } else if (s instanceof Point) {
        Point pt = (Point) s;
        int x = (int) ((pt.x - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1));
        int y = (int) ((pt.y - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1));

        if (x >= 0 && x < imageWidth && y >= 0 && y < imageHeight)
            graphics.fillRect(x, y, 1, 1);
    } else if (s instanceof Rectangle) {
        Rectangle r = (Rectangle) s;
        int s_x1 = (int) ((r.x1 - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1));
        int s_y1 = (int) ((r.y1 - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1));
        int s_x2 = (int) (((r.x2) - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1));
        int s_y2 = (int) (((r.y2) - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1));
        graphics.drawRect(s_x1, s_y1, s_x2 - s_x1 + 1, s_y2 - s_y1 + 1);
    } else if (s instanceof OGCShape) {
        OGCShape ogc_shape = (OGCShape) s;
        OGCGeometry geom = ogc_shape.geom;
        Color shape_color = graphics.getColor();
        if (geom instanceof OGCGeometryCollection) {
            OGCGeometryCollection geom_coll = (OGCGeometryCollection) geom;
            for (int i = 0; i < geom_coll.numGeometries(); i++) {
                OGCGeometry sub_geom = geom_coll.geometryN(i);
                // Recursive call to draw each geometry
                drawShape(graphics, new OGCShape(sub_geom), fileMbr, imageWidth, imageHeight, scale);
            }
        } else if (geom.getEsriGeometry() instanceof MultiPath) {
            MultiPath path = (MultiPath) geom.getEsriGeometry();
            double sub_geom_alpha = path.calculateLength2D() * scale;
            int color_alpha = sub_geom_alpha > 1.0 ? 255 : (int) Math.round(sub_geom_alpha * 255);

            if (color_alpha == 0)
                return;

            int[] xpoints = new int[path.getPointCount()];
            int[] ypoints = new int[path.getPointCount()];

            for (int i = 0; i < path.getPointCount(); i++) {
                double px = path.getPoint(i).getX();
                double py = path.getPoint(i).getY();

                // Transform a point in the polygon to image coordinates
                xpoints[i] = (int) Math.round((px - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1));
                ypoints[i] = (int) Math.round((py - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1));
            }

            // Draw the polygon
            graphics.setColor(new Color((shape_color.getRGB() & 0x00FFFFFF) | (color_alpha << 24), true));
            if (path instanceof Polygon)
                graphics.drawPolygon(xpoints, ypoints, path.getPointCount());
            else if (path instanceof Polyline)
                graphics.drawPolyline(xpoints, ypoints, path.getPointCount());
        }
    } else if (s instanceof JTSShape) {
        JTSShape jts_shape = (JTSShape) s;
        Geometry geom = jts_shape.geom;
        Color shape_color = graphics.getColor();

        drawJTSShape(graphics, geom, fileMbr, imageWidth, imageHeight, scale, shape_color);
    } else {
        LOG.warn("Cannot draw a shape of type: " + s.getClass());
        Rectangle r = s.getMBR();
        int s_x1 = (int) ((r.x1 - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1));
        int s_y1 = (int) ((r.y1 - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1));
        int s_x2 = (int) (((r.x2) - fileMbr.x1) * imageWidth / (fileMbr.x2 - fileMbr.x1));
        int s_y2 = (int) (((r.y2) - fileMbr.y1) * imageHeight / (fileMbr.y2 - fileMbr.y1));
        if (s_x1 >= 0 && s_x1 < imageWidth && s_y1 >= 0 && s_y1 < imageHeight)
            graphics.drawRect(s_x1, s_y1, s_x2 - s_x1 + 1, s_y2 - s_y1 + 1);
    }
}