Java Reference
In-Depth Information
Example 11•12: CompositeEffects.java (continued)
// Now set the compositing type to SrcIn, so colors come from the
// source, but translucency comes from the destination
osg.setComposite(AlphaComposite.SrcIn);
// Draw our loaded image into the off-screen image, compositing it.
osg.drawImage(cover, 0, 0, c);
// And then copy our off-screen image to the screen. Note that the
// image is translucent and some of the image shows through.
g.translate(COVERWIDTH+10, 0);
g.drawImage(offscreen, 0, 0, c);
g.drawString("SRC_IN", 0, COVERHEIGHT+15);
// If we do the same thing but use SrcOut, then the resulting image
// will have the inverted translucency values of the destination
osg.setComposite(AlphaComposite.Src);
osg.setPaint(new GradientPaint(0, 0, Color.black,
COVERWIDTH, COVERHEIGHT,
new Color(0, 0, 0, 0)));
osg.fillRect(0,0, COVERWIDTH, COVERHEIGHT);
osg.setComposite(AlphaComposite.SrcOut);
osg.drawImage(cover, 0, 0, c);
g.translate(COVERWIDTH+10, 0);
g.drawImage(offscreen, 0, 0, c);
g.drawString("SRC_OUT", 0, COVERHEIGHT+15);
// Here's a cool effect; it has nothing to do with compositing, but
// uses an arbitrary shape to clip the image. It uses Area to combine
// shapes into more complicated ones.
g.translate(COVERWIDTH+10, 0);
Shape savedClip = g.getClip(); // Save current clipping region
// Create a shape to use as the new clipping region.
// Begin with an ellipse
Area clip = new Area(new Ellipse2D.Float(0,0,COVERWIDTH,COVERHEIGHT));
// Intersect with a rectangle, truncating the ellipse.
clip.intersect(new Area(new Rectangle(5,5,
COVERWIDTH-10,COVERHEIGHT-10)));
// Then subtract an ellipse from the bottom of the truncated ellipse.
clip.subtract(new Area(new Ellipse2D.Float(COVERWIDTH/2-40,
COVERHEIGHT-20, 80, 40)));
// Use the resulting shape as the new clipping region
g.clip(clip);
// Then draw the image through this clipping region
g.drawImage(cover, 0, 0, c);
// Restore the old clipping region so we can label the effect
g.setClip(savedClip);
g.drawString("Clipping", 0, COVERHEIGHT+15);
}
}
Image Processing
Both Java 1.0 and 1.1 included a complex API for filtering images on the fly as
they were downloaded over a network connection. Although this API is still avail-
able in later versions of Java, it is not commonly used, nor is it demonstrated in
this topic. Java 2D defines a simpler API based on the BufferedImageOp interface
Search WWH ::




Custom Search