Java Code Examples for java.awt.geom.PathIterator#currentSegment()

The following examples show how to use java.awt.geom.PathIterator#currentSegment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LayoutPathImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public Shape mapShape(Shape s) {
    if (LOGMAP) LOG.format("mapshape on path: %s\n", LayoutPathImpl.SegmentPath.this);
    PathIterator pi = s.getPathIterator(null, 1); // cheap way to handle curves.

    if (LOGMAP) LOG.format("start\n");
    init();

    final double[] coords = new double[2];
    while (!pi.isDone()) {
        switch (pi.currentSegment(coords)) {
        case SEG_CLOSE: close(); break;
        case SEG_MOVETO: moveTo(coords[0], coords[1]); break;
        case SEG_LINETO: lineTo(coords[0], coords[1]); break;
        default: break;
        }

        pi.next();
    }
    if (LOGMAP) LOG.format("finish\n\n");

    GeneralPath gp = new GeneralPath();
    for (Segment seg: segments) {
        gp.append(seg.gp, false);
    }
    return gp;
}
 
Example 2
Source File: LayoutPathImpl.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public Shape mapShape(Shape s) {
    if (LOGMAP) LOG.format("mapshape on path: %s\n", LayoutPathImpl.SegmentPath.this);
    PathIterator pi = s.getPathIterator(null, 1); // cheap way to handle curves.

    if (LOGMAP) LOG.format("start\n");
    init();

    final double[] coords = new double[2];
    while (!pi.isDone()) {
        switch (pi.currentSegment(coords)) {
        case SEG_CLOSE: close(); break;
        case SEG_MOVETO: moveTo(coords[0], coords[1]); break;
        case SEG_LINETO: lineTo(coords[0], coords[1]); break;
        default: break;
        }

        pi.next();
    }
    if (LOGMAP) LOG.format("finish\n\n");

    GeneralPath gp = new GeneralPath();
    for (Segment seg: segments) {
        gp.append(seg.gp, false);
    }
    return gp;
}
 
Example 3
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
  Graphics2D g2 = (Graphics2D) g.create();
  g2.translate(x, y);
  g2.setPaint(Color.BLACK);
  g2.draw(shape);
  g2.setPaint(Color.RED);

  PathIterator i = new FlatteningPathIterator(shape.getPathIterator(null), 1d);
  double[] coords = new double[6];
  while (!i.isDone()) {
    i.currentSegment(coords);
    g2.fillRect((int) (coords[0] - .5), (int) (coords[1] - .5), 2, 2);
    i.next();
  }
  g2.dispose();
}
 
Example 4
Source File: Path2DCopyConstructor.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void testIterator(Path2D pathA, Path2D pathB) {
    final PathIterator itA = pathA.getPathIterator(at);
    final PathIterator itB = pathB.getPathIterator(at);

    float[] coordsA = new float[6];
    float[] coordsB = new float[6];

    int n = 0;
    for (; !itA.isDone() && !itB.isDone(); itA.next(), itB.next(), n++) {
        int typeA = itA.currentSegment(coordsA);
        int typeB = itB.currentSegment(coordsB);

        if (typeA != typeB) {
            throw new IllegalStateException("Path-segment[" + n + "] "
                + "type are not equals [" + typeA + "|" + typeB + "] !");
        }
        // Take care of floating-point precision:
        if (!equalsArrayEps(coordsA, coordsB, getLength(typeA))) {
            throw new IllegalStateException("Path-segment[" + n + "] coords"
                + " are not equals [" + Arrays.toString(coordsA) + "|"
                + Arrays.toString(coordsB) + "] !");
        }
    }
    if (!itA.isDone() || !itB.isDone()) {
        throw new IllegalStateException("Paths do not have same lengths !");
    }
    log("testIterator: " + n + " segments.");
}
 
Example 5
Source File: Elixir_001_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * 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.
 */
public static boolean equal(GeneralPath p1, 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 = p1.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;
}
 
