Examples of FlatteningPathIterator


Examples of java.awt.geom.FlatteningPathIterator

    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;
        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;
    }
View Full Code Here

Examples of java.awt.geom.FlatteningPathIterator

        return result;
    }

    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];
                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;
    }
View Full Code Here

Examples of java.awt.geom.FlatteningPathIterator

        this.theShape = t.createTransformedShape(shape);
    }

    public Shape createStrokedShape(Shape shape) {
        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 = phase;

        while (!it.isDone()) {
            type = it.currentSegment(points);
            switch (type) {
            case PathIterator.SEG_MOVETO:
                moveX = lastX = points[0];
                moveY = lastY = points[1];
                result.moveTo(moveX, moveY);
                next = 0;
                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) Math.sqrt(dx * dx + dy * dy);
                if (distance >= next) {
                    float r = 1.0f / distance;
                    float angle = (float) Math.atan2(dy, dx);
                    while (distance >= next) {
                        float x = lastX + next * dx * r;
                        float y = lastY + next * dy * r;
                        t.setToTranslation(x, y);
                        t.rotate(angle);
                        result.append(t.createTransformedShape(theShape), false);
                        next += advance;
                    }
                }
                next -= distance;
                lastX = thisX;
                lastY = thisY;
                break;
            }
            it.next();
        }

        return result;
    }
View Full Code Here

