Java Reference
In-Depth Information
Example 11•16: GenericPaint.java (continued)
* point to be painted by passing the coordinates of the point to the calling
* the abstract methods computeRed(), computeGreen(), computeBlue() and
* computeAlpha(). Subclasses must implement these three methods to perform
* whatever type of painting is desired. Note that while this class provides
* great flexibility, it is not very efficient.
**/
public abstract class GenericPaint implements Paint {
/** This is the main Paint method; all it does is return a PaintContext */
public PaintContext createContext(ColorModel cm,
Rectangle deviceBounds,
Rectangle2D userBounds,
AffineTransform xform,
RenderingHints hints) {
return new GenericPaintContext(xform);
}
/** This paint class allows translucent painting */
public int getTransparency() { return TRANSLUCENT; }
/**
* These three methods return the red, green, blue, and alpha values of
* the pixel at appear at the specified user-space coordinates. The return
* value of each method should be between 0 and 255.
**/
public abstract int computeRed(double x, double y);
public abstract int computeGreen(double x, double y);
public abstract int computeBlue(double x, double y);
public abstract int computeAlpha(double x, double y);
/**
* The PaintContext class does all the work of painting
**/
class GenericPaintContext implements PaintContext {
ColorModel model; // The color model
Point2D origin, unitVectorX, unitVectorY; // For device-to-user xform
public GenericPaintContext(AffineTransform userToDevice) {
// Our color model packs ARGB values into a single int
model = new DirectColorModel(32, 0x00ff0000,0x0000ff00,
0x000000ff, 0xff000000);
// The specified transform converts user to device pixels
// We need to figure out the reverse transformation, so we
// can compute the user space coordinates of each device pixel
try {
AffineTransform deviceToUser = userToDevice.createInverse();
origin = deviceToUser.transform(new Point(0,0), null);
unitVectorX = deviceToUser.deltaTransform(new Point(1,0),null);
unitVectorY = deviceToUser.deltaTransform(new Point(0,1),null);
}
catch (NoninvertibleTransformException e) {
// If we can't invert the transform, just use device space
origin = new Point(0,0);
unitVectorX = new Point(1,0);
unitVectorY = new Point(0, 1);
}
}
/** Return the color model used by this Paint implementation */
Search WWH ::




Custom Search