Example usage for java.awt.geom GeneralPath closePath

List of usage examples for java.awt.geom GeneralPath closePath

Introduction

In this page you can find the example usage for java.awt.geom GeneralPath closePath.

Prototype

public final synchronized void closePath() 

Source Link

Document

Closes the current subpath by drawing a straight line back to the coordinates of the last moveTo .

Usage

From source file:userinterface.graph.PrismErrorRenderer.java

/**
 * Draws the visual representation for one data item.
 *
 * @param g2  the graphics output target.
 * @param state  the renderer state.//from w  ww .j a  v  a 2 s  .co m
 * @param dataArea  the data area.
 * @param info  the plot rendering info.
 * @param plot  the plot.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param dataset  the dataset.
 * @param series  the series index.
 * @param item  the item index.
 * @param crosshairState  the crosshair state.
 * @param pass  the pass index
 * @author Muhammad Omer Saeed.
 */

@Override
public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairState, int pass) {

    if (!drawError) {
        super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item,
                crosshairState, pass);
        return;
    }

    switch (currentMethod) {

    case PrismErrorRenderer.ERRORBARS:

        if (pass == 0 && dataset instanceof XYSeriesCollection && getItemVisible(series, item)) {

            synchronized (dataset) {

                XYSeriesCollection collection = (XYSeriesCollection) dataset;

                PlotOrientation orientation = plot.getOrientation();
                // draw the error bar for the y-interval
                XYSeries s = collection.getSeries(series);
                PrismXYDataItem it = (PrismXYDataItem) s.getDataItem(item);

                double y0 = it.getYValue() + it.getError();
                double y1 = it.getYValue() - it.getError();
                double x = collection.getXValue(series, item);

                RectangleEdge edge = plot.getRangeAxisEdge();
                double yy0 = rangeAxis.valueToJava2D(y0, dataArea, edge);
                double yy1 = rangeAxis.valueToJava2D(y1, dataArea, edge);
                double xx = domainAxis.valueToJava2D(x, dataArea, plot.getDomainAxisEdge());
                Line2D line;
                Line2D cap1;
                Line2D cap2;
                double adj = this.capLength / 2.0;
                if (orientation == PlotOrientation.VERTICAL) {
                    line = new Line2D.Double(xx, yy0, xx, yy1);
                    cap1 = new Line2D.Double(xx - adj, yy0, xx + adj, yy0);
                    cap2 = new Line2D.Double(xx - adj, yy1, xx + adj, yy1);
                } else { // PlotOrientation.HORIZONTAL
                    line = new Line2D.Double(yy0, xx, yy1, xx);
                    cap1 = new Line2D.Double(yy0, xx - adj, yy0, xx + adj);
                    cap2 = new Line2D.Double(yy1, xx - adj, yy1, xx + adj);
                }

                g2.setPaint(getItemPaint(series, item));

                if (this.errorStroke != null) {
                    g2.setStroke(this.errorStroke);
                } else {
                    g2.setStroke(getItemStroke(series, item));
                }
                g2.draw(line);
                g2.draw(cap1);
                g2.draw(cap2);
            }

        }

        super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item,
                crosshairState, pass);

        break;

    case PrismErrorRenderer.ERRORDEVIATION:

        synchronized (dataset) {
            // do nothing if item is not visible
            if (!getItemVisible(series, item)) {
                return;
            }

            // first pass draws the shading
            if (pass == 0) {

                XYSeriesCollection collection = (XYSeriesCollection) dataset;
                XYSeries s = collection.getSeries(series);
                PrismXYDataItem it = (PrismXYDataItem) s.getDataItem(item);

                State drState = (State) state;

                double x = collection.getXValue(series, item);
                double yLow = it.getYValue() - it.getError();
                double yHigh = it.getYValue() + it.getError();

                RectangleEdge xAxisLocation = plot.getDomainAxisEdge();
                RectangleEdge yAxisLocation = plot.getRangeAxisEdge();

                double xx = domainAxis.valueToJava2D(x, dataArea, xAxisLocation);
                double yyLow = rangeAxis.valueToJava2D(yLow, dataArea, yAxisLocation);
                double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea, yAxisLocation);

                PlotOrientation orientation = plot.getOrientation();
                if (orientation == PlotOrientation.HORIZONTAL) {
                    drState.lowerCoordinates.add(new double[] { yyLow, xx });
                    drState.upperCoordinates.add(new double[] { yyHigh, xx });
                } else if (orientation == PlotOrientation.VERTICAL) {
                    drState.lowerCoordinates.add(new double[] { xx, yyLow });
                    drState.upperCoordinates.add(new double[] { xx, yyHigh });
                }

                if (item == (dataset.getItemCount(series) - 1)) {
                    // last item in series, draw the lot...
                    // set up the alpha-transparency...
                    Composite originalComposite = g2.getComposite();
                    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) this.alpha));
                    g2.setPaint(getItemPaint(series, item));
                    GeneralPath area = new GeneralPath();
                    double[] coords = (double[]) drState.lowerCoordinates.get(0);
                    area.moveTo((float) coords[0], (float) coords[1]);
                    for (int i = 1; i < drState.lowerCoordinates.size(); i++) {
                        coords = (double[]) drState.lowerCoordinates.get(i);
                        area.lineTo((float) coords[0], (float) coords[1]);
                    }
                    int count = drState.upperCoordinates.size();
                    coords = (double[]) drState.upperCoordinates.get(count - 1);
                    area.lineTo((float) coords[0], (float) coords[1]);
                    for (int i = count - 2; i >= 0; i--) {
                        coords = (double[]) drState.upperCoordinates.get(i);
                        area.lineTo((float) coords[0], (float) coords[1]);
                    }
                    area.closePath();
                    g2.fill(area);
                    g2.setComposite(originalComposite);

                    drState.lowerCoordinates.clear();
                    drState.upperCoordinates.clear();
                }
            }
            if (isLinePass(pass)) {

                // the following code handles the line for the y-values...it's
                // all done by code in the super class
                if (item == 0) {
                    State s = (State) state;
                    s.seriesPath.reset();
                    s.setLastPointGood(false);
                }

                if (getItemLineVisible(series, item)) {
                    drawPrimaryLineAsPath(state, g2, plot, dataset, pass, series, item, domainAxis, rangeAxis,
                            dataArea);
                }
            }

            // second pass adds shapes where the items are ..
            else if (isItemPass(pass)) {

                // setup for collecting optional entity info...
                EntityCollection entities = null;
                if (info != null) {
                    entities = info.getOwner().getEntityCollection();
                }

                drawSecondaryPass(g2, plot, dataset, pass, series, item, domainAxis, dataArea, rangeAxis,
                        crosshairState, entities);
            }

        }

        break;
    default:
        return;
    }
}