Examples of java.awt.geom.FlatteningPathIterator

  private Shape dashedStroke(PathIterator pi)
  {
    // The choice of (flatnessSq == width / 3) is made to be consistent with
    // the flattening in CubicSegment.getDisplacedSegments
    FlatteningPathIterator flat = new FlatteningPathIterator(pi,
                                                             Math.sqrt(width / 3));

    // Holds the endpoint of the current segment (or piece of a segment)
    double[] coords = new double[2];

    // Holds end of the last segment
    double x, y, x0, y0;
    x = x0 = y = y0 = 0;

    // Various useful flags
    boolean pathOpen = false;
    boolean dashOn = true;
    boolean offsetting = (phase != 0);

    // How far we are into the current dash
    double distance = 0;
    int dashIndex = 0;

    // And variables to hold the final output
    GeneralPath output = new GeneralPath();
    Segment[] p;

    // Iterate over the FlatteningPathIterator
    while (! flat.isDone())
      {
        switch (flat.currentSegment(coords))
          {
          case PathIterator.SEG_MOVETO:
            x0 = x = coords[0];
            y0 = y = coords[1];

            if (pathOpen)
              {
                capEnds();
                convertPath(output, start);
                start = end = null;
                pathOpen = false;
              }

            break;

          case PathIterator.SEG_LINETO:
            boolean segmentConsumed = false;

            while (! segmentConsumed)
              {
                // Find the total remaining length of this segment
                double segLength = Math.sqrt((x - coords[0]) * (x - coords[0])
                                             + (y - coords[1])
                                             * (y - coords[1]));
                boolean spanBoundary = true;
                double[] segmentEnd = null;

                // The current segment fits entirely inside the current dash
                if ((offsetting && distance + segLength <= phase)
                    || distance + segLength <= dash[dashIndex])
                  {
                    spanBoundary = false;
                  }
               
                // Otherwise, we need to split the segment in two, as this
                // segment spans a dash boundry
                else
                  {
                    segmentEnd = (double[]) coords.clone();

                    // Calculate the remaining distance in this dash,
                    // and coordinates of the dash boundary
                    double reqLength;
                    if (offsetting)
                      reqLength = phase - distance;
                    else
                      reqLength = dash[dashIndex] - distance;

                    coords[0] = x + ((coords[0] - x) * reqLength / segLength);
                    coords[1] = y + ((coords[1] - y) * reqLength / segLength);
                  }

                if (offsetting || ! dashOn)
                  {
                    // Dash is off, or we are in offset - treat this as a
                    // moveTo
                    x0 = x = coords[0];
                    y0 = y = coords[1];

                    if (pathOpen)
                      {
                        capEnds();
                        convertPath(output, start);
                        start = end = null;
                        pathOpen = false;
                      }
                  }
                else
                  {
                    // Dash is on - treat this as a lineTo
                    p = (new LineSegment(x, y, coords[0], coords[1])).getDisplacedSegments(width / 2.0);

                    if (! pathOpen)
                      {
                        start = p[0];
                        end = p[1];
                        pathOpen = true;
                      }
                    else
                      addSegments(p);

                    x = coords[0];
                    y = coords[1];
                  }

                // Update variables depending on whether we spanned a
                // dash boundary or not
                if (! spanBoundary)
                  {
                    distance += segLength;
                    segmentConsumed = true;
                  }
                else
                  {
                    if (offsetting)
                      offsetting = false;
                    dashOn = ! dashOn;
                    distance = 0;
                    coords = segmentEnd;

                    if (dashIndex + 1 == dash.length)
                      dashIndex = 0;
                    else
                      dashIndex++;

                    // Since the value of segmentConsumed is still false,
                    // the next run of the while loop will complete the segment
                  }
              }
            break;

          // This is a flattened path, so we don't need to deal with curves
          }
        flat.next();
      }

    if (pathOpen)
      {
        capEnds();
View Full Code Here

Examples of java.awt.geom.FlatteningPathIterator

  class ZigzagStroke implements Stroke {
    private static final float FLATNESS = 1;

    public Shape createStrokedShape (Shape shape) {
      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 phase = 0;
      while (!it.isDone()) {
        type = it.currentSegment(points);
        switch (type) {
        case PathIterator.SEG_MOVETO:
          moveX = lastX = points[0];
          moveY = lastY = points[1];
          result.moveTo(moveX, moveY);
          next = wavelength / 2;
          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)Math.sqrt(dx * dx + dy * dy);
          if (distance >= next) {
            float r = 1.0f / distance;
            while (distance >= next) {
              float x = lastX + next * dx * r;
              float y = lastY + next * dy * r;
              if ((phase & 1) == 0)
                result.lineTo(x + amplitude * dy * r, y - amplitude * dx * r);
              else
                result.lineTo(x - amplitude * dy * r, y + amplitude * dx * r);
              next += wavelength;
              phase++;
            }
          }
          next -= distance;
          lastX = thisX;
          lastY = thisY;
          if (type == PathIterator.SEG_CLOSE) result.closePath();
          break;
        }
        it.next();
      }
      return new BasicStroke(getWidth(), BasicStroke.CAP_SQUARE, getJoin()).createStrokedShape(result);
    }
View Full Code Here

Examples of java.awt.geom.FlatteningPathIterator

    private static final float FLATNESS = 1;

    public Shape createStrokedShape (Shape shape) {
      GeneralPath result = new GeneralPath();
      shape = new BasicStroke(getWidth(), BasicStroke.CAP_SQUARE, getJoin()).createStrokedShape(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 next = 0;
      while (!it.isDone()) {
        type = it.currentSegment(points);
        switch (type) {
        case PathIterator.SEG_MOVETO:
          moveX = lastX = randomize(points[0]);
          moveY = lastY = randomize(points[1]);
          result.moveTo(moveX, moveY);
          next = 0;
          break;

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

        case PathIterator.SEG_LINETO:
          thisX = randomize(points[0]);
          thisY = randomize(points[1]);
          float dx = thisX - lastX;
          float dy = thisY - lastY;
          float distance = (float)Math.sqrt(dx * dx + dy * dy);
          if (distance >= next) {
            float r = 1.0f / distance;
            while (distance >= next) {
              float x = lastX + next * dx * r;
              float y = lastY + next * dy * r;
              result.lineTo(randomize(x), randomize(y));
              next += detail;
            }
          }
          next -= distance;
          lastX = thisX;
          lastY = thisY;
          break;
        }
        it.next();
      }

      return result;
    }
View Full Code Here

Examples of java.awt.geom.FlatteningPathIterator

  {
    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;
    // boolean first = false;
    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;
    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 );
          // first = true;
          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)Math.sqrt( dx * dx + dy * dy );
          if( distance >= next )
          {
            float r = 1.0f / distance;
            float angle = (float)Math.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 );
              result.append( t.createTransformedShape( glyph ), false );
              next += ( advance + nextAdvance ) * factor;
              currentChar++;
              if( repeat )
                currentChar %= length;
            }
          }
          next -= distance;
          // first = false;
          lastX = thisX;
          lastY = thisY;
          break;
      }
      it.next();
    }

    return result;
  }
