Java Reference
In-Depth Information
Example 11•15: CustomStrokes.java (continued)
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);
}
}
Custom Paint
Figure 11-8 showed a variety of shape-filling techniques; it included a large letter
A filled with a complex pattern defined by the GenericPaint class. Example 11-16
shows the implementation of this class. You may want to take another look at
Example 11-10 to see how the GenericPaint class is used, before you dive into
the code listed here.
The GenericPaint class itself is pretty simple: it defines both the abstract color
computation methods that subclasses implement and a createContext() method
that returns a PaintContext . The implementation of PaintContext does all the
hard work. This is pretty low-level stuff, so don't be dismayed if you don't under-
stand everything. The code should at least give you a basic idea of how painting
works in Java 2D.
Example 11•16: GenericPaint.java
package com.davidflanagan.examples.graphics;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
/**
* This is an abstract Paint implementation that computes the color of each
Search WWH ::




Custom Search