/**
  * Writes the <code>shape</code>, <code>coords</code>, <code>href</code>,
  * <code>nohref</code> Attribute for the specified figure and shape.
  *
  * @return Returns true, if the polygon is inside of the image bounds.
  */
 private boolean writePolyAttributes(IXMLElement elem, SVGFigure f, Shape shape) {
     AffineTransform t = TRANSFORM.getClone(f);
     if (t == null) {
         t = drawingTransform;
     } else {
         t.preConcatenate(drawingTransform);
     }
     
     StringBuilder buf = new StringBuilder();
     float[] coords = new float[6];
     GeneralPath path = new GeneralPath();
     for (PathIterator i = shape.getPathIterator(t, 1.5f);
     ! i.isDone(); i.next()) {
         switch (i.currentSegment(coords)) {
             case PathIterator.SEG_MOVETO :
                 if (buf.length() != 0) {
                     throw new IllegalArgumentException("Illegal shape "+shape);
                 }
                 if (buf.length() != 0) {
                     buf.append(',');
                 }
                 buf.append((int) coords[0]);
                 buf.append(',');
                 buf.append((int) coords[1]);
                 path.moveTo(coords[0], coords[1]);
                 break;
             case PathIterator.SEG_LINETO :
                 if (buf.length() != 0) {
                     buf.append(',');
                 }
                 buf.append((int) coords[0]);
                 buf.append(',');
                 buf.append((int) coords[1]);
                 path.lineTo(coords[0], coords[1]);
                 break;
             case PathIterator.SEG_CLOSE :
                 path.closePath();
                 break;
             default :
                 throw new InternalError("Illegal segment type "+i.currentSegment(coords));
         }
     }
     elem.setAttribute("shape", "poly");
     elem.setAttribute("coords", buf.toString());
     writeHrefAttribute(elem, f);
     return path.intersects(new Rectangle2D.Float(bounds.x, bounds.y, bounds.width, bounds.height));
 }
    public void addGlyph(GlyphData gv, float x, float y) {
      AffineTransform at = AffineTransform.getTranslateInstance(x, y);
      PathIterator pi = gv.gp.getPathIterator(at);

      float[] coords = new float[6];

      while (!pi.isDone()) {
        int type = pi.currentSegment(coords);

        switch (type) {
          case PathIterator.SEG_MOVETO:
            moveTo(coords[0], coords[1]);
            break;
          case PathIterator.SEG_LINETO:
            lineTo(coords[0], coords[1]);
            break;
          case PathIterator.SEG_CUBICTO:
            curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
            break;
          case PathIterator.SEG_CLOSE:
            closePath();
            break;
          default:
            System.out.println("Unknown path type: " + type);
            break;
        }

        pi.next();
      }
    }
示例#3
0
 public String toString() {
   StringBuffer sb = new StringBuffer();
   sb.append("polygon(");
   sb.append(fillValue);
   sb.append(")[\n");
   PathIterator iterator = path.getPathIterator(null);
   float[] values = new float[2];
   while (!iterator.isDone()) {
     int type = iterator.currentSegment(values);
     Point2D pt = new Point2D.Double(values[0], values[1]);
     sb.append("\t");
     sb.append(pt);
     sb.append("\n");
     iterator.next();
   }
   //        sb.append(iterator);
   //        for(Point2D pt : point2Ds) {
   //            sb.append("\t");
   //            sb.append(pt);
   //            sb.append("\n");
   //        }
   //        sb.append(path.toString());
   sb.append("]");
   return sb.toString();
 }
示例#4
0
 private void addLine(
     AffineTransform at, ArrayList<PathSegment> segments, double[] coords, Line2D line) {
   PathIterator iterator = line.getPathIterator(at);
   iterator.currentSegment(coords);
   segments.add(new LinePathSegment(coords));
   iterator.next();
   iterator.currentSegment(coords);
   segments.add(new LinePathSegment(coords));
 }
示例#5
0
 private void addArc(
     AffineTransform at, ArrayList<PathSegment> segments, double[] coords, Arc2D arc) {
   PathIterator iterator;
   iterator = arc.getPathIterator(at);
   iterator.currentSegment(coords);
   segments.add(new LinePathSegment(coords));
   iterator.next();
   iterator.currentSegment(coords);
   segments.add(new CubicPathSegment(coords));
 }