Example 6
Source File: Path2DCopyConstructor.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static void testFlattening(Path2D pathA, Path2D pathB) {
    final PathIterator itA = pathA.getPathIterator(at, FLATNESS);
    final PathIterator itB = pathB.getPathIterator(at, FLATNESS);

    float[] coordsA = new float[6];
    float[] coordsB = new float[6];

    int n = 0;
    for (; !itA.isDone() && !itB.isDone(); itA.next(), itB.next(), n++) {
        int typeA = itA.currentSegment(coordsA);
        int typeB = itB.currentSegment(coordsB);

        if (typeA != typeB) {
            throw new IllegalStateException("Path-segment[" + n + "] "
                + "type are not equals [" + typeA + "|" + typeB + "] !");
        }
        // Take care of floating-point precision:
        if (!equalsArrayEps(coordsA, coordsB, getLength(typeA))) {
            throw new IllegalStateException("Path-segment[" + n + "] coords"
                + " are not equals [" + Arrays.toString(coordsA) + "|"
                + Arrays.toString(coordsB) + "] !");
        }
    }
    if (!itA.isDone() || !itB.isDone()) {
        throw new IllegalStateException("Paths do not have same lengths !");
    }
    log("testFlattening: " + n + " segments.");
}
 
Example 7
Source File: WayStroke.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
public Shape createStrokedShape(Shape shape) {
    // We are flattening the path iterator to only get line segments.
    PathIterator path = shape.getPathIterator(null, 1);
    float points[] = new float[6];
    GeneralPath strokepath = new GeneralPath();
    float ix = 0, iy = 0;
    float px = 0, py = 0;

    while (!path.isDone()) {
        int type = path.currentSegment(points);
        switch (type) {
            case PathIterator.SEG_MOVETO:
                ix = px = points[0];
                iy = py = points[1];
                strokepath.moveTo(ix, iy);
                break;
            case PathIterator.SEG_LINETO:
                strokepath.append(createArrow(px, py, points[0], points[1]),
                        false);
                px = points[0];
                py = points[1];
                break;
            case PathIterator.SEG_CLOSE:
                if (px != ix && py != ix)
                    strokepath.append(createArrow(px, py, ix, iy), false);
                break;
            default:
                strokepath.append(createArrow(px, py, points[0], points[1]),
                        false);
                px = points[0];
                py = points[1];
                // never appear.
        }
        path.next();
    }
    return strokepath;
}
 
Example 8
Source File: ViewUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static StringBuilder appendPath(StringBuilder sb, int indent, PathIterator pathIterator) {
    double[] coords = new double[6];
    while (!pathIterator.isDone()) {
        int type = pathIterator.currentSegment(coords);
        String typeStr;
        int endIndex;
        switch (type) {
            case PathIterator.SEG_CLOSE:
                typeStr = "SEG_CLOSE";
                endIndex = 0;
                break;
            case PathIterator.SEG_CUBICTO:
                typeStr = "SEG_CUBICTO";
                endIndex = 6;
                break;
            case PathIterator.SEG_LINETO:
                typeStr = "SEG_LINETO";
                endIndex = 2;
                break;
            case PathIterator.SEG_MOVETO:
                typeStr = "SEG_MOVETO";
                endIndex = 2;
                break;
            case PathIterator.SEG_QUADTO:
                typeStr = "SEG_QUADTO";
                endIndex = 4;
                break;
            default:
                throw new IllegalStateException("Invalid type=" + type);
        }
        ArrayUtilities.appendSpaces(sb, indent);
        sb.append(typeStr).append(": ");
        for (int i = 0; i < endIndex;) {
            sb.append("[").append(coords[i++]).append(",").append(coords[i++]).append("] ");
        }
        sb.append('\n');
        pathIterator.next();
    }
    return sb;
}
 
Example 9
Source File: Path2DCopyConstructor.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static void testFlattening(Path2D pathA, Path2D pathB) {
    final PathIterator itA = pathA.getPathIterator(at, FLATNESS);
    final PathIterator itB = pathB.getPathIterator(at, FLATNESS);

    float[] coordsA = new float[6];
    float[] coordsB = new float[6];

    int n = 0;
    for (; !itA.isDone() && !itB.isDone(); itA.next(), itB.next(), n++) {
        int typeA = itA.currentSegment(coordsA);
        int typeB = itB.currentSegment(coordsB);

        if (typeA != typeB) {
            throw new IllegalStateException("Path-segment[" + n + "] "
                + "type are not equals [" + typeA + "|" + typeB + "] !");
        }
        // Take care of floating-point precision:
        if (!equalsArrayEps(coordsA, coordsB, getLength(typeA))) {
            throw new IllegalStateException("Path-segment[" + n + "] coords"
                + " are not equals [" + Arrays.toString(coordsA) + "|"
                + Arrays.toString(coordsB) + "] !");
        }
    }
    if (!itA.isDone() || !itB.isDone()) {
        throw new IllegalStateException("Paths do not have same lengths !");
    }
    log("testFlattening: " + n + " segments.");
}
 
