Hans-Peter Stricker
Hans-Peter Stricker

Reputation: 363

Hand-drawn graphics with JAVA

Is there a way (a library) with which one can give the (contour) lines of AWT-2D-objects a "hand-drawn" look (somehow shaky: not exactly following the "official" path, not having perfect line contours)

enter image description here

(The official path should be followed almost perfectly, with just some "random" noise. The line contours should be almost perfect, with just some "random" noise.)

enter image description here

Upvotes: 0

Views: 323

Answers (2)

lbalazscs
lbalazscs

Reputation: 17784

Use the SloppyStroke from here: 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);
  }
}

Upvotes: 2

Yuriy
Yuriy

Reputation: 1394

Once I tried to do something similar with jhlabs. Please take a look at the WobbleStroke sample at: http://www.jhlabs.com/java/java2d/strokes/

In general - you have to implement the java.awt.Stroke class. So better search for appropriate implementations.

Here is a good sample of a brush stroke as well: http://javagraphics.blogspot.com/2007/04/strokes-brush-stroke.html

Upvotes: 1

Related Questions