View Full Code Here

Examples of java.awt.geom.FlatteningPathIterator

   *            the shape
   * @return the float
   */
  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];
          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;
  }
View Full Code Here

Examples of java.awt.geom.FlatteningPathIterator

    protected void initialise() {

  pathLength = 0f;

  FlatteningPathIterator fpi = new FlatteningPathIterator(path.getPathIterator(new AffineTransform()), 0.01f);
  segments = new Vector(20);
  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));

  while (!fpi.isDone()) {
     
      segType = fpi.currentSegment(seg);
     
      switch (segType) {
   
      case PathIterator.SEG_MOVETO:
   
    //System.out.println("== MOVE TO " + seg[0] + " " + seg[1]);
   
    segments.add(new PathSegment(segType, seg[0], seg[1], pathLength));
    currentX = seg[0];
    currentY = seg[1];
    lastMoveX = currentX;
    lastMoveY = currentY;
   
    break;
   
      case PathIterator.SEG_LINETO:
   
    //System.out.println("== LINE TO " + seg[0] + " " + seg[1]);
   
    pathLength += Point2D.distance(currentX, currentY, seg[0], seg[1]);
    segments.add(new PathSegment(segType, seg[0], seg[1], pathLength));

    currentX = seg[0];
    currentY = seg[1];
   
    break;
   
      case PathIterator.SEG_CLOSE:
   
    //System.out.println("== CLOSE TO " + lastMoveX + " " + lastMoveY);

    pathLength += Point2D.distance(currentX, currentY, lastMoveX, lastMoveY);
    segments.add(new PathSegment(PathIterator.SEG_LINETO, lastMoveX, lastMoveY, pathLength));
       
    currentX = lastMoveX;
    currentY = lastMoveY;
       
    break;

      default:
       
    // ouch, where have these come from
    System.out.println("Bad path segment types");
   
      }
     
      fpi.next();
     
  }

  initialised = true;
View Full Code Here

Examples of java.awt.geom.FlatteningPathIterator

    /**
     * @see java.awt.Stroke#createStrokedShape(java.awt.Shape)
     */
    public Shape createStrokedShape (Shape shape) {
      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 phase = 0;
      while (!it.isDone()) {
        type = it.currentSegment(points);
        switch (type) {
        case PathIterator.SEG_MOVETO:
          moveX = lastX = points[0];
          moveY = lastY = points[1];
          result.moveTo(moveX, moveY);
          next = wavelength / 2;
          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)Math.sqrt(dx * dx + dy * dy);
          if (distance >= next) {
            float r = 1.0f / distance;
            while (distance >= next) {
              float x = lastX + next * dx * r;
              float y = lastY + next * dy * r;
              if ((phase & 1) == 0)
                result.lineTo(x + amplitude * dy * r, y - amplitude * dx * r);
              else
                result.lineTo(x - amplitude * dy * r, y + amplitude * dx * r);
              next += wavelength;
              phase++;
            }
          }
          next -= distance;
          lastX = thisX;
          lastY = thisY;
          if (type == PathIterator.SEG_CLOSE) result.closePath();
          break;
        }
        it.next();
      }
      return new BasicStroke(getWidth(), BasicStroke.CAP_SQUARE, getJoin()).createStrokedShape(result);
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.