Example usage for java.awt Shape getPathIterator

List of usage examples for java.awt Shape getPathIterator

Introduction

In this page you can find the example usage for java.awt Shape getPathIterator.

Prototype

public PathIterator getPathIterator(AffineTransform at);

Source Link

Document

Returns an iterator object that iterates along the Shape boundary and provides access to the geometry of the Shape outline.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Shape s = new Rectangle2D.Double(0, 0, 72, 72);

    PathIterator pi = s.getPathIterator(null);

    while (pi.isDone() == false) {
        describeCurrentSegment(pi);/*from w w w.  j a  va  2  s. c  o m*/
        pi.next();
    }

}

From source file:DescribePath.java

public static void describePath(Shape s) {
    PathIterator pi = s.getPathIterator(null);

    while (pi.isDone() == false) {
        describeCurrentSegment(pi);// w w w .  j  a v a2  s. c  o m
        pi.next();
    }
}

From source file:Main.java

/**
 * Converts a {@link Shape} to TDL. /*  w ww .  ja  v  a 2 s .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: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];//  ww w .  jav a 2s  .  com
            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:CustomStrokes.java

public Shape createStrokedShape(Shape shape) {
    GeneralPath newshape = new GeneralPath(); // Start with an empty shape

    // Iterate through the specified shape, perturb its coordinates, and
    // use them to build up the new shape.
    float[] coords = new float[6];
    for (PathIterator i = shape.getPathIterator(null); !i.isDone(); i.next()) {
        int type = i.currentSegment(coords);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            perturb(coords, 2);//  w  ww  .  ja  va  2  s. co m
            newshape.moveTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_LINETO:
            perturb(coords, 2);
            newshape.lineTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_QUADTO:
            perturb(coords, 4);
            newshape.quadTo(coords[0], coords[1], coords[2], coords[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            perturb(coords, 6);
            newshape.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
            break;
        case PathIterator.SEG_CLOSE:
            newshape.closePath();
            break;
        }
    }

    // Finally, stroke the perturbed shape and return the result
    return stroke.createStrokedShape(newshape);
}

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

public Shape createStrokedShape(Shape shape) {
    FontRenderContext frc = new FontRenderContext(null, true, true);
    GlyphVector glyphVector = font.createGlyphVector(frc, text);

    GeneralPath result = new GeneralPath();
    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 next = 0;
    int currentChar = 0;
    int length = glyphVector.getNumGlyphs();

    if (length == 0)
        return result;

    float factor = stretchToFit ? measurePathLength(shape) / (float) glyphVector.getLogicalBounds().getWidth()
            : 1.0f;/*  w  ww. ja v  a2s. c  om*/
    float height = (float) glyphVector.getLogicalBounds().getHeight();
    float nextAdvance = 0;

    while (currentChar < length && !it.isDone()) {
        type = it.currentSegment(points);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            moveX = lastX = points[0];
            moveY = lastY = points[1];
            result.moveTo(moveX, moveY);
            nextAdvance = glyphVector.getGlyphMetrics(currentChar).getAdvance() * 0.5f;
            next = nextAdvance;
            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;
            float distance = (float) FastMath.sqrt(dx * dx + dy * dy);
            if (distance >= next) {
                float r = 1.0f / distance;
                float angle = (float) FastMath.atan2(dy, dx);
                while (currentChar < length && distance >= next) {
                    Shape glyph = glyphVector.getGlyphOutline(currentChar);
                    Point2D p = glyphVector.getGlyphPosition(currentChar);
                    float px = (float) p.getX();
                    float py = (float) p.getY();
                    float x = lastX + next * dx * r;
                    float y = lastY + next * dy * r;
                    float advance = nextAdvance;
                    nextAdvance = currentChar < length - 1
                            ? glyphVector.getGlyphMetrics(currentChar + 1).getAdvance() * 0.5f
                            : 0;
                    t.setToTranslation(x, y);
                    t.rotate(angle);
                    t.translate(-px - advance, -py + height * factor / 2.0f);
                    result.append(t.createTransformedShape(glyph), false);
                    next += (advance + nextAdvance) * factor;
                    currentChar++;
                    if (repeat)
                        currentChar %= length;
                }
            }
            next -= distance;
            lastX = thisX;
            lastY = thisY;
            break;
        }
        it.next();
    }

    return result;
}

From source file:net.sf.maltcms.chromaui.annotations.PeakAnnotationRenderer.java

private void drawEntity(Shape entity, Graphics2D g2, Color fill, Color stroke, ChartPanel chartPanel,
        boolean scale, float alpha) {
    if (entity != null) {
        //System.out.println("Drawing entity with bbox: "+entity.getBounds2D());
        Shape savedClip = g2.getClip();
        Rectangle2D dataArea = chartPanel.getScreenDataArea();
        Color c = g2.getColor();/*  w  w  w .j  a v a  2s . c  o  m*/
        Composite comp = g2.getComposite();
        g2.clip(dataArea);
        g2.setColor(fill);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        AffineTransform originalTransform = g2.getTransform();
        Shape transformed = entity;
        FlatteningPathIterator iter = new FlatteningPathIterator(
                transformed.getPathIterator(new AffineTransform()), 1);
        Path2D.Float path = new Path2D.Float();
        path.append(iter, false);
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        g2.fill(path);
        if (stroke != null) {
            g2.setColor(stroke);
            g2.draw(path);
        }
        g2.setComposite(comp);
        g2.setColor(c);
        g2.setClip(savedClip);
    } else {
        Logger.getLogger(getClass().getName()).info("Entity is null!");
    }
}

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;
        }/* w  w w . ja v  a2 s.  com*/
    }

    return strokedShape;
}

From source file:ExtendedGeneralPath.java

/**
 * Delegates to the enclosed <code>GeneralPath</code>.
 *//*  w  ww.  ja  v a  2 s  . com*/
public void append(Shape s, boolean connect) {
    append(s.getPathIterator(new AffineTransform()), connect);
}

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.
 *///  www  .  j  a va  2 s.  c  om
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);
    }
}