0 to 1.0. For example, using an Alpha-Composite with an alpha of .5 will cause everything to be rendered at half its normal opacity.
Figure 5.8 shows how the additional alpha parameter affects the eight compositing rules. The light gray rectangle is opaque against a transparent drawing surface, just as in Figure 5.7. A dark gray rectangle is then rendered using a specific compositing rule and an additional alpha parameter of 0.8.
Figure 5.8. Compositing rules with an alpha parameter of 0.8
The following example shows how to use an AlphaComposite instance with the additional alpha parameter. First, a red rectangle is drawn, using the default compositing rule. Then an AlphaComposite with an alpha multiplier of 0.4 is used to render some blue text. The parts of the text that are over the rectangle are a shade of purple. The parts of the text outside the rectangle are a pale blue color. This is because the blue of the source (the text) is combined with the white background. Figure 15.3 shows this example in action.
import java.awt.*;
import java.awt.geom.*;
public class TransparentText {
public static void main(String[] args) {
ApplicationFrame f = new ApplicationFrame("TransparentText v1.0") {
page 87
Java 2D Graphics
private int mNumberOfLines = 25;
private Color[] mColors = { Color.red, Color.green, Color.blue };
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
// Set the rendering quality.
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Paint a red rectangle.
Rectangle2D r = new Rectangle2D.Double(50, 50, 150, 100);
g2.setPaint(Color.red);
g2.fill(r);
// Set a composite with transparency.
Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
.4f);
g2.setComposite(c);
// Draw some blue text.
g2.setPaint(Color.blue);
g2.setFont(new Font("Times New Roman", Font.PLAIN, 72));
g2.drawString("Composite", 25, 130);
}
};
f.setSize(400, 200);
f.center();
f.setVisible(true);
}
}
The results of some of the Porter-Duff compositing rules are often confusing, even for very simple operations like rendering two rectangles. One of the complicating factors is that the results may vary based on whether you do your rendering in an offscreen image or directly on the screen. The reason for this has to do with the alpha values of the drawing surface. Drawing surfaces that are on the screen always have an alpha value of 1.0 everywhere. The pixels of an offscreen drawing surface, however, can take on any alpha value.
For example, suppose a red rectangle is rendered on the drawing surface using the SrcOver rule.
Then a blue rectangle is rendered overlapping the red rectangle using the SrcOut rule.
When the rendering is done directly to the screen, the drawing surface has an alpha of 1.0
everywhere. When the red rectangle is rendered, the pixels in the rectangle are colored red without affecting the alpha of the drawing surface. When the blue rectangle is rendered using the SrcOut rule, neither the source color nor the destination color is used. (Look back at Table 5-1: it shows that none of the destination color is used, by definition, and that 1 - d of the source color is used.
But d = 1.0 everywhere for this onscreen drawing surface, so none of the source color is used either.) The net result is that the area of the blue rectangle is cleared of either color, which sets it to black. Figure C-4 shows how this looks.
The results are quite different when this same rendering sequence is performed on an offscreen image, as shown in Figure 15.5. Assume that the offscreen drawing surface has been filled with a color such that all of its pixels' alpha values are 0.0. When the red rectangle is rendered, the pixels underneath the rectangle are changed to red with an alpha of 1.0. The rest of the drawing surface remains transparent (alpha is 0.0 everywhere else). When the blue rectangle is rendered, some pixels overlap the red rectangle and some don't. The ones that do not overlap are rendered blue, because the alpha of the destination is 0.0 there and all of the source color is used. In the region of overlap with the red rectangle, the alpha of the drawing surface is 1.0. None of the source color and none of the destination color is used; the area of overlap is cleared, which includes setting the alpha page 88
Java 2D Graphics