【问题标题】:Hand-drawn graphics with JAVA用JAVA手绘图形
【发布时间】:2015-01-23 14:53:42
【问题描述】:

有没有一种方法(库)可以使 AWT-2D 对象的(轮廓)线具有“手绘”外观(有点不稳定:不完全遵循“官方”路径,没有完美线轮廓)

(应该几乎完美地遵循官方路径,只有一些“随机”噪音。线条轮廓应该几乎完美,只有一些“随机”噪音。)

【问题讨论】:

    标签: java graphics awt java-2d


    【解决方案1】:

    从这里使用 SloppyStroke:http://www.java2s.com/Code/Java/2D-Graphics-GUI/CustomStrokes.htm

    /**
     * This Stroke implementation randomly perturbs the line and curve segments that
     * make up a Shape, and then strokes that perturbed shape. It uses PathIterator
     * to loop through the Shape and GeneralPath to build up the modified shape.
     * Finally, it uses a BasicStroke to stroke the modified shape. The result is a
     * "sloppy" looking shape.
     */
    
    class SloppyStroke implements Stroke {
      BasicStroke stroke;
    
      float sloppiness;
    
      public SloppyStroke(float width, float sloppiness) {
        this.stroke = new BasicStroke(width); // Used to stroke modified shape
        this.sloppiness = sloppiness; // How sloppy should we be?
      }
    
      public Shape createStrokedShape(Shape shape) {
        GeneralPath newshape = new GeneralPath(); // Start with an empty shape
    
        // Iterate through the specified shape, perturb its coordinates, and
        // use them to build up the new shape.
        float[] coords = new float[6];
        for (PathIterator i = shape.getPathIterator(null); !i.isDone(); i
            .next()) {
          int type = i.currentSegment(coords);
          switch (type) {
          case PathIterator.SEG_MOVETO:
            perturb(coords, 2);
            newshape.moveTo(coords[0], coords[1]);
            break;
          case PathIterator.SEG_LINETO:
            perturb(coords, 2);
            newshape.lineTo(coords[0], coords[1]);
            break;
          case PathIterator.SEG_QUADTO:
            perturb(coords, 4);
            newshape.quadTo(coords[0], coords[1], coords[2], coords[3]);
            break;
          case PathIterator.SEG_CUBICTO:
            perturb(coords, 6);
            newshape.curveTo(coords[0], coords[1], coords[2], coords[3],
                coords[4], coords[5]);
            break;
          case PathIterator.SEG_CLOSE:
            newshape.closePath();
            break;
          }
        }
    
        // Finally, stroke the perturbed shape and return the result
        return stroke.createStrokedShape(newshape);
      }
    
      // Randomly modify the specified number of coordinates, by an amount
      // specified by the sloppiness field.
      void perturb(float[] coords, int numCoords) {
        for (int i = 0; i < numCoords; i++)
          coords[i] += (float) ((Math.random() * 2 - 1.0) * sloppiness);
      }
    }
    

    【讨论】:

      【解决方案2】:

      有一次我尝试用 jhlabs 做类似的事情。请查看 WobbleStroke 示例:http://www.jhlabs.com/java/java2d/strokes/

      一般来说 - 你必须实现 java.awt.Stroke 类。所以更好地寻找合适的实现。

      这里也是一个很好的笔触示例: http://javagraphics.blogspot.com/2007/04/strokes-brush-stroke.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-06
        • 2012-11-07
        相关资源
        最近更新 更多