Example 10
Source File: Path2DCopyConstructor.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void testFlattening(Path2D pathA, Path2D pathB) {
    final PathIterator itA = pathA.getPathIterator(at, FLATNESS);
    final PathIterator itB = pathB.getPathIterator(at, FLATNESS);

    float[] coordsA = new float[6];
    float[] coordsB = new float[6];

    int n = 0;
    for (; !itA.isDone() && !itB.isDone(); itA.next(), itB.next(), n++) {
        int typeA = itA.currentSegment(coordsA);
        int typeB = itB.currentSegment(coordsB);

        if (typeA != typeB) {
            throw new IllegalStateException("Path-segment[" + n + "] "
                + "type are not equals [" + typeA + "|" + typeB + "] !");
        }
        // Take care of floating-point precision:
        if (!equalsArrayEps(coordsA, coordsB, getLength(typeA))) {
            throw new IllegalStateException("Path-segment[" + n + "] coords"
                + " are not equals [" + Arrays.toString(coordsA) + "|"
                + Arrays.toString(coordsB) + "] !");
        }
    }
    if (!itA.isDone() || !itB.isDone()) {
        throw new IllegalStateException("Paths do not have same lengths !");
    }
    log("testFlattening: " + n + " segments.");
}
 
Example 11
Source File: WPathGraphics.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Given a Java2D <code>PathIterator</code> instance,
 * this method translates that into a Window's path
 * in the printer device context.
 */
private void convertToWPath(PathIterator pathIter) {

    float[] segment = new float[6];
    int segmentType;

    WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();

    /* Map the PathIterator's fill rule into the Window's
     * polygon fill rule.
     */
    int polyFillRule;
    if (pathIter.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
        polyFillRule = WPrinterJob.POLYFILL_ALTERNATE;
    } else {
        polyFillRule = WPrinterJob.POLYFILL_WINDING;
    }
    wPrinterJob.setPolyFillMode(polyFillRule);

    wPrinterJob.beginPath();

    while (pathIter.isDone() == false) {
        segmentType = pathIter.currentSegment(segment);

        switch (segmentType) {
         case PathIterator.SEG_MOVETO:
            wPrinterJob.moveTo(segment[0], segment[1]);
            break;

         case PathIterator.SEG_LINETO:
            wPrinterJob.lineTo(segment[0], segment[1]);
            break;

        /* Convert the quad path to a bezier.
         */
         case PathIterator.SEG_QUADTO:
            int lastX = wPrinterJob.getPenX();
            int lastY = wPrinterJob.getPenY();
            float c1x = lastX + (segment[0] - lastX) * 2 / 3;
            float c1y = lastY + (segment[1] - lastY) * 2 / 3;
            float c2x = segment[2] - (segment[2] - segment[0]) * 2/ 3;
            float c2y = segment[3] - (segment[3] - segment[1]) * 2/ 3;
            wPrinterJob.polyBezierTo(c1x, c1y,
                                     c2x, c2y,
                                     segment[2], segment[3]);
            break;

         case PathIterator.SEG_CUBICTO:
            wPrinterJob.polyBezierTo(segment[0], segment[1],
                                     segment[2], segment[3],
                                     segment[4], segment[5]);
            break;

         case PathIterator.SEG_CLOSE:
            wPrinterJob.closeFigure();
            break;
        }


        pathIter.next();
    }

    wPrinterJob.endPath();

}
 
