Example usage for java.awt.geom PathIterator currentSegment

List of usage examples for java.awt.geom PathIterator currentSegment

Introduction

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

Prototype

public int currentSegment(double[] coords);

Source Link

Document

Returns the coordinates and type of the current path segment in the iteration.

Usage

From source file:DescribePath.java

public static void describeCurrentSegment(PathIterator pi) {
    double[] coordinates = new double[6];
    int type = pi.currentSegment(coordinates);
    switch (type) {
    case PathIterator.SEG_MOVETO:
        System.out.println("move to " + coordinates[0] + ", " + coordinates[1]);
        break;/*from  w  w w. j  a v a 2 s  .c o m*/
    case PathIterator.SEG_LINETO:
        System.out.println("line to " + coordinates[0] + ", " + coordinates[1]);
        break;
    case PathIterator.SEG_QUADTO:
        System.out.println("quadratic to " + coordinates[0] + ", " + coordinates[1] + ", " + coordinates[2]
                + ", " + coordinates[3]);
        break;
    case PathIterator.SEG_CUBICTO:
        System.out.println("cubic to " + coordinates[0] + ", " + coordinates[1] + ", " + coordinates[2] + ", "
                + coordinates[3] + ", " + coordinates[4] + ", " + coordinates[5]);
        break;
    case PathIterator.SEG_CLOSE:
        System.out.println("close");
        break;
    default:
        break;
    }
}

From source file:Main.java

/**
 * Converts a {@link Shape} to TDL. /* w w w  .j a  va  2s .c o m*/
 * @param s
 * @return a string containing a space separated list of the points
 * specifiying the shape
 */
public static String shapeToXML(Shape s) {
    StringBuilder sb = new StringBuilder();

    PathIterator pi = s.getPathIterator(new AffineTransform());

    float[] coords = new float[6];

    while (!pi.isDone()) {
        pi.currentSegment(coords);

        sb.append(String.valueOf(coords[0]) + "," + String.valueOf(coords[1]));

        pi.next();

        if (!pi.isDone()) {
            sb.append(" ");
        }
    }

    return sb.toString();
}

From source file:Main.java

public static void writePath(GeneralPath path, ObjectOutputStream out) throws IOException {
    PathIterator i = path.getPathIterator(null);
    float[] data = new float[6];

    while (!i.isDone()) {
        switch (i.currentSegment(data)) {
        case PathIterator.SEG_MOVETO:
            out.writeInt(PathIterator.SEG_MOVETO);
            out.writeFloat(data[0]);//from  w ww  .  j  a  v  a2s .  com
            out.writeFloat(data[1]);
            break;

        case PathIterator.SEG_LINETO:
            out.writeInt(PathIterator.SEG_LINETO);
            out.writeFloat(data[0]);
            out.writeFloat(data[1]);
            break;

        case PathIterator.SEG_QUADTO:
            out.writeInt(PathIterator.SEG_QUADTO);
            out.writeFloat(data[0]);
            out.writeFloat(data[1]);
            out.writeFloat(data[2]);
            out.writeFloat(data[3]);
            break;

        case PathIterator.SEG_CUBICTO:
            out.writeInt(PathIterator.SEG_CUBICTO);
            out.writeFloat(data[0]);
            out.writeFloat(data[1]);
            out.writeFloat(data[2]);
            out.writeFloat(data[3]);
            out.writeFloat(data[4]);
            out.writeFloat(data[5]);
            break;

        case PathIterator.SEG_CLOSE:
            out.writeInt(PathIterator.SEG_CLOSE);
            break;

        default:
            throw new IOException();
        }

        i.next();
    }

    out.writeInt(PATH_IS_DONE);
}

From source file:Main.java

/**
 * Serialises a <code>Shape</code> object.
 *
 * @param shape  the shape object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *//*from w  w w.  j a  v a2 s  .  co m*/
public static void writeShape(final Shape shape, final ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (shape != null) {
        stream.writeBoolean(false);
        if (shape instanceof Line2D) {
            final Line2D line = (Line2D) shape;
            stream.writeObject(Line2D.class);
            stream.writeDouble(line.getX1());
            stream.writeDouble(line.getY1());
            stream.writeDouble(line.getX2());
            stream.writeDouble(line.getY2());
        } else if (shape instanceof Rectangle2D) {
            final Rectangle2D rectangle = (Rectangle2D) shape;
            stream.writeObject(Rectangle2D.class);
            stream.writeDouble(rectangle.getX());
            stream.writeDouble(rectangle.getY());
            stream.writeDouble(rectangle.getWidth());
            stream.writeDouble(rectangle.getHeight());
        } else if (shape instanceof Ellipse2D) {
            final Ellipse2D ellipse = (Ellipse2D) shape;
            stream.writeObject(Ellipse2D.class);
            stream.writeDouble(ellipse.getX());
            stream.writeDouble(ellipse.getY());
            stream.writeDouble(ellipse.getWidth());
            stream.writeDouble(ellipse.getHeight());
        } else if (shape instanceof Arc2D) {
            final Arc2D arc = (Arc2D) shape;
            stream.writeObject(Arc2D.class);
            stream.writeDouble(arc.getX());
            stream.writeDouble(arc.getY());
            stream.writeDouble(arc.getWidth());
            stream.writeDouble(arc.getHeight());
            stream.writeDouble(arc.getAngleStart());
            stream.writeDouble(arc.getAngleExtent());
            stream.writeInt(arc.getArcType());
        } else if (shape instanceof GeneralPath) {
            stream.writeObject(GeneralPath.class);
            final PathIterator pi = shape.getPathIterator(null);
            final float[] args = new float[6];
            stream.writeBoolean(pi.isDone());
            while (!pi.isDone()) {
                final int type = pi.currentSegment(args);
                stream.writeInt(type);
                // TODO: could write this to only stream the values
                // required for the segment type
                for (int i = 0; i < 6; i++) {
                    stream.writeFloat(args[i]);
                }
                stream.writeInt(pi.getWindingRule());
                pi.next();
                stream.writeBoolean(pi.isDone());
            }
        } else {
            stream.writeObject(shape.getClass());
            stream.writeObject(shape);
        }
    } else {
        stream.writeBoolean(true);
    }
}

