Java Reference
In-Depth Information
Example 11•12: CompositeEffects.java (continued)
g.translate(10, 10);
g.drawImage(cover, 0, 0, c);
g.drawString("SRC_OVER", 0, COVERHEIGHT+15);
// Draw the cover again, using AlphaComposite to make the opaque
// colors of the image 50% translucent
g.translate(COVERWIDTH+10, 0);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
0.5f));
g.drawImage(cover, 0, 0, c);
// Restore the pre-defined default Composite for the screen, so
// opaque colors stay opaque.
g.setComposite(AlphaComposite.SrcOver);
// Label the effect
g.drawString("SRC_OVER, 50%", 0, COVERHEIGHT+15);
// Now get an offscreen image to work with. In order to achieve
// certain compositing effects, the drawing surface must support
// transparency. Onscreen drawing surfaces cannot, so we have to do the
// compositing in an offscreen image that is specially created to have
// an "alpha channel", then copy the final result to the screen.
BufferedImage offscreen =
new BufferedImage(COVERWIDTH, COVERHEIGHT,
BufferedImage.TYPE_INT_ARGB);
// First, fill the image with a color gradient background that varies
// left-to-right from opaque to transparent yellow
Graphics2D osg = offscreen.createGraphics();
osg.setPaint(new GradientPaint(0, 0, Color.yellow,
COVERWIDTH, 0,
new Color(255, 255, 0, 0)));
osg.fillRect(0,0, COVERWIDTH, COVERHEIGHT);
// Now copy the cover image on top of this, but use the DstOver rule
// which draws it "underneath" the existing pixels, and allows the
// image to show depending on the transparency of those pixels.
osg.setComposite(AlphaComposite.DstOver);
osg.drawImage(cover, 0, 0, c);
// And display this composited image on the screen. Note that the
// image is opaque and that none of the screen background shows through
g.translate(COVERWIDTH+10, 0);
g.drawImage(offscreen, 0, 0, c);
g.drawString("DST_OVER", 0, COVERHEIGHT+15);
// Now start over and do a new effect with the off-screen image.
// First, fill the offscreen image with a new color gradient. We
// don't care about the colors themselves; we just want the
// translucency of the background to vary. We use opaque black to
// transparent black. Note that since we've already used this offscreen
// image, we set the composite to Src, we can fill the image and
// ignore anything that is already there.
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);
Search WWH ::




Custom Search