Example 12
Source File: DuctusRenderingEngine.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private void feedConsumer(PathConsumer consumer, PathIterator pi) {
    try {
        consumer.beginPath();
        boolean pathClosed = false;
        float mx = 0.0f;
        float my = 0.0f;
        float point[]  = new float[6];

        while (!pi.isDone()) {
            int type = pi.currentSegment(point);
            if (pathClosed == true) {
                pathClosed = false;
                if (type != PathIterator.SEG_MOVETO) {
                    // Force current point back to last moveto point
                    consumer.beginSubpath(mx, my);
                }
            }
            switch (type) {
            case PathIterator.SEG_MOVETO:
                mx = point[0];
                my = point[1];
                consumer.beginSubpath(point[0], point[1]);
                break;
            case PathIterator.SEG_LINETO:
                consumer.appendLine(point[0], point[1]);
                break;
            case PathIterator.SEG_QUADTO:
                consumer.appendQuadratic(point[0], point[1],
                                         point[2], point[3]);
                break;
            case PathIterator.SEG_CUBICTO:
                consumer.appendCubic(point[0], point[1],
                                     point[2], point[3],
                                     point[4], point[5]);
                break;
            case PathIterator.SEG_CLOSE:
                consumer.closedSubpath();
                pathClosed = true;
                break;
            }
            pi.next();
        }

        consumer.endPath();
    } catch (PathException e) {
        throw new InternalError("Unable to Stroke shape ("+
                                e.getMessage()+")", e);
    }
}
 
Example 13
Source File: GeneralPathSerializer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Writes a serializable object description to the given object output stream.
 *
 * @param o   the to be serialized object.
 * @param out the outputstream that should receive the object.
 * @throws java.io.IOException if an I/O error occured.
 */
public void writeObject( final Object o, final ObjectOutputStream out )
  throws IOException {
  final GeneralPath gp = (GeneralPath) o;
  final PathIterator it = gp.getPathIterator( new AffineTransform() );
  out.writeInt( it.getWindingRule() );
  while ( it.isDone() == false ) {
    final float[] corrds = new float[ 6 ];
    final int type = it.currentSegment( corrds );
    out.writeInt( type );

    switch( type ) {
      case PathIterator.SEG_MOVETO: {
        out.writeFloat( corrds[ 0 ] );
        out.writeFloat( corrds[ 1 ] );
        break;
      }
      case PathIterator.SEG_LINETO: {
        out.writeFloat( corrds[ 0 ] );
        out.writeFloat( corrds[ 1 ] );
        break;
      }
      case PathIterator.SEG_QUADTO: {
        out.writeFloat( corrds[ 0 ] );
        out.writeFloat( corrds[ 1 ] );
        out.writeFloat( corrds[ 2 ] );
        out.writeFloat( corrds[ 3 ] );
        break;
      }
      case PathIterator.SEG_CUBICTO: {
        out.writeFloat( corrds[ 0 ] );
        out.writeFloat( corrds[ 1 ] );
        out.writeFloat( corrds[ 2 ] );
        out.writeFloat( corrds[ 3 ] );
        out.writeFloat( corrds[ 4 ] );
        out.writeFloat( corrds[ 5 ] );
        break;
      }
      case PathIterator.SEG_CLOSE: {
        break;
      }
      default:
        throw new IOException( "Unexpected type encountered: " + type );
    }
    it.next();
  }
  out.writeInt( -1 );
}
 