From source file:net.sourceforge.processdash.ui.web.reports.RadarPlot.java

protected void drawRadar(Graphics2D g2, Rectangle2D plotArea, PlotRenderingInfo info, int pieIndex,
        PieDataset data) {//from ww w  .  j  a  va2s. c o  m

    // adjust the plot area by the interior spacing value
    double gapHorizontal = plotArea.getWidth() * this.interiorGap;
    double gapVertical = plotArea.getHeight() * this.interiorGap;
    double radarX = plotArea.getX() + gapHorizontal / 2;
    double radarY = plotArea.getY() + gapVertical / 2;
    double radarW = plotArea.getWidth() - gapHorizontal;
    double radarH = plotArea.getHeight() - gapVertical;

    // make the radar area a square if the radar chart is to be circular...
    // NOTE that non-circular radar charts are not currently supported.
    if (true) { //circular) {
        double min = Math.min(radarW, radarH) / 2;
        radarX = (radarX + radarX + radarW) / 2 - min;
        radarY = (radarY + radarY + radarH) / 2 - min;
        radarW = 2 * min;
        radarH = 2 * min;
    }

    double radius = radarW / 2;
    double centerX = radarX + radarW / 2;
    double centerY = radarY + radarH / 2;

    Rectangle2D radarArea = new Rectangle2D.Double(radarX, radarY, radarW, radarH);

    // plot the data (unless the dataset is null)...
    if ((data != null) && (data.getKeys().size() > 0)) {

        // get a list of categories...
        List keys = data.getKeys();
        int numAxes = keys.size();

        // draw each of the axes on the radar chart, and register
        // the shape of the radar line.

        double multiplier = 1.0;
        GeneralPath lineShape = new GeneralPath(GeneralPath.WIND_NON_ZERO, numAxes + 1);
        GeneralPath gridShape = new GeneralPath(GeneralPath.WIND_NON_ZERO, numAxes + 1);

        int axisNumber = -1;
        Iterator iterator = keys.iterator();
        while (iterator.hasNext()) {
            Comparable currentKey = (Comparable) iterator.next();
            axisNumber++;
            Number dataValue = data.getValue(currentKey);

            double value = (dataValue != null ? dataValue.doubleValue() : 0);
            if (value > 1 || Double.isNaN(value) || Double.isInfinite(value))
                value = 1.0;
            if (value < 0)
                value = 0.0;
            multiplier *= value;

            double angle = 2 * Math.PI * axisNumber / numAxes;
            double deltaX = Math.sin(angle) * radius;
            double deltaY = -Math.cos(angle) * radius;

            // draw the spoke
            g2.setPaint(axisPaint);
            g2.setStroke(axisStroke);
            Line2D line = new Line2D.Double(centerX, centerY, centerX + deltaX, centerY + deltaY);
            g2.draw(line);

            // register the grid line and the shape line
            if (axisNumber == 0) {
                gridShape.moveTo((float) deltaX, (float) deltaY);
                lineShape.moveTo((float) (deltaX * value), (float) (deltaY * value));
            } else {
                gridShape.lineTo((float) deltaX, (float) deltaY);
                lineShape.lineTo((float) (deltaX * value), (float) (deltaY * value));
            }

            if (showAxisLabels) {
                // draw the label
                double labelX = centerX + deltaX * (1 + axisLabelGap);
                double labelY = centerY + deltaY * (1 + axisLabelGap);
                String label = currentKey.toString();
                drawLabel(g2, radarArea, label, axisNumber, labelX, labelY);
            }

        }
        gridShape.closePath();
        lineShape.closePath();

        // draw five gray concentric gridlines
        g2.translate(centerX, centerY);
        g2.setPaint(gridLinePaint);
        g2.setStroke(gridLineStroke);
        for (int i = 5; i > 0; i--) {
            Shape scaledGrid = gridShape
                    .createTransformedShape(AffineTransform.getScaleInstance(i / 5.0, i / 5.0));
            g2.draw(scaledGrid);
        }

        // get the color for the plot shape.
        Paint dataPaint = plotLinePaint;
        if (dataPaint == ADAPTIVE_COLORING) {
            //multiplier = Math.exp(Math.log(multiplier) * 2 / numAxes);
            dataPaint = getMultiplierColor((float) multiplier);
        }

        // compute a slightly transparent version of the plot color for
        // the fill.
        Paint dataFill = null;
        if (dataPaint instanceof Color && getForegroundAlpha() != 1.0)
            dataFill = new Color(((Color) dataPaint).getRed() / 255f, ((Color) dataPaint).getGreen() / 255f,
                    ((Color) dataPaint).getBlue() / 255f, getForegroundAlpha());
        else
            dataFill = dataPaint;

        // draw the plot shape.  First fill with a parially
        // transparent color, then stroke with the opaque color.
        g2.setPaint(dataFill);
        g2.fill(lineShape);
        g2.setPaint(dataPaint);
        g2.setStroke(plotLineStroke);
        g2.draw(lineShape);

        // cleanup the graphics context.
        g2.translate(-centerX, -centerY);
    }
}

