Example usage for java.awt Graphics2D fillPolygon

List of usage examples for java.awt Graphics2D fillPolygon

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:Main.java

static void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
    // x1 and y1 are coordinates of circle or rectangle
    // x2 and y2 are coordinates of circle or rectangle, to this point is directed the arrow
    Graphics2D g = (Graphics2D) g1.create();
    double dx = x2 - x1;
    double dy = y2 - y1;
    double angle = Math.atan2(dy, dx);
    int len = (int) Math.sqrt(dx * dx + dy * dy);
    AffineTransform t = AffineTransform.getTranslateInstance(x1, y1);
    t.concatenate(AffineTransform.getRotateInstance(angle));
    g.transform(t);/*from  w  w  w  . j  av  a  2 s .c o m*/
    g.drawLine(0, 0, len, 0);
    int basePosition = len - ARROW_HEAD_SIZE.width;
    int height = ARROW_HEAD_SIZE.height;
    g.fillPolygon(new int[] { len, basePosition, basePosition, len }, new int[] { 0, -height, height, 0 }, 4);
}

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;
    }//from  w  ww  .j  a  v a2  s  .co 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./*w ww.j av  a2 s  .c o  m*/
 *
 * @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:ucar.unidata.idv.control.chart.RangeFilter.java

/**
 * Draws the annotation.//from w ww  .j  a v  a 2  s.com
 *
 * @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());
    boolean selected = getSelected();
    if (attached != null) {
        selected |= attached.getSelected();
    }

    if (selected) {
        g2.setColor(COLOR_SELECTED);
    } else {
        g2.setColor(getColor());
    }
    y = (int) rangeAxis.valueToJava2D(rangeValue, dataArea, RectangleEdge.LEFT);

    int width = (int) ANNOTATION_WIDTH;
    int width2 = (int) (ANNOTATION_WIDTH / 2);
    x = (int) dataArea.getX();
    //        System.err.println("x/y:" + x +"/" +y);

    int[] xs;
    int[] ys;
    if (type == TYPE_LESSTHAN) {
        xs = new int[] { x, x + width, x + width2, x };
        ys = new int[] { y, y, y + width, y };
    } else {
        xs = new int[] { x, x + width, x + width2, x };
        ys = new int[] { y, y, y - width, y };

    }
    g2.fillPolygon(xs, ys, xs.length);

    g2.setColor(Color.gray);
    g2.drawLine(x + width, y, (int) (dataArea.getX() + dataArea.getWidth()), y);

    if ((attached != null) && (type == TYPE_LESSTHAN)) {
        int otherY = (int) rangeAxis.valueToJava2D(attached.rangeValue, dataArea, RectangleEdge.LEFT);

        g2.drawLine(x + width2, y + width, x + width2, otherY - width);
    }
}

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

/**
 * draw the time line/* w w  w  . j  ava2  s  .  c  om*/
 *
 * @param g2 param
 * @param plot param
 * @param dataArea param
 * @param domainAxis param
 * @param rangeAxis param
 * @param rendererIndex param
 * @param info param
 */
private void drawTime(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis,
        ValueAxis rangeAxis, int rendererIndex, PlotRenderingInfo info) {
    try {
        Animation animation = control.getSomeAnimation();
        if (animation == null) {
            return;
        }
        Real dttm = animation.getAniValue();
        if (dttm == null) {
            return;
        }
        g2.setStroke(new BasicStroke());
        g2.setColor(Color.black);
        double timeValue = dttm.getValue(CommonUnit.secondsSinceTheEpoch);
        int x = (int) domainAxis.valueToJava2D(timeValue * 1000, dataArea, RectangleEdge.BOTTOM);
        if ((x < dataArea.getX()) || (x > dataArea.getX() + dataArea.getWidth())) {
            return;
        }

        int bottom = (int) (dataArea.getY() + dataArea.getHeight());
        int top = (int) (dataArea.getY());
        int offset = 0;
        if (false && (clockImage == null)) {
            clockImage = GuiUtils.getImage("/auxdata/ui/icons/clock.gif");
            clockImage.getHeight(this);
            offset = clockImage.getHeight(null);
        }

        //            g2.drawLine(x, (int) dataArea.getY(), x, bottom - offset);
        int w = 8;
        int w2 = w / 2;
        int[] xs = { x - w2, x, x + w2, x };
        int[] ys = { top, top + w, top, top };
        //            g2.drawLine(x, top, x, top+10);
        g2.fillPolygon(xs, ys, xs.length);
        if (clockImage != null) {
            g2.drawImage(clockImage, x - clockImage.getWidth(null) / 2, bottom - clockImage.getHeight(null),
                    null);
        }
    } catch (VisADException exc) {
    } catch (RemoteException exc) {
    }
}

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

public static void fillTriangle(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.fillPolygon(xa, ya, 3);
}