示例#6
0
  public NewPolygon2D clip(Rectangle2D boundingBox) {
    Area thisArea = new Area(path);
    thisArea.intersect(new Area(boundingBox));
    PathIterator iterator = thisArea.getPathIterator(null);
    double[] v = new double[2];
    while (!iterator.isDone()) {
      int type = iterator.currentSegment(v);
      System.err.println(":" + v[0] + v[1] + "\n");
      iterator.next();
    }
    System.exit(-1);

    GeneralPath path = new GeneralPath(thisArea);
    path.closePath();
    NewPolygon2D newPolygon = new NewPolygon2D(path);
    newPolygon.setFillValue(this.getFillValue());
    return newPolygon;
  }
示例#7
0
    private void buildSegments(Border border, Jig jig, AffineTransform at) {
      segments = new ArrayList<PathSegment>();
      double[] coords = new double[6];

      PathIterator topIterator = jig.getTopLine().getPathIterator(at);
      topIterator.currentSegment(coords);
      MovePathSegment start = new MovePathSegment(coords);
      segments.add(start);
      topIterator.next();
      topIterator.currentSegment(coords);
      segments.add(new LinePathSegment(coords));

      if (border.topRightRadius > 0) addArc(at, segments, coords, jig.getTopRightArc());
      addLine(at, segments, coords, jig.getRightLine());
      if (border.bottomRightRadius > 0) addArc(at, segments, coords, jig.getBottomRightArc());
      addLine(at, segments, coords, jig.getBottomLine());
      if (border.bottomLeftRadius > 0) addArc(at, segments, coords, jig.getBottomLeftArc());
      addLine(at, segments, coords, jig.getLeftLine());
      if (border.topLeftRadius > 0) addArc(at, segments, coords, jig.getTopLeftArc());

      start.dump(coords);
      segments.add(new LinePathSegment(coords));
    }
示例#8
0
    /**
     * Converts an awt Area to a String representing an SVG path.
     *
     * @param a The Area to convert to an SVG path.
     * @returns An SVG specification of the passed in Area.
     */
    protected static String toSVGPath(Area a) {
      StringBuilder sb = new StringBuilder();

      PathIterator it = a.getPathIterator(null);
      if (null == it) {
        return new String();
      }

      // PathIterator is not a normal Java Iterator
      while (!it.isDone()) {
        double[] c = new double[6];

        switch (it.currentSegment(c)) {
          case PathIterator.SEG_MOVETO:
            sb.append(String.format("M%.2f,%.2f ", c[0], c[1]));
            break;
          case PathIterator.SEG_LINETO:
            sb.append(String.format("L%.2f,%.2f ", c[0], c[1]));
            break;
          case PathIterator.SEG_QUADTO:
            sb.append(String.format("Q%.2f,%.2f,%.2f,%.2f ", c[0], c[1], c[2], c[3]));
            break;
          case PathIterator.SEG_CUBICTO:
            sb.append(
                String.format(
                    "C%.2f,%.2f,%.2f,%.2f,%.2f,%.2f ", c[0], c[1], c[2], c[3], c[4], c[5]));
            break;
          case PathIterator.SEG_CLOSE:
            sb.append("Z");
            break;
        }

        // update
        it.next();
      }
      return sb.toString();
    }