From source file:nl.b3p.kaartenbalie.core.server.b3pLayering.ConfigLayer.java

protected void drawTitledMessageBox(Graphics2D g2d, String title, String message, int x, int y, int w, int h) {
    /* Do some calculations and init variables. */
    g2d.setFont(KBConfiguration.OHD_messageBoxFont);
    FontMetrics fm = g2d.getFontMetrics();
    int labelHeight = KBConfiguration.OHD_messageBoxFont.getSize() + (KBConfiguration.OHD_padding * 2);
    int angling = labelHeight;
    Rectangle2D testRectangle = fm.getStringBounds(title, g2d);
    int labelWidth = (int) testRectangle.getWidth();

    if (w < labelWidth + (2 * angling)) {
        w = labelWidth + (2 * angling);/*  w  w  w  . j a v  a2s . c  o  m*/
    }
    y += labelHeight;
    /* Now draw the box...    */
    drawMessageBox(g2d, message, x, y, w, h);

    /* Draw the label background */
    g2d.setColor(KBConfiguration.OHD_labelBoxColor);
    GeneralPath label = new GeneralPath();
    label.moveTo(x, y);
    label.lineTo(x + angling, y - labelHeight);
    label.lineTo(x + angling + labelWidth, y - labelHeight);
    label.lineTo(x + (angling * 2) + labelWidth, y);
    label.closePath();
    g2d.fill(label);

    /* Draw the label Lines..  */
    g2d.setColor(KBConfiguration.OHD_borderBoxTopLeft);
    g2d.drawLine(x, y, x + angling, y - labelHeight);
    g2d.drawLine(x + angling, y - labelHeight, x + angling + labelWidth, y - labelHeight);
    g2d.setColor(KBConfiguration.OHD_borderBoxBottomRight);
    g2d.drawLine(x + angling + labelWidth, y - labelHeight, x + (angling * 2) + labelWidth, y);
    g2d.setColor(KBConfiguration.OHD_borderBoxBackground);
    g2d.drawLine(x + (angling * 2) + labelWidth, y, x, y);
    /*Then add the title... */
    g2d.setColor(KBConfiguration.OHD_labelFontBoxColor);
    g2d.drawString(title, x + angling, y - KBConfiguration.OHD_padding);

}

