List of usage examples for java.awt.geom PathIterator isDone
public boolean isDone();
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 www. j a v a2 s.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:longMethod.jfreechart.drawItem.SamplingXYLineRenderer.java
/** * Draws the visual representation of a single data item. * * @param g2 the graphics device./* w w w.j av a 2 s . c o m*/ * @param state the renderer state. * @param dataArea the area within which the data is being drawn. * @param info collects information about the drawing. * @param plot the plot (can be used to obtain standard color * information etc). * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param dataset the dataset. * @param series the series index (zero-based). * @param item the item index (zero-based). * @param crosshairState crosshair information for the plot * (<code>null</code> permitted). * @param pass the pass index. */ 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) { // do nothing if item is not visible if (!getItemVisible(series, item)) { return; } RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); // get the data point... double x1 = dataset.getXValue(series, item); double y1 = dataset.getYValue(series, item); double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation); double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation); State s = (State) state; // update path to reflect latest point if (!Double.isNaN(transX1) && !Double.isNaN(transY1)) { float x = (float) transX1; float y = (float) transY1; PlotOrientation orientation = plot.getOrientation(); if (orientation == PlotOrientation.HORIZONTAL) { x = (float) transY1; y = (float) transX1; } if (s.lastPointGood) { if ((Math.abs(x - s.lastX) > s.dX)) { s.seriesPath.lineTo(x, y); if (s.lowY < s.highY) { s.intervalPath.moveTo((float) s.lastX, (float) s.lowY); s.intervalPath.lineTo((float) s.lastX, (float) s.highY); } s.lastX = x; s.openY = y; s.highY = y; s.lowY = y; s.closeY = y; } else { s.highY = Math.max(s.highY, y); s.lowY = Math.min(s.lowY, y); s.closeY = y; } } else { s.seriesPath.moveTo(x, y); s.lastX = x; s.openY = y; s.highY = y; s.lowY = y; s.closeY = y; } s.lastPointGood = true; } else { s.lastPointGood = false; } // if this is the last item, draw the path ... if (item == s.getLastItemIndex()) { // draw path PathIterator pi = s.seriesPath.getPathIterator(null); int count = 0; while (!pi.isDone()) { count++; pi.next(); } g2.setStroke(getItemStroke(series, item)); g2.setPaint(getItemPaint(series, item)); g2.draw(s.seriesPath); g2.draw(s.intervalPath); } }
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 a 2 s .co 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; }
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;//from w w w .ja v a 2 s . 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:ExtendedGeneralPath.java
/** * Delegates to the enclosed <code>GeneralPath</code>. *//*from w w w .j a v a2s. c o m*/ public void append(PathIterator pi, boolean connect) { double[] vals = new double[6]; while (!pi.isDone()) { Arrays.fill(vals, 0); int type = pi.currentSegment(vals); pi.next(); if (connect && (numVals != 0)) { if (type == PathIterator.SEG_MOVETO) { double x = vals[0]; double y = vals[1]; if ((x != cx) || (y != cy)) { // Change MOVETO to LINETO. type = PathIterator.SEG_LINETO; } else { // Redundent segment (move to current loc) drop it... if (pi.isDone()) break; // Nothing interesting type = pi.currentSegment(vals); pi.next(); } } connect = false; } switch (type) { case PathIterator.SEG_CLOSE: closePath(); break; case PathIterator.SEG_MOVETO: moveTo((float) vals[0], (float) vals[1]); break; case PathIterator.SEG_LINETO: lineTo((float) vals[0], (float) vals[1]); break; case PathIterator.SEG_QUADTO: quadTo((float) vals[0], (float) vals[1], (float) vals[2], (float) vals[3]); break; case PathIterator.SEG_CUBICTO: curveTo((float) vals[0], (float) vals[1], (float) vals[2], (float) vals[3], (float) vals[4], (float) vals[5]); break; } } }
From source file:SWTGraphics2D.java
/** * Converts an AWT <code>Shape</code> into a SWT <code>Path</code>. * * @param shape the shape (<code>null</code> not permitted). * * @return The path.//from ww w. jav a 2s . c o m */ private Path toSwtPath(Shape shape) { int type; float[] coords = new float[6]; Path path = new Path(this.gc.getDevice()); PathIterator pit = shape.getPathIterator(null); while (!pit.isDone()) { type = pit.currentSegment(coords); switch (type) { case (PathIterator.SEG_MOVETO): path.moveTo(coords[0], coords[1]); break; case (PathIterator.SEG_LINETO): path.lineTo(coords[0], coords[1]); break; case (PathIterator.SEG_QUADTO): path.quadTo(coords[0], coords[1], coords[2], coords[3]); break; case (PathIterator.SEG_CUBICTO): path.cubicTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]); break; case (PathIterator.SEG_CLOSE): path.close(); break; default: break; } pit.next(); } return path; }
From source file:PathLength.java
/** * Flattens the path and determines the path length. *///from w w w . j a v a 2s.com protected void initialise() { pathLength = 0f; PathIterator pi = path.getPathIterator(new AffineTransform()); SingleSegmentPathIterator sspi = new SingleSegmentPathIterator(); segments = new ArrayList(20); List indexes = new ArrayList(20); int index = 0; int origIndex = -1; float lastMoveX = 0f; float lastMoveY = 0f; float currentX = 0f; float currentY = 0f; float[] seg = new float[6]; int segType; segments.add(new PathSegment(PathIterator.SEG_MOVETO, 0f, 0f, 0f, origIndex)); while (!pi.isDone()) { origIndex++; indexes.add(new Integer(index)); segType = pi.currentSegment(seg); switch (segType) { case PathIterator.SEG_MOVETO: segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex)); currentX = seg[0]; currentY = seg[1]; lastMoveX = currentX; lastMoveY = currentY; index++; pi.next(); break; case PathIterator.SEG_LINETO: pathLength += Point2D.distance(currentX, currentY, seg[0], seg[1]); segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex)); currentX = seg[0]; currentY = seg[1]; index++; pi.next(); break; case PathIterator.SEG_CLOSE: pathLength += Point2D.distance(currentX, currentY, lastMoveX, lastMoveY); segments.add(new PathSegment(PathIterator.SEG_LINETO, lastMoveX, lastMoveY, pathLength, origIndex)); currentX = lastMoveX; currentY = lastMoveY; index++; pi.next(); break; default: sspi.setPathIterator(pi, currentX, currentY); FlatteningPathIterator fpi = new FlatteningPathIterator(sspi, 0.01f); while (!fpi.isDone()) { segType = fpi.currentSegment(seg); if (segType == PathIterator.SEG_LINETO) { pathLength += Point2D.distance(currentX, currentY, seg[0], seg[1]); segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex)); currentX = seg[0]; currentY = seg[1]; index++; } fpi.next(); } } } segmentIndexes = new int[indexes.size()]; for (int i = 0; i < segmentIndexes.length; i++) { segmentIndexes[i] = ((Integer) indexes.get(i)).intValue(); } initialised = true; }
From source file:org.apache.fop.afp.AFPGraphics2D.java
/** * Processes a path iterator generating the necessary painting operations. * * @param iter PathIterator to process//www . j a v a2s. c o m */ private void processPathIterator(PathIterator iter) { double[] dstPts = new double[6]; double[] currentPosition = new double[2]; for (int[] openingCoords = new int[2]; !iter.isDone(); iter.next()) { switch (iter.currentSegment(dstPts)) { case PathIterator.SEG_LINETO: graphicsObj.addLine(new int[] { (int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]) }, true); currentPosition = new double[] { dstPts[X], dstPts[Y] }; break; case PathIterator.SEG_QUADTO: graphicsObj.addFillet(new int[] { (int) Math.round(dstPts[X1]), (int) Math.round(dstPts[Y1]), (int) Math.round(dstPts[X2]), (int) Math.round(dstPts[Y2]) }, true); currentPosition = new double[] { dstPts[X2], dstPts[Y2] }; break; case PathIterator.SEG_CUBICTO: double[] cubicCoords = new double[] { currentPosition[0], currentPosition[1], dstPts[X1], dstPts[Y1], dstPts[X2], dstPts[Y2], dstPts[X3], dstPts[Y3] }; double[][] quadParts = CubicBezierApproximator.fixedMidPointApproximation(cubicCoords); if (quadParts.length >= 4) { for (int segIndex = 0; segIndex < quadParts.length; segIndex++) { double[] quadPts = quadParts[segIndex]; if (quadPts != null && quadPts.length == 4) { graphicsObj.addFillet( new int[] { (int) Math.round(quadPts[X1]), (int) Math.round(quadPts[Y1]), (int) Math.round(quadPts[X2]), (int) Math.round(quadPts[Y2]) }, true); currentPosition = new double[] { quadPts[X2], quadPts[Y2] }; } } } break; case PathIterator.SEG_MOVETO: openingCoords = new int[] { (int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]) }; currentPosition = new double[] { dstPts[X], dstPts[Y] }; graphicsObj.setCurrentPosition(openingCoords); break; case PathIterator.SEG_CLOSE: graphicsObj.addLine(openingCoords, true); currentPosition = new double[] { openingCoords[0], openingCoords[1] }; break; default: LOG.debug("Unrecognised path iterator type"); break; } } }
From source file:org.apache.pdfbox.pdfviewer.font.CFFGlyph2D.java
private GeneralPath transformGlyph(GeneralPath glyph) { // we have to invert all y-coordinates due to the moved 0,0-reference PathIterator iter = glyph.getPathIterator(null); float[] currentSegment = new float[6]; Path2D.Float path = new Path2D.Float(iter.getWindingRule()); boolean glyphTransformed = false; while (!iter.isDone()) { glyphTransformed = true;// w w w . j a va2 s. co m int type = iter.currentSegment(currentSegment); switch (type) { case PathIterator.SEG_MOVETO: path.moveTo(currentSegment[0], -currentSegment[1]); break; case PathIterator.SEG_LINETO: path.lineTo(currentSegment[0], -currentSegment[1]); break; case PathIterator.SEG_QUADTO: path.quadTo(currentSegment[0], -currentSegment[1], currentSegment[2], -currentSegment[3]); break; case PathIterator.SEG_CUBICTO: path.curveTo(currentSegment[0], -currentSegment[1], currentSegment[2], -currentSegment[3], currentSegment[4], -currentSegment[5]); break; case PathIterator.SEG_CLOSE: path.closePath(); break; } iter.next(); } if (glyphTransformed) { return new GeneralPath(path); } else { return glyph; } }
From source file:org.apache.pdfbox.rendering.PageDrawer.java
/** * Returns true if the given path is rectangular. *///from www .java 2s. c o m private boolean isRectangular(GeneralPath path) { PathIterator iter = path.getPathIterator(null); double[] coords = new double[6]; int count = 0; int[] xs = new int[4]; int[] ys = new int[4]; while (!iter.isDone()) { switch (iter.currentSegment(coords)) { case PathIterator.SEG_MOVETO: if (count == 0) { xs[count] = (int) Math.floor(coords[0]); ys[count] = (int) Math.floor(coords[1]); } else { return false; } count++; break; case PathIterator.SEG_LINETO: if (count < 4) { xs[count] = (int) Math.floor(coords[0]); ys[count] = (int) Math.floor(coords[1]); } else { return false; } count++; break; case PathIterator.SEG_CUBICTO: return false; case PathIterator.SEG_CLOSE: break; } iter.next(); } if (count == 4) { return xs[0] == xs[1] || xs[0] == xs[2] || ys[0] == ys[1] || ys[0] == ys[3]; } return false; }