Example usage for java.awt.geom GeneralPath GeneralPath

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

Introduction

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

Prototype

public GeneralPath(Shape s) 

Source Link

Document

Constructs a new GeneralPath object from an arbitrary Shape object.

Usage

From source file:MyCanvas.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    Point2D.Float point = new Point2D.Float(100, 100); // store start point
    GeneralPath p = new GeneralPath(GeneralPath.WIND_NON_ZERO);
    p.moveTo(point.x, point.y);//w w  w .j a  v a 2 s .co m
    p.lineTo(point.x + 20.0f, point.y - 5.0f); // Line from start to A
    point = (Point2D.Float) p.getCurrentPoint();
    p.lineTo(point.x + 5.0f, point.y - 20.0f); // Line from A to B
    point = (Point2D.Float) p.getCurrentPoint();
    p.lineTo(point.x + 5.0f, point.y + 20.0f); // Line from B to C
    point = (Point2D.Float) p.getCurrentPoint();
    p.lineTo(point.x + 20.0f, point.y + 5.0f); // Line from C to D
    point = (Point2D.Float) p.getCurrentPoint();
    p.lineTo(point.x - 20.0f, point.y + 5.0f); // Line from D to E
    point = (Point2D.Float) p.getCurrentPoint();
    p.lineTo(point.x - 5.0f, point.y + 20.0f); // Line from E to F
    point = (Point2D.Float) p.getCurrentPoint();
    p.lineTo(point.x - 5.0f, point.y - 20.0f); // Line from F to g
    p.closePath(); // Line from G to start
    g2D.draw(p);
}

From source file:Utils.java

public static Shape generatePolygon(int sides, int outsideRadius, int insideRadius, boolean normalize) {
    Shape shape = generatePolygon(sides, outsideRadius, insideRadius);
    if (normalize) {
        Rectangle2D bounds = shape.getBounds2D();
        GeneralPath path = new GeneralPath(shape);
        shape = path// ww  w. j a  v  a 2  s. c  om
                .createTransformedShape(AffineTransform.getTranslateInstance(-bounds.getX(), -bounds.getY()));
    }
    return shape;
}

From source file:Draw2DObjects.java