From source file:Main.java

/**
 * Tests two polygons for equality.  If both are <code>null</code> this
 * method returns <code>true</code>.
 *
 * @param p1  path 1 (<code>null</code> permitted).
 * @param p2  path 2 (<code>null</code> permitted).
 *
 * @return A boolean./*w  ww .ja v a 2 s  .c o m*/
 */
public static boolean equal(final GeneralPath p1, final GeneralPath p2) {
    if (p1 == null) {
        return (p2 == null);
    }
    if (p2 == null) {
        return false;
    }
    if (p1.getWindingRule() != p2.getWindingRule()) {
        return false;
    }
    PathIterator iterator1 = p1.getPathIterator(null);
    PathIterator iterator2 = p2.getPathIterator(null);
    double[] d1 = new double[6];
    double[] d2 = new double[6];
    boolean done = iterator1.isDone() && iterator2.isDone();
    while (!done) {
        if (iterator1.isDone() != iterator2.isDone()) {
            return false;
        }
        int seg1 = iterator1.currentSegment(d1);
        int seg2 = iterator2.currentSegment(d2);
        if (seg1 != seg2) {
            return false;
        }
        if (!Arrays.equals(d1, d2)) {
            return false;
        }
        iterator1.next();
        iterator2.next();
        done = iterator1.isDone() && iterator2.isDone();
    }
    return true;
}

From source file:com.t_oster.visicut.misc.Helper.java

public static Rectangle2D smallestBoundingBox(Shape s, AffineTransform t) {
    double minX = 0;
    double maxX = 0;
    double minY = 0;
    double maxY = 0;
    PathIterator pi = s.getPathIterator(t, 1);
    double[] last = null;
    boolean first = true;
    while (!pi.isDone()) {
        double[] d = new double[8];
        switch (pi.currentSegment(d)) {
        case PathIterator.SEG_LINETO: {
            if (last != null) {
                if (first) {
                    minX = last[0];//from w ww  .ja v  a  2 s. com
                    maxX = last[0];
                    minY = last[1];
                    maxY = last[1];
                    first = false;
                } else {
                    if (last[0] < minX) {
                        minX = last[0];
                    }
                    if (last[0] > maxX) {
                        maxX = last[0];
                    }
                    if (last[1] < minY) {
                        minY = last[1];
                    }
                    if (last[1] > maxY) {
                        maxY = last[1];
                    }
                }
            }
            if (first) {
                minX = d[0];
                maxX = d[0];
                minY = d[1];
                maxY = d[1];
                first = false;
            } else {
                if (d[0] < minX) {
                    minX = d[0];
                }
                if (d[0] > maxX) {
                    maxX = d[0];
                }
                if (d[1] < minY) {
                    minY = d[1];
                }
                if (d[1] > maxY) {
                    maxY = d[1];
                }
            }
            break;
        }
        case PathIterator.SEG_MOVETO: {
            last = d;
            break;
        }
        }
        pi.next();
    }
    return new Rectangle.Double(minX, minY, maxX - minX, maxY - minY);
}

From source file:it.unibo.alchemist.model.implementations.linkingrules.ConnectionBeam.java