示例#9
0
  protected int convertPathData(Shape s, AffineTransform at) {
    PathIterator pi = s.getPathIterator(at);

    double[] coords = new double[6];
    double currX = 0;
    double currY = 0;

    while (!pi.isDone()) {
      int curOp = pi.currentSegment(coords);

      int pointIndex;
      switch (curOp) {
        case PathIterator.SEG_MOVETO:
          ops.addByte(CAIRO_PATH_OP_MOVE_TO);
          pointIndex = points.getNextIndex();
          points.setX(pointIndex, DoubleToCairoFixed(coords[0]));
          points.setY(pointIndex, DoubleToCairoFixed(coords[1]));
          currX = coords[0];
          currY = coords[1];
          break;

        case PathIterator.SEG_LINETO:
          ops.addByte(CAIRO_PATH_OP_LINE_TO);
          pointIndex = points.getNextIndex();
          points.setX(pointIndex, DoubleToCairoFixed(coords[0]));
          points.setY(pointIndex, DoubleToCairoFixed(coords[1]));
          currX = coords[0];
          currY = coords[1];
          break;

          /** q0 = p0 q1 = (p0+2*p1)/3 q2 = (p2+2*p1)/3 q3 = p2 */
        case PathIterator.SEG_QUADTO:
          double x1 = coords[0];
          double y1 = coords[1];
          double x2, y2;
          double x3 = coords[2];
          double y3 = coords[3];

          x2 = x1 + (x3 - x1) / 3;
          y2 = y1 + (y3 - y1) / 3;
          x1 = currX + 2 * (x1 - currX) / 3;
          y1 = currY + 2 * (y1 - currY) / 3;

          ops.addByte(CAIRO_PATH_OP_CURVE_TO);
          pointIndex = points.getNextIndex();
          points.setX(pointIndex, DoubleToCairoFixed(x1));
          points.setY(pointIndex, DoubleToCairoFixed(y1));
          pointIndex = points.getNextIndex();
          points.setX(pointIndex, DoubleToCairoFixed(x2));
          points.setY(pointIndex, DoubleToCairoFixed(y2));
          pointIndex = points.getNextIndex();
          points.setX(pointIndex, DoubleToCairoFixed(x3));
          points.setY(pointIndex, DoubleToCairoFixed(y3));
          currX = x3;
          currY = y3;
          break;

        case PathIterator.SEG_CUBICTO:
          ops.addByte(CAIRO_PATH_OP_CURVE_TO);
          pointIndex = points.getNextIndex();
          points.setX(pointIndex, DoubleToCairoFixed(coords[0]));
          points.setY(pointIndex, DoubleToCairoFixed(coords[1]));
          pointIndex = points.getNextIndex();
          points.setX(pointIndex, DoubleToCairoFixed(coords[2]));
          points.setY(pointIndex, DoubleToCairoFixed(coords[3]));
          pointIndex = points.getNextIndex();
          points.setX(pointIndex, DoubleToCairoFixed(coords[4]));
          points.setY(pointIndex, DoubleToCairoFixed(coords[5]));
          currX = coords[4];
          currY = coords[5];
          break;

        case PathIterator.SEG_CLOSE:
          ops.addByte(CAIRO_PATH_OP_CLOSE_PATH);
          break;
      }

      pi.next();
    }

    return pi.getWindingRule();
  }
  /**
   * Wrapper method around the <tt>paintText()</tt> method of the <tt>VisualEdgePainter</tt>
   * interface. This method performs the calculation to determine the position where the text will
   * be drawn.
   */
  private void paintText(VisualEdge vEdge, Graphics2D g2d) {
    Point fromPoint = new Point();
    Point toPoint = new Point();
    GeneralPath gPath = vEdge.getGeneralPath();
    PathIterator iterator = gPath.getPathIterator(null);
    FontMetrics fontMetrics;
    float edgeSegment[] = new float[6];
    double currentLength = 0;
    float cumulativeLength = 0;
    float x1 = 0, y1 = 0, x2 = 0, y2 = 0;
    int segmentType;
    boolean firstPointInitialized = false;

    // Get the total length of the edge
    float edgeLength = vEdge.getEdgeLength(vEdge, fromPoint, toPoint);

    while (!iterator.isDone()) {
      segmentType = iterator.currentSegment(edgeSegment);

      switch (segmentType) {
        case PathIterator.SEG_LINETO:
        case PathIterator.SEG_MOVETO:
          x2 = edgeSegment[0];
          y2 = edgeSegment[1];
          break;
        case PathIterator.SEG_QUADTO:
          x2 = edgeSegment[2];
          y2 = edgeSegment[3];
          break;
        case PathIterator.SEG_CUBICTO:
          x2 = edgeSegment[4];
          y2 = edgeSegment[5];
      }

      if (firstPointInitialized) {
        currentLength = Point2D.distance(x1, y1, x2, y2);
        cumulativeLength += currentLength;
      }

      iterator.next();

      // If we are halfway through the length of the edge,
      // then paint the text
      if (cumulativeLength >= (edgeLength / 2) || cumulativeLength >= edgeLength) {
        // Ratio of the remaining half-length over the length of the current edge
        double ratio = ((edgeLength / 2) - (cumulativeLength - currentLength)) / currentLength;
        fontMetrics = vEdge.getFontMetrics();

        // Take into account the text's length
        this.paintText(
            g2d,
            vEdge.getFont(),
            vEdge.getFontcolor(),
            vEdge.getLabel(),
            (float)
                    (fromPoint.getX() < toPoint.getX()
                        ? (x1 + (Math.abs(x2 - x1) * ratio))
                        : (x1 - (Math.abs(x2 - x1) * ratio)))
                - fontMetrics.stringWidth(vEdge.getLabel()) / 2,
            (float)
                (fromPoint.getY() < toPoint.getY()
                    ? (y1 + (Math.abs(y2 - y1) * ratio))
                    : (y1 - (Math.abs(y2 - y1) * ratio))));
        break;
      }

      x1 = x2;
      y1 = y2;

      if (!firstPointInitialized) {
        firstPointInitialized = true;
      }
    }
  }