From source file:org.amanzi.awe.render.network.NetworkRenderer.java

/**
 * render sector element on map//  w  w  w.j  a va 2 s . c o  m
 * 
 * @param destination
 * @param point
 * @param element
 */
private void renderSector(final Graphics2D destination, final Point point, final double azimuth,
        final double beamwidth, final IDataElement sector) {
    int size = getSize();
    int x = getSectorXCoordinate(point, size);
    int y = getSectorYCoordinate(point, size);
    destination.setColor(networkRendererStyle.changeColor(getColor(sector), networkRendererStyle.getAlpha()));
    GeneralPath path = new GeneralPath();
    path.moveTo(x, y);
    Arc2D a = createSector(point, networkRendererStyle.getLargeElementSize(), getAngle(azimuth, beamwidth),
            beamwidth);
    path.append(a.getPathIterator(null), true);
    path.closePath();
    destination.draw(path);
    destination.fill(path);
    // create border
    destination.setColor(networkRendererStyle.getBorderColor());
    destination.draw(path);
}

From source file:org.amanzi.awe.render.network.NetworkRenderer.java

/**
 * Draw black border around selected sector
 * //from   w w w.  j a v  a2  s  .c o  m
 * @param destination
 * @param point
 * @param model
 * @param sector
 */
private void renderSelectionBorder(Graphics2D destination, Point point, ISectorElement sector, int index,
        int count) {
    Pair<Double, Double> sectorParameters = getSectorParameters(sector, index, count);
    int size = getSize();
    double azimuth = sectorParameters.getLeft();
    double beamwidth = sectorParameters.getRight();

    GeneralPath path = new GeneralPath();
    path.moveTo(getSectorXCoordinate(point, size), getSectorYCoordinate(point, size));
    Arc2D a = createSector(point, networkRendererStyle.getLargeElementSize(), getAngle(azimuth, beamwidth),
            beamwidth);
    path.append(a.getPathIterator(null), true);
    path.closePath();
    destination
            .setColor(networkRendererStyle.changeColor(SELECTED_SECTOR_COLOR, networkRendererStyle.getAlpha()));
    destination.draw(path);
    destination.drawString(sector.getName(), (int) a.getEndPoint().getX() + 10, (int) a.getEndPoint().getY());
}

