Example usage for java.awt Graphics2D drawPolygon

List of usage examples for java.awt Graphics2D drawPolygon

Introduction

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

Prototype

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

Source Link

Document

Draws a closed polygon defined by arrays of x and y coordinates.

Usage

From source file:edu.oregonstate.eecs.mcplan.domains.racetrack.RacetrackVisualization.java

private void drawJtsPolygon(final Geometry geom, final Graphics2D g, final boolean fill) {
    final Coordinate[] coords = geom.getCoordinates();
    final int[] poly_x = new int[coords.length];
    final int[] poly_y = new int[coords.length];
    for (int j = 0; j < coords.length; ++j) {
        poly_x[j] = (int) coords[j].x;
        poly_y[j] = (int) coords[j].y;
    }//www  .j av  a  2 s.  c  o m
    if (fill) {
        g.fillPolygon(poly_x, poly_y, coords.length);
    } else {
        g.drawPolygon(poly_x, poly_y, coords.length);
    }
}

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

/**
 * Draws the annotation.//from w  w  w. j  a  v  a2s.  c om
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param dataArea  the data area.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param rendererIndex  the renderer index.
 * @param info  an optional info object that will be populated with
 *              entity information.
 */
public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis,
        int rendererIndex, PlotRenderingInfo info) {
    super.setGraphicsState(g2);
    if (!getPlotWrapper().okToDraw(this)) {
        return;
    }

    g2.setStroke(new BasicStroke());
    if (false && getSelected()) {
        g2.setColor(COLOR_SELECTED);
    } else {
        g2.setColor(getColor());
    }
    x = getXFromValue(dataArea, domainAxis);

    int width2 = (int) (ANNOTATION_WIDTH / 2);
    int bottom = (int) (dataArea.getY() + dataArea.getHeight());
    y = bottom;
    int[] xs = { x - width2, x + width2, x, x - width2 };
    int[] ys = { bottom - ANNOTATION_WIDTH, bottom - ANNOTATION_WIDTH, bottom, bottom - ANNOTATION_WIDTH };
    g2.fillPolygon(xs, ys, xs.length);

    if ((getName() != null) && !isForAnimation) {
        FontMetrics fm = g2.getFontMetrics();
        int width = fm.stringWidth(getName());
        int textLeft = x - width / 2;
        g2.drawString(getName(), textLeft, bottom - ANNOTATION_WIDTH - 2);
    }

    if (getSelected()) {
        g2.setColor(COLOR_SELECTED);
        g2.drawPolygon(xs, ys, xs.length);
    }

    if (getPropertyListeners().hasListeners(PROP_WAYPOINTVALUE) || isForAnimation) {
        g2.setColor(Color.gray);
        g2.drawLine(x, y - ANNOTATION_WIDTH, x, (int) dataArea.getY());
    }

    boolean playSound = canPlaySound();

    if (isForAnimation) {
        if (clockImage == null) {
            clockImage = GuiUtils.getImage("/auxdata/ui/icons/clock.gif");
        }
        if (playSound) {
            g2.drawImage(clockImage, x - 8, (int) dataArea.getY() + 1, null);
        } else {
            g2.drawImage(clockImage, x - 8, (int) dataArea.getY() + 1, null);
        }
    }

    if (canPlaySound()) {
        if (noteImage == null) {
            noteImage = GuiUtils.getImage("/auxdata/ui/icons/note.gif");
        }
        if (isForAnimation) {
            g2.drawImage(noteImage, x + 8, (int) dataArea.getY() + 1, null);
        } else {
            g2.drawImage(noteImage, x, (int) dataArea.getY() + 1, null);
        }
    }

    if (minutesSpan > 0.0) {
        int left = (int) domainAxis.valueToJava2D(domainValue - (minutesSpan * 60000) / 2, dataArea,
                RectangleEdge.BOTTOM);
        int right = (int) domainAxis.valueToJava2D(domainValue + (minutesSpan * 60000) / 2, dataArea,
                RectangleEdge.BOTTOM);
        g2.setPaint(Color.black);
        g2.setStroke(new BasicStroke(2.0f));
        g2.drawLine(left, y, right, y);
    }

}

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  va 2 s  .  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);
    }
}

From source file:lu.fisch.unimozer.Diagram.java

private void drawExtends(Graphics2D g, Point pFrom, Point pTo) {
    int ARROW_SIZE = 16;
    double ARROW_ANGLE = Math.PI / 6;

    //g.setColor(Color.BLACK);
    double angle = Math.atan2(-(pFrom.y - pTo.y), pFrom.x - pTo.x);

    Point pArrow = new Point(pTo.x + (int) ((ARROW_SIZE - 2) * Math.cos(angle)),
            pTo.y - (int) ((ARROW_SIZE - 2) * Math.sin(angle)));

    // draw the arrow head
    int[] xPoints = { pTo.x, pTo.x + (int) ((ARROW_SIZE) * Math.cos(angle + ARROW_ANGLE)),
            pTo.x + (int) (ARROW_SIZE * Math.cos(angle - ARROW_ANGLE)) };
    int[] yPoints = { pTo.y, pTo.y - (int) ((ARROW_SIZE) * Math.sin(angle + ARROW_ANGLE)),
            pTo.y - (int) (ARROW_SIZE * Math.sin(angle - ARROW_ANGLE)) };

    g.drawPolygon(xPoints, yPoints, 3);
    g.drawLine(pFrom.x, pFrom.y, pArrow.x, pArrow.y);
}