示例#11
0
  private void RegionChange(Shape s) {
    try {

      GeneralPath g = (GeneralPath) s;

      PathIterator iterator = g.getPathIterator(null);
      float[] floats = new float[6];
      int i = 0;
      int x[] = new int[5];
      int y[] = new int[5];

      while (!iterator.isDone()) {

        iterator.currentSegment(floats);

        x[i] = (int) floats[0];
        y[i] = (int) floats[1];
        i++;
        iterator.next();
      }

      panConnection con = new panConnection();

      ArrayList<Shape> shapes = con.getAllRegions();

      p =
          new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {

              // repaint all regions
              ArrayList<Shape> shapes;
              try {
                shapes = con.getAllRegions();

                Graphics2D g2d = (Graphics2D) g;
                for (int i = 0; i < shapes.size(); i++) {
                  g2d.draw(shapes.get(i));
                }

              } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }

              // paint inner shapes
              try {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setColor(Color.red);
                // ponds
                shapes = con.getInnerPonds(x, y);
                for (int i = 0; i < shapes.size(); i++) {
                  Rectangle2D rect = shapes.get(i).getBounds2D();
                  g2d.setPaint(Color.RED);
                  g2d.fillOval(
                      (int) rect.getCenterX() - ((int) rect.getWidth() / 2),
                      (int) rect.getCenterY() - ((int) rect.getHeight() / 2),
                      (int) rect.getWidth(),
                      (int) rect.getHeight());

                  g2d.draw(shapes.get(i));
                }
                // lions
                ArrayList<Point2D> lions;
                lions = con.getInnerLions(x, y);
                for (int i = 0; i < lions.size(); i++) {
                  g2d.setColor(Color.RED);
                  g2d.fillOval((int) lions.get(i).getX(), (int) lions.get(i).getY(), 5, 5);

                  g2d.drawOval((int) lions.get(i).getX(), (int) lions.get(i).getY(), 5, 5);
                }

              } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }

              // paint outside shapes
              try {
                Graphics2D g2d = (Graphics2D) g;
                g2d.setColor(Color.BLACK);
                // ponds
                shapes = con.getOuterPonds(x, y);
                for (int i = 0; i < shapes.size(); i++) {
                  Rectangle2D rect = shapes.get(i).getBounds2D();

                  //    g2d.fill(circle);
                  g2d.setPaint(Color.BLUE);
                  g2d.fillOval(
                      (int) rect.getCenterX() - ((int) rect.getWidth() / 2),
                      (int) rect.getCenterY() - ((int) rect.getHeight() / 2),
                      (int) rect.getWidth(),
                      (int) rect.getHeight());

                  g2d.setColor(Color.BLACK);
                  g2d.draw(shapes.get(i));
                }
                // lions
                ArrayList<Point2D> lions;
                lions = con.getOuterLions(x, y);
                for (int i = 0; i < lions.size(); i++) {
                  g2d.setColor(Color.GREEN);
                  g2d.fillOval((int) lions.get(i).getX(), (int) lions.get(i).getY(), 5, 5);

                  g2d.drawOval((int) lions.get(i).getX(), (int) lions.get(i).getY(), 5, 5);
                }

              } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
              }
            }

            @Override
            public Dimension getPreferredSize() {
              return new Dimension(505, 505);
            }
          };

      p.repaint();
      p.setBackground(Color.white);

      mainMap.add(p);

      mainMap.pack();
      mainMap.setVisible(true);

      // g2.draw(shapes.get(0));

    } catch (Exception e) {
      System.out.println("kao");
      e.printStackTrace();
    }
  }
示例#12
0
    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;
      boolean first = false;
      float next = 0;
      int phase = 0;

      float factor = 1;

      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);
            first = true;
            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;
              float angle = (float) Math.atan2(dy, dx);
              while (distance >= next) {
                float x = lastX + next * dx * r;
                float y = lastY + next * dy * r;
                float tx = amplitude * dy * r;
                float ty = amplitude * dx * 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;
            first = false;
            lastX = thisX;
            lastY = thisY;
            if (type == PathIterator.SEG_CLOSE) result.closePath();
            break;
        }
        it.next();
      }

      // return stroke.createStrokedShape( result );
      return result;
    }
