Java Reference
In-Depth Information
Example 11•15: CustomStrokes.java (continued)
float[] coords = new float[6];
for(PathIterator i=shape.getPathIterator(null); !i.isDone();i.next()) {
int type = i.currentSegment(coords);
Shape s = null, s2 = null, s3 = null;
switch(type) {
case PathIterator.SEG_CUBICTO:
markPoint(strokedShape, coords[4], coords[5]); // falls through
case PathIterator.SEG_QUADTO:
markPoint(strokedShape, coords[2], coords[3]); // falls through
case PathIterator.SEG_MOVETO:
case PathIterator.SEG_LINETO:
markPoint(strokedShape, coords[0], coords[1]); // falls through
case PathIterator.SEG_CLOSE:
break;
}
}
return strokedShape;
}
/** Add a small square centered at (x,y) to the specified path */
void markPoint(GeneralPath path, float x, float y) {
path.moveTo(x-radius, y-radius); // Begin a new sub-path
path.lineTo(x+radius, y-radius); // Add a line segment to it
path.lineTo(x+radius, y+radius); // Add a second line segment
path.lineTo(x-radius, y+radius); // And a third
path.closePath();
// Go back to last moveTo position
}
}
/**
* 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;
Search WWH ::




Custom Search