Example 14
Source File: PdfGraphics2D.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void followPath( Shape s, final int drawType ) {
  if ( s == null ) {
    return;
  }
  if ( drawType == PdfGraphics2D.STROKE ) {
    if ( !( stroke instanceof BasicStroke ) ) {
      s = stroke.createStrokedShape( s );
      followPath( s, PdfGraphics2D.FILL );
      return;
    }
  }
  if ( drawType == PdfGraphics2D.STROKE ) {
    setStrokeDiff( stroke, oldStroke );
    oldStroke = stroke;
    setStrokePaint();
  } else if ( drawType == PdfGraphics2D.FILL ) {
    setFillPaint();
  }
  final PathIterator points;
  if ( drawType == PdfGraphics2D.CLIP ) {
    points = s.getPathIterator( PdfGraphics2D.IDENTITY );
  } else {
    points = s.getPathIterator( transform );
  }
  final float[] coords = new float[6];
  int traces = 0;
  while ( !points.isDone() ) {
    ++traces;
    final int segtype = points.currentSegment( coords );
    normalizeY( coords );
    switch ( segtype ) {
      case PathIterator.SEG_CLOSE:
        cb.closePath();
        break;

      case PathIterator.SEG_CUBICTO:
        cb.curveTo( coords[0], coords[1], coords[2], coords[3], coords[4], coords[5] );
        break;

      case PathIterator.SEG_LINETO:
        cb.lineTo( coords[0], coords[1] );
        break;

      case PathIterator.SEG_MOVETO:
        cb.moveTo( coords[0], coords[1] );
        break;

      case PathIterator.SEG_QUADTO:
        cb.curveTo( coords[0], coords[1], coords[2], coords[3] );
        break;
      default:
        throw new IllegalStateException( "Invalid segment type in path" );
    }
    points.next();
  }
  switch ( drawType ) {
    case PdfGraphics2D.FILL:
      if ( traces > 0 ) {
        if ( points.getWindingRule() == PathIterator.WIND_EVEN_ODD ) {
          cb.eoFill();
        } else {
          cb.fill();
        }
      }
      break;
    case PdfGraphics2D.STROKE:
      if ( traces > 0 ) {
        cb.stroke();
      }
      break;
    default: // drawType==CLIP
      if ( traces == 0 ) {
        cb.rectangle( 0, 0, 0, 0 );
      }
      if ( points.getWindingRule() == PathIterator.WIND_EVEN_ODD ) {
        cb.eoClip();
      } else {
        cb.clip();
      }
      cb.newPath();
  }
}
 
Example 15
Source File: Scribbler.java    From pumpernickel with MIT License 4 votes vote down vote up
/**
 * 
 * @param tol
 *            a float from [0,1]
 * @param movement
 *            a float from [0,1]
 */
public static void create(Shape s, float tol, float movement,
		long randomSeed, PathWriter writer) {
	Random random = new Random(randomSeed);
	PathIterator i = s.getPathIterator(null, .2);
	float[] f = new float[6];
	float x = 0;
	float y = 0;
	float dx = 0;
	float dy = 0;
	int segments;
	float startX;
	float startY;
	float progress;
	float newX, newY;
	while (i.isDone() == false) {
		int k = i.currentSegment(f);
		if (k == PathIterator.SEG_MOVETO) {
			x = f[0];
			y = f[1];
			writer.moveTo(x, y);
		} else if (k == PathIterator.SEG_LINETO) {
			random.setSeed(randomSeed);
			startX = x;
			startY = y;
			segments = (int) (Math.sqrt((f[0] - x) * (f[0] - x)
					+ (f[1] - y) * (f[1] - y)) / 10 + .5);
			if (segments <= 0)
				segments = 1;

			for (k = 0; k < segments; k++) {
				dx += movement / 2 - movement * random.nextFloat();
				if (dx > tol)
					dx = tol;
				if (dx < -tol)
					dx = -tol;
				dy += movement / 2 - movement * random.nextFloat();
				if (dy > tol)
					dy = tol;
				if (dy < -tol)
					dy = -tol;
				progress = ((float) k + 1) / (segments);
				newX = f[0] * progress + startX * (1 - progress) + dx;
				newY = f[1] * progress + startY * (1 - progress) + dy;
				writer.lineTo(newX, newY);
				x = newX;
				y = newY;
			}
			x = f[0];
			y = f[1];
		} else if (k == PathIterator.SEG_CLOSE) {
			writer.closePath();
		}
		i.next();
	}
}
 