From source file:org.apache.fontbox.ttf.GlyphRenderer.java

/**
 * Use the given points to calculate a GeneralPath.
 *
 * @param points the points to be used to generate the GeneralPath
 *
 * @return the calculated GeneralPath/*from w  w w. ja  v a 2 s. c o  m*/
 */
private GeneralPath calculatePath(Point[] points) {
    GeneralPath path = new GeneralPath();
    int start = 0;
    for (int p = 0, len = points.length; p < len; ++p) {
        if (points[p].endOfContour) {
            Point firstPoint = points[start];
            Point lastPoint = points[p];
            List<Point> contour = new ArrayList<Point>();
            for (int q = start; q <= p; ++q) {
                contour.add(points[q]);
            }
            if (points[start].onCurve) {
                // using start point at the contour end
                contour.add(firstPoint);
            } else if (points[p].onCurve) {
                // first is off-curve point, trying to use one from the end
                contour.add(0, lastPoint);
            } else {
                // start and end are off-curve points, creating implicit one
                Point pmid = midValue(firstPoint, lastPoint);
                contour.add(0, pmid);
                contour.add(pmid);
            }
            moveTo(path, contour.get(0));
            for (int j = 1, clen = contour.size(); j < clen; j++) {
                Point pnow = contour.get(j);
                if (pnow.onCurve) {
                    lineTo(path, pnow);
                } else if (contour.get(j + 1).onCurve) {
                    quadTo(path, pnow, contour.get(j + 1));
                    ++j;
                } else {
                    quadTo(path, pnow, midValue(pnow, contour.get(j + 1)));
                }
            }
            path.closePath();
            start = p + 1;
        }
    }
    return path;
}

From source file:org.apache.pdfbox.pdfviewer.font.TTFGlyph2D.java

private void closePath(GeneralPath path) {
    path.closePath();
    LOG.debug("closePath");
}

From source file:org.esa.snap.graphbuilder.gpf.ui.worldmap.NestWorldMapPane.java

public static ArrayList<GeneralPath> assemblePathList(GeoPos[] geoPoints) {
    final GeneralPath path = new GeneralPath(GeneralPath.WIND_NON_ZERO, geoPoints.length + 8);
    final ArrayList<GeneralPath> pathList = new ArrayList<>(16);

    if (geoPoints.length > 1) {
        double lon, lat;
        double minLon = 0, maxLon = 0;

        boolean first = true;
        for (GeoPos gp : geoPoints) {
            lon = gp.getLon();//from ww  w . j a v a  2  s .c om
            lat = gp.getLat();
            if (first) {
                minLon = lon;
                maxLon = lon;
                path.moveTo(lon, lat);
                first = false;
            }
            if (lon < minLon) {
                minLon = lon;
            }
            if (lon > maxLon) {
                maxLon = lon;
            }
            path.lineTo(lon, lat);
        }
        path.closePath();

        int runIndexMin = (int) Math.floor((minLon + 180) / 360);
        int runIndexMax = (int) Math.floor((maxLon + 180) / 360);

        if (runIndexMin == 0 && runIndexMax == 0) {
            // the path is completely within [-180, 180] longitude
            pathList.add(path);
            return pathList;
        }

        final Area pathArea = new Area(path);
        final GeneralPath pixelPath = new GeneralPath(GeneralPath.WIND_NON_ZERO);
        for (int k = runIndexMin; k <= runIndexMax; k++) {
            final Area currentArea = new Area(
                    new Rectangle2D.Float(k * 360.0f - 180.0f, -90.0f, 360.0f, 180.0f));
            currentArea.intersect(pathArea);
            if (!currentArea.isEmpty()) {
                pathList.add(areaToPath(currentArea, -k * 360.0, pixelPath));
            }
        }
    }
    return pathList;
}