private boolean projectedBeamOvercomesObstacle(final Position pos1, final Position pos2) {
    final double p1x = pos1.getCoordinate(0);
    final double p1y = pos1.getCoordinate(1);
    final double p2x = pos2.getCoordinate(0);
    final double p2y = pos2.getCoordinate(1);
    final double x = p2x - p1x;
    final double y = p2y - p1y;
    /*// w w  w .  ja  v  a2  s .  c  o m
     * Compute the angle
     */
    final double angle = atan2(y, x);
    /*
     * Deduce surrounding beam vertices
     */
    final double dx = range * cos(PI / 2 + angle);
    final double dy = range * sin(PI / 2 + angle);
    /*
     * Enlarge the beam
     */
    final double cx = range * cos(angle);
    final double cy = range * sin(angle);
    /*
     * Create the beam
     */
    final Path2D.Double beamShape = new Path2D.Double();
    beamShape.moveTo(p1x + dx - cx, p1y + dy - cy);
    beamShape.lineTo(p1x - dx - cx, p1y - dy - cy);
    beamShape.lineTo(p2x - dx + cx, p2y - dy + cy);
    beamShape.lineTo(p2x + dx + cx, p2y + dy + cy);
    beamShape.closePath();
    final Area beam = new Area(beamShape);
    /*
     * Perform subtraction
     */
    beam.subtract(obstacles);
    /*
     * Rebuild single areas
     */
    final List<Path2D.Double> subareas = new ArrayList<>();
    Path2D.Double curpath = new Path2D.Double();
    final PathIterator pi = beam.getPathIterator(null);
    final double[] coords = new double[COORDS];
    while (!pi.isDone()) {
        switch (pi.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            curpath = new Path2D.Double();
            curpath.moveTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_LINETO:
            curpath.lineTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_CLOSE:
            curpath.closePath();
            subareas.add(curpath);
            break;
        default:
            throw new IllegalArgumentException();
        }
        pi.next();
    }
    /*
     * At least one area must contain both points
     */
    for (final Path2D.Double p : subareas) {
        if (p.contains(p1x, p1y) && p.contains(p2x, p2y)) {
            return true;
        }
    }
    return false;
}

From source file:org.pentaho.plugin.jfreereport.reportcharts.JFreeChartReportDrawable.java

private AbstractImageMapEntry createMapEntry(final Shape area, final Rectangle2D dataArea) {
    if (buggyDrawArea) {
        if (area instanceof Ellipse2D) {
            final Ellipse2D ellipse2D = (Ellipse2D) area;
            if (ellipse2D.getWidth() == ellipse2D.getHeight()) {
                return new CircleImageMapEntry((float) (ellipse2D.getCenterX() + dataArea.getX()),
                        (float) (ellipse2D.getCenterY() + dataArea.getY()), (float) (ellipse2D.getWidth() / 2));
            }/*from   www . j a v  a 2s .  c o  m*/
        } else if (area instanceof Rectangle2D) {
            final Rectangle2D rect = (Rectangle2D) area;
            return (new RectangleImageMapEntry((float) (rect.getX() + dataArea.getX()),
                    (float) (rect.getY() + dataArea.getY()), (float) (rect.getX() + rect.getWidth()),
                    (float) (rect.getY() + rect.getHeight())));
        }
    } else {
        if (area instanceof Ellipse2D) {
            final Ellipse2D ellipse2D = (Ellipse2D) area;
            if (ellipse2D.getWidth() == ellipse2D.getHeight()) {
                return new CircleImageMapEntry((float) (ellipse2D.getCenterX()),
                        (float) (ellipse2D.getCenterY()), (float) (ellipse2D.getWidth() / 2));
            }
        } else if (area instanceof Rectangle2D) {
            final Rectangle2D rect = (Rectangle2D) area;
            return (new RectangleImageMapEntry((float) (rect.getX()), (float) (rect.getY()),
                    (float) (rect.getX() + rect.getWidth()), (float) (rect.getY() + rect.getHeight())));
        }
    }

    final Area a = new Area(area);
    if (buggyDrawArea) {
        a.transform(AffineTransform.getTranslateInstance(dataArea.getX(), dataArea.getY()));
    }
    if (dataArea.isEmpty() == false) {
        a.intersect(new Area(dataArea));
    }
    final PathIterator pathIterator = a.getPathIterator(null, 2);
    final FloatList floats = new FloatList(100);
    final float[] coords = new float[6];
    while (pathIterator.isDone() == false) {
        final int retval = pathIterator.currentSegment(coords);
        if (retval == PathIterator.SEG_MOVETO || retval == PathIterator.SEG_LINETO) {
            floats.add(coords[0]);
            floats.add(coords[1]);
        }
        pathIterator.next();
    }

    if (floats.size() == 0) {
        return null;
    }
    return (new PolygonImageMapEntry(floats.toArray()));
}

From source file:com.jhlabs.awt.TextStroke.java

public float measurePathLength(Shape shape) {
    PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS);
    float points[] = new float[6];
    float moveX = 0, moveY = 0;
    float lastX = 0, lastY = 0;
    float thisX = 0, thisY = 0;
    int type = 0;
    float total = 0;

    while (!it.isDone()) {
        type = it.currentSegment(points);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            moveX = lastX = points[0];//from   w w  w .  java2s .  c o m
            moveY = lastY = points[1];
            break;

        case PathIterator.SEG_CLOSE:
            points[0] = moveX;
            points[1] = moveY;
            // Fall into....

        case PathIterator.SEG_LINETO:
            thisX = points[0];
            thisY = points[1];
            float dx = thisX - lastX;
            float dy = thisY - lastY;
            total += (float) Math.sqrt(dx * dx + dy * dy);
            lastX = thisX;
            lastY = thisY;
            break;
        }
        it.next();
    }

    return total;
}

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  w  w w.j a  v a2 s .c  o m
 */
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;
}