使用JAVA手绘图形

时间:2014-11-25 14:38:19

标签: java graphics awt java-2d

是否有一种方法(图书馆),人们可以用它来给出AWT-2D对象的(轮廓)线a"手绘"看起来(不知何故摇摇欲坠:不完全遵循"官方"路径,没有完美的线条轮廓)

enter image description here

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

enter image description here

2 个答案:

答案 0 :(得分:1)

一旦我尝试用jhlabs做类似的事情。请在http://www.jhlabs.com/java/java2d/strokes/

查看WobbleStroke示例

通常 - 您必须实现java.awt.Stroke类。所以更好地搜索适当的实现。

以下是画笔笔划的一个很好的示例: http://javagraphics.blogspot.com/2007/04/strokes-brush-stroke.html

答案 1 :(得分: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);
  }
}