Example 16
Source File: PdfGraphics2D.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private void followPath(Shape s, int drawType) {
	if (s == null) {
		return;
	}
	if (drawType == STROKE) {
		if (!(stroke instanceof BasicStroke)) {
			s = stroke.createStrokedShape(s);
			followPath(s, FILL);
			return;
		}
	}
	if (drawType == STROKE) {
		setStrokeDiff(stroke, oldStroke);
		oldStroke = stroke;
		setStrokePaint();
	} else if (drawType == FILL) {
		setFillPaint();
	}
	PathIterator points;
	int traces = 0;
	if (drawType == CLIP) {
		points = s.getPathIterator(IDENTITY);
	} else {
		points = s.getPathIterator(transform);
	}
	float[] coords = new float[6];
	while (!points.isDone()) {
		++traces;
		int segtype = points.currentSegment(coords);
		normalizeY(coords);
		switch (segtype) {
			case PathIterator.SEG_CLOSE:
				cb.closePath();
				break;

			case PathIterator.SEG_CUBICTO:
				cb.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
				break;

			case PathIterator.SEG_LINETO:
				cb.lineTo(coords[0], coords[1]);
				break;

			case PathIterator.SEG_MOVETO:
				cb.moveTo(coords[0], coords[1]);
				break;

			case PathIterator.SEG_QUADTO:
				cb.curveTo(coords[0], coords[1], coords[2], coords[3]);
				break;
		}
		points.next();
	}
	switch (drawType) {
		case FILL:
			if (traces > 0) {
				if (points.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
					cb.eoFill();
				} else {
					cb.fill();
				}
			}
			break;
		case STROKE:
			if (traces > 0) {
				cb.stroke();
			}
			break;
		default: // drawType==CLIP
			if (traces == 0) {
				cb.rectangle(0, 0, 0, 0);
			}
			if (points.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
				cb.eoClip();
			} else {
				cb.clip();
			}
			cb.newPath();
	}
}
 
Example 17
Source File: Crossings.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static Crossings findCrossings(PathIterator pi,
                                      double xlo, double ylo,
                                      double xhi, double yhi)
{
    Crossings cross;
    if (pi.getWindingRule() == pi.WIND_EVEN_ODD) {
        cross = new EvenOdd(xlo, ylo, xhi, yhi);
    } else {
        cross = new NonZero(xlo, ylo, xhi, yhi);
    }
    // coords array is big enough for holding:
    //     coordinates returned from currentSegment (6)
    //     OR
    //         two subdivided quadratic curves (2+4+4=10)
    //         AND
    //             0-1 horizontal splitting parameters
    //             OR
    //             2 parametric equation derivative coefficients
    //     OR
    //         three subdivided cubic curves (2+6+6+6=20)
    //         AND
    //             0-2 horizontal splitting parameters
    //             OR
    //             3 parametric equation derivative coefficients
    double coords[] = new double[23];
    double movx = 0;
    double movy = 0;
    double curx = 0;
    double cury = 0;
    double newx, newy;
    while (!pi.isDone()) {
        int type = pi.currentSegment(coords);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            if (movy != cury &&
                cross.accumulateLine(curx, cury, movx, movy))
            {
                return null;
            }
            movx = curx = coords[0];
            movy = cury = coords[1];
            break;
        case PathIterator.SEG_LINETO:
            newx = coords[0];
            newy = coords[1];
            if (cross.accumulateLine(curx, cury, newx, newy)) {
                return null;
            }
            curx = newx;
            cury = newy;
            break;
        case PathIterator.SEG_QUADTO:
            newx = coords[2];
            newy = coords[3];
            if (cross.accumulateQuad(curx, cury, coords)) {
                return null;
            }
            curx = newx;
            cury = newy;
            break;
        case PathIterator.SEG_CUBICTO:
            newx = coords[4];
            newy = coords[5];
            if (cross.accumulateCubic(curx, cury, coords)) {
                return null;
            }
            curx = newx;
            cury = newy;
            break;
        case PathIterator.SEG_CLOSE:
            if (movy != cury &&
                cross.accumulateLine(curx, cury, movx, movy))
            {
                return null;
            }
            curx = movx;
            cury = movy;
            break;
        }
        pi.next();
    }
    if (movy != cury) {
        if (cross.accumulateLine(curx, cury, movx, movy)) {
            return null;
        }
    }
    if (debug) {
        cross.print();
    }
    return cross;
}
 
Example 18
Source File: ShapeStringUtils.java    From pumpernickel with MIT License 4 votes vote down vote up
public static String toString(PathIterator i) {
	float[] f = new float[6];
	// TODO: didn't you read once that a string buffer
	// is a little inefficient?
	StringBuffer sb = new StringBuffer();
	int k;
	int j = 0;
	while (i.isDone() == false) {
		k = i.currentSegment(f);

		if (k == PathIterator.SEG_MOVETO) {
			sb.append('m');
			j = 2;
		} else if (k == PathIterator.SEG_LINETO) {
			sb.append('l');
			j = 2;
		} else if (k == PathIterator.SEG_QUADTO) {
			sb.append('q');
			j = 4;
		} else if (k == PathIterator.SEG_CUBICTO) {
			sb.append('c');
			j = 6;
		} else if (k == PathIterator.SEG_CLOSE) {
			sb.append('z');
			j = 0;
		}
		if (j != 0) {
			sb.append(' ');
			for (int a = 0; a < j; a++) {
				sb.append(Float.toString(f[a]));
				if (a < j - 1)
					sb.append(' ');
			}
		}

		i.next();
		if (i.isDone() == false)
			sb.append(' ');
	}
	return sb.toString();
}
 