示例#13
0
  public void iterateBpt() {
    if (items[0] == null || !(items[0].getItem() instanceof ItemBptBase)) {

      if (bluePrintBuilder != null) {
        bluePrintBuilder = null;
      }

      if (builderRobot != null) {
        builderRobot.setDead();
        builderRobot = null;
      }

      if (box.isInitialized()) {
        box.deleteLasers();
        box.reset();
      }

      if (currentPathIterator != null) {
        currentPathIterator = null;
      }

      return;
    }

    if (bluePrintBuilder == null || bluePrintBuilder.done) {
      if (path != null && path.size() > 1) {
        if (currentPathIterator == null) {
          Iterator<BlockIndex> it = path.iterator();
          BlockIndex start = it.next();
          currentPathIterator = new PathIterator(start, it);
        }

        if (bluePrintBuilder != null && builderRobot != null) {
          builderRobot.markEndOfBlueprint(bluePrintBuilder);
        }

        bluePrintBuilder = currentPathIterator.next();

        if (bluePrintBuilder != null) {
          box.deleteLasers();
          box.reset();
          box.initialize(bluePrintBuilder);
          box.createLasers(worldObj, LaserKind.Stripes);
        }

        if (builderRobot != null) {
          builderRobot.setBox(box);
        }

        if (bluePrintBuilder == null) {
          currentPathIterator = currentPathIterator.iterate();
        }

        if (currentPathIterator == null) {
          done = true;
        }
      } else {
        if (bluePrintBuilder != null && bluePrintBuilder.done) {
          if (builderRobot != null) {
            builderRobot.markEndOfBlueprint(bluePrintBuilder);
          }

          done = true;
          bluePrintBuilder = null;
        } else {
          bluePrintBuilder =
              instanciateBluePrint(
                  xCoord,
                  yCoord,
                  zCoord,
                  Orientations.values()[worldObj.getBlockMetadata(xCoord, yCoord, zCoord)]
                      .reverse());

          if (bluePrintBuilder != null) {
            box.initialize(bluePrintBuilder);
            box.createLasers(worldObj, LaserKind.Stripes);
          }
        }
      }
    }
  }
示例#14
0
  public void iterateBpt(boolean forceIterate) {
    if (getStackInSlot(0) == null || !(getStackInSlot(0).getItem() instanceof ItemBlueprint)) {
      if (box.isInitialized()) {
        if (currentBuilder != null) {
          currentBuilder = null;
        }

        if (box.isInitialized()) {
          box.reset();
        }

        if (currentPathIterator != null) {
          currentPathIterator = null;
        }

        updateRequirements();

        sendNetworkUpdate();

        return;
      }
    }

    if (currentBuilder == null || (currentBuilder.isDone(this) || forceIterate)) {
      if (path != null && path.size() > 1) {
        if (currentPathIterator == null) {
          Iterator<BlockIndex> it = path.iterator();
          BlockIndex start = it.next();
          currentPathIterator =
              new PathIterator(
                  start,
                  it,
                  ForgeDirection.values()[worldObj.getBlockMetadata(xCoord, yCoord, zCoord)]
                      .getOpposite());
        }

        if (currentBuilder != null && currentBuilder.isDone(this)) {
          currentBuilder.postProcessing(worldObj);
        }

        currentBuilder = currentPathIterator.next();

        if (currentBuilder != null) {
          box.reset();
          box.initialize(currentBuilder);
          sendNetworkUpdate();
        }

        if (currentBuilder == null) {
          currentPathIterator = currentPathIterator.iterate();
        }

        if (currentPathIterator == null) {
          done = true;
        } else {
          done = false;
        }

        updateRequirements();
      } else {
        if (currentBuilder != null && currentBuilder.isDone(this)) {
          currentBuilder.postProcessing(worldObj);
          currentBuilder = recursiveBuilder.nextBuilder();

          updateRequirements();
        } else {
          BlueprintBase bpt = instanciateBlueprint();

          if (bpt != null) {
            recursiveBuilder =
                new RecursiveBlueprintBuilder(
                    bpt,
                    worldObj,
                    xCoord,
                    yCoord,
                    zCoord,
                    ForgeDirection.values()[worldObj.getBlockMetadata(xCoord, yCoord, zCoord)]
                        .getOpposite());

            currentBuilder = recursiveBuilder.nextBuilder();

            updateRequirements();
          }
        }

        if (currentBuilder == null) {
          done = true;
        } else {
          box.initialize(currentBuilder);
          sendNetworkUpdate();
          done = false;
        }
      }
    }

    if (done && getStackInSlot(0) != null) {
      boolean dropBlueprint = true;
      for (int i = 1; i < getSizeInventory(); ++i) {
        if (getStackInSlot(i) == null) {
          setInventorySlotContents(i, getStackInSlot(0));
          dropBlueprint = false;
          break;
        }
      }
      if (dropBlueprint) {
        InvUtils.dropItems(getWorldObj(), getStackInSlot(0), xCoord, yCoord, zCoord);
      }

      setInventorySlotContents(0, null);
      box.reset();
    }
  }