From source file:com.aerohive.nms.web.config.lbs.services.HmFolderServiceImpl.java

private BufferedImage createFloorImage(HmFolder floor, double scale, int floorWidth, int floorHeight,
        Map<Long, Integer> channelMap, Map<Long, Integer> colorMap, int borderX, int borderY, double gridSize)
        throws Exception {
    BufferedImage image = new BufferedImage(floorWidth + borderX + 1, floorHeight + borderY + 1,
            BufferedImage.TYPE_INT_ARGB);
    if (floor == null) {
        return image;
    }//from w  w  w.j a  va 2s. c  o m
    double metricWidth = 0.0, metricHeight = 0.0, offsetX = 0.0, offsetY = 0.0;
    int imageWidth = 0;
    LengthUnit lengthUnit = LengthUnit.METERS;

    if (null != floor.getMetricWidth()) {
        metricWidth = floor.getMetricWidth().doubleValue();
    }
    if (null != floor.getMetricHeight()) {
        metricHeight = floor.getMetricHeight().doubleValue();
    }
    if (null != floor.getImageWidth()) {
        imageWidth = floor.getImageWidth().intValue();
    }
    if (null != floor.getOffsetX()) {
        offsetX = floor.getOffsetX().doubleValue();
    }
    if (null != floor.getOffsetY()) {
        offsetY = floor.getOffsetY().doubleValue();
    }
    if (null != floor.getLengthUnit()) {
        lengthUnit = floor.getLengthUnit();
    }

    Graphics2D g2 = image.createGraphics();
    g2.setStroke(new BasicStroke(1));
    if (getDistanceMetric(metricWidth, lengthUnit) == 0) {
        g2.setColor(new Color(255, 255, 255));
        g2.fillRect(borderX, 0, floorWidth + 1, borderY);
        g2.fillRect(0, 0, borderX, borderY + floorHeight + 1);
        g2.setColor(new Color(120, 120, 120));
        g2.drawLine(0, borderY, floorWidth + borderX, borderY);
        g2.drawLine(borderX, 0, borderX, floorHeight + borderY);
        g2.setColor(new Color(255, 255, 204));
        g2.fillRect(borderX + 2, borderY + 2, 162, 25);
        g2.setColor(new Color(0, 51, 102));
        g2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
        g2.drawString("Please size this floor plan.", borderX + 8, borderY + 19);
        return image;
    }
    double screenWidth = scale * getDistanceMetric(metricWidth, lengthUnit);
    double imageScale = screenWidth / imageWidth;
    int originX = (int) (getDistanceMetric(offsetX, lengthUnit) * scale);
    int originY = (int) (getDistanceMetric(offsetY, lengthUnit) * scale);
    g2.setColor(new Color(255, 255, 255));
    if (floor.getBackground() != null && floor.getBackground().length() > 0) {
        LinkedMultiValueMap<String, String> metadata = new LinkedMultiValueMap<>();
        String url = getImageBaseUrl(floor.getOwnerId()) + floor.getBackground();
        try {
            BufferedImage map = ImageIO.read(clientFileService.getFile(url, "AFS_TOKEN", metadata));
            AffineTransform transform = new AffineTransform();
            transform.scale(imageScale, imageScale);
            g2.drawImage(map, new AffineTransformOp(transform, null), getFloorX(0, borderX, originX),
                    getFloorY(0, borderY, originY));
        } catch (Exception e) {
            logger.error(String.format("image file not found with url: %s", url));
            double screenHeight = scale * getDistanceMetric(metricHeight, lengthUnit);
            g2.fillRect(getFloorX(0, borderX, originX), getFloorY(0, borderY, originY), (int) screenWidth,
                    (int) screenHeight);
        }
    } else {
        double screenHeight = scale * getDistanceMetric(metricHeight, lengthUnit);
        g2.fillRect(getFloorX(0, borderX, originX), getFloorY(0, borderY, originY), (int) screenWidth,
                (int) screenHeight);
    }
    g2.setColor(new Color(204, 204, 204));
    // Right edge border
    g2.drawLine(borderX + floorWidth, borderY + 1, borderX + floorWidth, borderY + floorHeight);
    // Left edge border (right of tick marks)
    g2.drawLine(borderX + 1, borderY + floorHeight, borderX + floorWidth, borderY + floorHeight);
    g2.setColor(new Color(255, 255, 255));
    g2.fillRect(borderX, 0, floorWidth + 1, borderY);
    g2.fillRect(0, 0, borderX, borderY + floorHeight + 1);
    g2.setColor(new Color(120, 120, 120));
    g2.drawLine(0, borderY, floorWidth + borderX, borderY);
    g2.drawLine(borderX, 0, borderX, floorHeight + borderY);

    Font font = new Font(Font.SANS_SERIF, Font.BOLD, 12);
    double actualWidth = floorWidth / scale;
    double actualHeight = floorHeight / scale;
    String firstLabel;
    double unitScale = scale;
    if (LengthUnit.FEET == lengthUnit) {
        firstLabel = "0 feet";
        actualWidth /= HmFolder.FEET_TO_METERS;
        actualHeight /= HmFolder.FEET_TO_METERS;
        unitScale *= HmFolder.FEET_TO_METERS;
    } else {
        firstLabel = "0 meters";
    }
    g2.drawString(firstLabel, borderX + 4, 12);
    double gridX = gridSize;
    while (gridX < actualWidth) {
        int x = (int) (gridX * unitScale) + borderX;
        g2.drawLine(x, 0, x, borderY);
        boolean label = true;
        if (gridX + gridSize >= actualWidth) {
            // Last mark
            if (x + getNumberPixelWidth(gridX) + 2 > floorWidth) {
                label = false;
            }
        }
        if (label) {
            g2.drawString("" + (int) gridX, x + 4, 12);
        }
        gridX += gridSize;
    }

    double gridY = 0;
    while (gridY < actualHeight) {
        int y = (int) (gridY * unitScale) + borderY;
        g2.drawLine(0, y, borderX, y);
        double lx = gridY;
        int dx = 1;
        for (int bx = borderX; bx >= 16; bx -= 7) {
            if (lx < 10) {
                dx += 7;
            } else {
                lx /= 10;
            }
        }
        boolean label = true;
        if (gridY + gridSize >= actualHeight) {
            // Last mark
            if (y - borderY + 13 > floorHeight) {
                label = false;
            }
        }
        if (label) {
            g2.drawString("" + (int) gridY, dx, y + 13);
        }
        gridY += gridSize;
    }

    double mapToImage = getMapToMetric(metricWidth, imageWidth, lengthUnit) * scale;
    if (floor.getPerimeter().size() > 0) {
        g2.setStroke(new BasicStroke(2));
        g2.setColor(new Color(2, 159, 245));
        int[] xPoints = new int[floor.getPerimeter().size()];
        int[] yPoints = new int[floor.getPerimeter().size()];
        int nPoints = 0;
        int perimId = floor.getPerimeter().get(0).getId();
        for (int i = 0; i < floor.getPerimeter().size(); i++) {
            HmVertex vertex = floor.getPerimeter().get(i);
            if (vertex.getId() != perimId) {
                g2.drawPolygon(xPoints, yPoints, nPoints);
                nPoints = 0;
                perimId = vertex.getId();
            }
            xPoints[nPoints] = getFloorX((int) (vertex.getX() * mapToImage), borderX, originX);
            yPoints[nPoints++] = getFloorY((int) (vertex.getY() * mapToImage), borderY, originY);
        }
        g2.drawPolygon(xPoints, yPoints, nPoints);
    }

    g2.setStroke(new BasicStroke(1));
    g2.setColor(new Color(0, 170, 0));
    g2.setFont(font.deriveFont(Font.BOLD, 11));

    List<HmDeviceLocationEx> devices = deviceLocationExRep.findAllHmDevices(floor.getId());
    if (null != devices && !devices.isEmpty()) {
        for (HmDeviceLocationEx device : devices) {
            double x = device.getX() * mapToImage;
            double y = device.getY() * mapToImage;
            createNodeImage(device.getId(), channelMap, colorMap, getFloorX((int) x, borderX, originX),
                    getFloorY((int) y, borderY, originY), g2);
        }
    } else {
        List<HmDevicePlanningEx> plannedDevices = devicePlanningExRep.findAllPlannedDevices(floor.getId());
        for (HmDevicePlanningEx plannedDevice : plannedDevices) {
            double x = plannedDevice.getX() * mapToImage;
            double y = plannedDevice.getY() * mapToImage;
            createNodeImage(plannedDevice.getId(), channelMap, colorMap, getFloorX((int) x, borderX, originX),
                    getFloorY((int) y, borderY, originY), g2);
        }
    }
    return image;
}

From source file:se.trixon.almond.GraphicsHelper.java

public static void drawTriangle(int x, int y, int size, Graphics2D graphics2D) {
    int xa[] = { x, x + size / 2, x - size / 2 };
    int ya[] = { y - size / 2, y + size / 2, y + size / 2 };
    graphics2D.drawPolygon(xa, ya, 3);
}