From source file:org.esa.snap.graphbuilder.gpf.ui.worldmap.NestWorldMapPane.java

public static GeneralPath areaToPath(final Area negativeArea, final double deltaX,
        final GeneralPath pixelPath) {

    final float[] floats = new float[6];
    // move to correct rectangle
    final AffineTransform transform = AffineTransform.getTranslateInstance(deltaX, 0.0);
    final PathIterator iterator = negativeArea.getPathIterator(transform);

    while (!iterator.isDone()) {
        final int segmentType = iterator.currentSegment(floats);
        if (segmentType == PathIterator.SEG_LINETO) {
            pixelPath.lineTo(floats[0], floats[1]);
        } else if (segmentType == PathIterator.SEG_MOVETO) {
            pixelPath.moveTo(floats[0], floats[1]);
        } else if (segmentType == PathIterator.SEG_CLOSE) {
            pixelPath.closePath();
        }//from w w  w. jav  a 2  s  . c o m
        iterator.next();
    }
    return pixelPath;
}

From source file:org.squidy.designer.util.ImageUtils.java

public static Shape getShapeOfImage(BufferedImage image) {
    // Get the data
    Raster data = image.getData();
    ///*from  w w w  .jav a2  s.  c o  m*/
    //      System.out.println("num of bands = " + data.getNumBands());
    // The colour of the pixel looking at
    // Shoulld have length of 4 (RGBA)
    int[] lookAt = null;
    // The map of all the points
    Point2D[][] pointMap = new Point2D[data.getWidth()][data.getHeight()];
    // The from point
    Point2D from = null;
    // The general path
    GeneralPath path = new GeneralPath();

    // Go round height
    for (int y = 0; y < data.getHeight(); y++) {
        // Go round width
        for (int x = 0; x < data.getWidth(); x++) {
            // Get the colour
            lookAt = data.getPixel(x, y, lookAt);
            // The alpha
            int a = lookAt[3];
            // If > then 0
            if (a > 0) {
                // Output 1
                //System.out.print(1);
                // Save point
                pointMap[x][y] = new Point2D.Double(x, y);

                if (from == null) {
                    from = pointMap[x][y];
                }
            } // 0
            else {
                // Output 0
                //System.out.print(0);
                // Nothing her
                pointMap[x][y] = null;
            }
        }
        // New line
        //System.out.println();
    }

    // Move it to the from
    if (from != null) {
        path.moveTo(from.getX(), from.getY());
        /*
         * Make the shape
         */
        // Go round height
        for (int y = 0; y < data.getHeight(); y++) {
            // Go round width
            for (int x = 0; x < data.getWidth(); x++) {
                // If the point is not null
                if (pointMap[x][y] != null) {
                    // Draw a line to
                    path.append(new Rectangle2D.Double(pointMap[x][y].getX(), pointMap[x][y].getY(), 1, 1),
                            true);
                    //               path.lineTo(pointMap[x][y].getX(), pointMap[x][y].getY());
                }
            }

        }
        path.closePath();
        // TODO: Put in the middle
        return path;
    }
    return null;
}