public Draw2DObjects() {
    add("Center", new MyCanvas());
    shapes[0] = new Line2D.Double(0.0, 0.0, 100.0, 100.0);
    shapes[1] = new Rectangle2D.Double(10.0, 100.0, 200.0, 200.0);
    shapes[2] = new Ellipse2D.Double(20.0, 200.0, 100.0, 100.0);
    GeneralPath path = new GeneralPath(new Line2D.Double(300.0, 100.0, 400.0, 150.0));
    path.append(new Line2D.Double(25.0, 175.0, 300.0, 100.0), true);
    shapes[3] = path;//www .j a  va2s.com
    shapes[4] = new RoundRectangle2D.Double(350.0, 250, 200.0, 100.0, 50.0, 25.0);
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

From source file:Draw2DCustomStrokePaint.java

public Draw2DCustomStrokePaint() {
    add("Center", new MyCanvas());
    for (int i = 0; i < shapes.length; ++i)
        shapes[i] = null;/*  www.j a  va 2 s.  c  o  m*/
    shapes[0] = new Line2D.Double(0.0, 0.0, 100.0, 100.0);
    shapes[1] = new Rectangle2D.Double(10.0, 100.0, 200.0, 200.0);
    shapes[2] = new Ellipse2D.Double(20.0, 200.0, 100.0, 100.0);
    GeneralPath path = new GeneralPath(new Line2D.Double(300.0, 100.0, 400.0, 150.0));
    path.append(new Line2D.Double(25.0, 175.0, 300.0, 100.0), true);
    shapes[3] = path;
    shapes[4] = new RoundRectangle2D.Double(350.0, 250, 200.0, 100.0, 50.0, 25.0);
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

From source file:de.unibayreuth.bayeos.goat.chart.DefaultItemRenderer.java

/** Creates a new instance of StatusRenderer */
public DefaultItemRenderer() {
    super();//from w  w w  . ja  v  a 2s  . c o m
    float size = (float) (pref.getInt("shapesize", 8) / 2);

    p = new GeneralPath(GeneralPath.WIND_NON_ZERO);
    p.moveTo(-size, size / 4);
    p.lineTo(-size / 4, size / 4);
    p.lineTo(-size / 4, size);
    p.lineTo(+size / 4, size);
    p.lineTo(+size / 4, size / 4);
    p.lineTo(+size, size / 4);
    p.lineTo(+size, -size / 4);
    p.lineTo(+size / 4, -size / 4);
    p.lineTo(+size / 4, -size);
    p.lineTo(-size / 4, -size);
    p.lineTo(-size / 4, -size / 4);
    p.lineTo(-size, -size / 4);
    p.closePath();
    setShape(p);
    int rend = pref.getInt("xyitemrenderer", DefaultXYItemRenderer.LINES);
    if (rend == DefaultXYItemRenderer.LINES) {
        setPlotLines(true);
        setPlotShapes(false);
    } else if (rend == DefaultXYItemRenderer.SHAPES_AND_LINES) {
        setPlotLines(true);
        setPlotShapes(true);
    } else if (rend == DefaultXYItemRenderer.SHAPES) {
        setPlotLines(false);
        setPlotShapes(true);
    }

    if (pref.getBoolean("charttooltips", false)) {
        setToolTipGenerator(new TimeSeriesToolTipGenerator());
    } else {
        setToolTipGenerator(null);

    }

    setGapThreshold(pref.getDouble("chartthreshold", 0.0));

}

From source file:ExtendedGeneralPath.java

/**
 * Constructs a new <code>ExtendedGeneralPath</code> with the
 * specified winding rule to control operations that require the
 * interior of the path to be defined./*from   w ww  .j  av a2  s .co m*/
 */
public ExtendedGeneralPath(int rule) {
    path = new GeneralPath(rule);
}

From source file:CustomStrokes.java

public Shape createStrokedShape(Shape shape) {
    // Start off by stroking the shape with a thin line. Store the
    // resulting shape in a GeneralPath object so we can add to it.
    GeneralPath strokedShape = new GeneralPath(new BasicStroke(1.0f).createStrokedShape(shape));

    // Use a PathIterator object to iterate through each of the line and
    // curve segments of the shape. For each one, mark the endpoint and
    // control points (if any) by adding a rectangle to the GeneralPath
    float[] coords = new float[6];
    for (PathIterator i = shape.getPathIterator(null); !i.isDone(); i.next()) {
        int type = i.currentSegment(coords);
        Shape s = null, s2 = null, s3 = null;
        switch (type) {
        case PathIterator.SEG_CUBICTO:
            markPoint(strokedShape, coords[4], coords[5]); // falls through
        case PathIterator.SEG_QUADTO:
            markPoint(strokedShape, coords[2], coords[3]); // falls through
        case PathIterator.SEG_MOVETO:
        case PathIterator.SEG_LINETO:
            markPoint(strokedShape, coords[0], coords[1]); // falls through
        case PathIterator.SEG_CLOSE:
            break;
        }/*from   www  . j  a  v a  2 s.com*/
    }

    return strokedShape;
}

From source file:de.bund.bfr.jung.JungUtils.java

static Line2D getLineInMiddle(Shape edgeShape) {
    GeneralPath path = new GeneralPath(edgeShape);
    float[] seg = new float[6];
    List<Point2D> points = new ArrayList<>();

    for (PathIterator i = path.getPathIterator(null, 1); !i.isDone(); i.next()) {
        i.currentSegment(seg);//  w  ww . jav a 2 s . co  m
        points.add(new Point2D.Float(seg[0], seg[1]));
    }

    Point2D first = points.get(0);
    Point2D last = points.get(points.size() - 1);

    if (first.equals(last)) {
        Point2D minP = points.stream().min((p1, p2) -> Double.compare(p1.getY(), p2.getY())).get();

        return new Line2D.Float(minP, new Point2D.Float((float) (minP.getX() + 1.0), (float) minP.getY()));
    } else {
        for (int i = 0; i < points.size() - 1; i++) {
            Point2D p1 = points.get(i);
            Point2D p2 = points.get(i + 1);

            if (p2.distance(last) < p2.distance(first)) {
                Line2D ortho = getOrthogonal(new Line2D.Float(first, last));
                Point2D pp1 = getIntersection(new Line2D.Float(p1, p2), ortho);
                Point2D pp2 = new Point2D.Float((float) (pp1.getX() + last.getX() - first.getX()),
                        (float) (pp1.getY() + last.getY() - first.getY()));

                return new Line2D.Float(pp1, pp2);
            }
        }

        return null;
    }
}

From source file:Polygon2D.java

private void updatePath(float x, float y) {
    closedPath = null;//w  w w . ja va  2s  . c  o m
    if (path == null) {
        path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
        path.moveTo(x, y);
        bounds = new Rectangle2D.Float(x, y, 0, 0);
    } else {
        path.lineTo(x, y);
        float _xmax = (float) bounds.getMaxX();
        float _ymax = (float) bounds.getMaxY();
        float _xmin = (float) bounds.getMinX();
        float _ymin = (float) bounds.getMinY();
        if (x < _xmin)
            _xmin = x;
        else if (x > _xmax)
            _xmax = x;
        if (y < _ymin)
            _ymin = y;
        else if (y > _ymax)
            _ymax = y;
        bounds = new Rectangle2D.Float(_xmin, _ymin, _xmax - _xmin, _ymax - _ymin);
    }
}

From source file:edu.uci.ics.jung.visualization.picking.ShapePickSupport.java

/**
 * Returns an edge whose shape intersects the 'pickArea' footprint of the passed
 * x,y, coordinates.//from  ww  w .java  2  s  .c om
 */
public E getEdge(Layout<V, E> layout, double x, double y) {

    Point2D ip = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(Layer.VIEW,
            new Point2D.Double(x, y));
    x = ip.getX();
    y = ip.getY();

    // as a Line has no area, we can't always use edgeshape.contains(point) so we
    // make a small rectangular pickArea around the point and check if the
    // edgeshape.intersects(pickArea)
    Rectangle2D pickArea = new Rectangle2D.Float((float) x - pickSize / 2, (float) y - pickSize / 2, pickSize,
            pickSize);
    E closest = null;
    double minDistance = Double.MAX_VALUE;
    while (true) {
        try {
            for (E e : getFilteredEdges(layout)) {

                Shape edgeShape = getTransformedEdgeShape(layout, e);
                if (edgeShape == null)
                    continue;

                // because of the transform, the edgeShape is now a GeneralPath
                // see if this edge is the closest of any that intersect
                if (edgeShape.intersects(pickArea)) {
                    float cx = 0;
                    float cy = 0;
                    float[] f = new float[6];
                    PathIterator pi = new GeneralPath(edgeShape).getPathIterator(null);
                    if (pi.isDone() == false) {
                        pi.next();
                        pi.currentSegment(f);
                        cx = f[0];
                        cy = f[1];
                        if (pi.isDone() == false) {
                            pi.currentSegment(f);
                            cx = f[0];
                            cy = f[1];
                        }
                    }
                    float dx = (float) (cx - x);
                    float dy = (float) (cy - y);
                    float dist = dx * dx + dy * dy;
                    if (dist < minDistance) {
                        minDistance = dist;
                        closest = e;
                    }
                }
            }
            break;
        } catch (ConcurrentModificationException cme) {
        }
    }
    return closest;
}