Example 19
Source File: WPathGraphics.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Given a Java2D <code>PathIterator</code> instance,
 * this method translates that into a Window's path
 * in the printer device context.
 */
private void convertToWPath(PathIterator pathIter) {

    float[] segment = new float[6];
    int segmentType;

    WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();

    /* Map the PathIterator's fill rule into the Window's
     * polygon fill rule.
     */
    int polyFillRule;
    if (pathIter.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
        polyFillRule = WPrinterJob.POLYFILL_ALTERNATE;
    } else {
        polyFillRule = WPrinterJob.POLYFILL_WINDING;
    }
    wPrinterJob.setPolyFillMode(polyFillRule);

    wPrinterJob.beginPath();

    while (pathIter.isDone() == false) {
        segmentType = pathIter.currentSegment(segment);

        switch (segmentType) {
         case PathIterator.SEG_MOVETO:
            wPrinterJob.moveTo(segment[0], segment[1]);
            break;

         case PathIterator.SEG_LINETO:
            wPrinterJob.lineTo(segment[0], segment[1]);
            break;

        /* Convert the quad path to a bezier.
         */
         case PathIterator.SEG_QUADTO:
            int lastX = wPrinterJob.getPenX();
            int lastY = wPrinterJob.getPenY();
            float c1x = lastX + (segment[0] - lastX) * 2 / 3;
            float c1y = lastY + (segment[1] - lastY) * 2 / 3;
            float c2x = segment[2] - (segment[2] - segment[0]) * 2/ 3;
            float c2y = segment[3] - (segment[3] - segment[1]) * 2/ 3;
            wPrinterJob.polyBezierTo(c1x, c1y,
                                     c2x, c2y,
                                     segment[2], segment[3]);
            break;

         case PathIterator.SEG_CUBICTO:
            wPrinterJob.polyBezierTo(segment[0], segment[1],
                                     segment[2], segment[3],
                                     segment[4], segment[5]);
            break;

         case PathIterator.SEG_CLOSE:
            wPrinterJob.closeFigure();
            break;
        }


        pathIter.next();
    }

    wPrinterJob.endPath();

}
 
Example 20
Source File: DuctusRenderingEngine.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void feedConsumer(PathConsumer consumer, PathIterator pi) {
    try {
        consumer.beginPath();
        boolean pathClosed = false;
        float mx = 0.0f;
        float my = 0.0f;
        float point[]  = new float[6];

        while (!pi.isDone()) {
            int type = pi.currentSegment(point);
            if (pathClosed == true) {
                pathClosed = false;
                if (type != PathIterator.SEG_MOVETO) {
                    // Force current point back to last moveto point
                    consumer.beginSubpath(mx, my);
                }
            }
            switch (type) {
            case PathIterator.SEG_MOVETO:
                mx = point[0];
                my = point[1];
                consumer.beginSubpath(point[0], point[1]);
                break;
            case PathIterator.SEG_LINETO:
                consumer.appendLine(point[0], point[1]);
                break;
            case PathIterator.SEG_QUADTO:
                consumer.appendQuadratic(point[0], point[1],
                                         point[2], point[3]);
                break;
            case PathIterator.SEG_CUBICTO:
                consumer.appendCubic(point[0], point[1],
                                     point[2], point[3],
                                     point[4], point[5]);
                break;
            case PathIterator.SEG_CLOSE:
                consumer.closedSubpath();
                pathClosed = true;
                break;
            }
            pi.next();
        }

        consumer.endPath();
    } catch (PathException e) {
        throw new InternalError("Unable to Stroke shape ("+
                                e.getMessage()+")", e);
    }
}