CONTENTS | PREV | NEXT Java 2D API


2.6 Compositing Images

"Managing Complex Drawing Operations" on page 6 discussed basic compositing and introduced the AlphaComposite class, an implementation of the Composite interface. This class supports a number of different composition styles. Instances of this class embody a composition rule that describes how to blend a new color with an existing one. The alpha values for rendering are derived from a Color, Paint, or Image object, combined with pixel coverage information from a rasterized path (when antialiased rendering is being performed).


2.6.1 Managing Transparency

One of the most commonly used compositing rules in the AlphaComposite class is SRC_OVER. When AlphaComposite.SRC_OVER is applied, it indicates that the new color (the source color) should be blended over the existing color (the destination color).

AlphaComposite objects can be created with an extra alpha value to increase the transparency of objects being drawn. This extra alpha value is combined with the graphics objects' alpha values. The combined alpha value indicates, as a percentage, how much of the existing color should show through the new color. It is a measure of the transparency of the new color. Opaque colors (alpha=1.0) don't allow any existing color show through, while transparent colors (alpha=0.0) let all of the existing color show through.

The example shown in "Managing Complex Drawing Operations" on page 6 uses an AlphaComposite object to make one of the rectangles partially transparent. The following example shows how you could use a second AlphaComposite object so that both rectangles are partially transparent. To get the results shown in Figure 2-4, draw the text using the default composite object so that it is totally opaque. Then, use an AlphaComposite.SRC_OVER object with an extra alpha of 0.5 to increase the transparency of the two overlapping rectangles:

public void paint(Graphics g){
  Graphics2D g2d = (Graphics2D) g;
  Font myfont = new Font ("Helvetica-Bold", Font.PLAIN, 40);
  StyledString myString1 = new StyledString("Compositing",
    myfont);
g2d.drawString(myString1, 270f, 280f);
StyledString myString2 = new StyledString("is Fun!", myfont);
g2d.drawString(myString2, 320f, 340f);
AlphaComposite comp =   
  AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
g2d.setComposite(comp);
g2d.setColor(Color.red);



GeneralPath path = new GeneralPath(GeneralPath.EVEN_ODD);
path.moveTo(0.0f, 0.0f); // lower left corner
path.lineTo(200.0f, 0.0f); // lower right corner
path.lineTo(200.0f, -100.0f); // upper right corner
path.lineTo(0.0f, -100.0f); // upper left corner
path.closePath(); // close the rectangle



AffineTransform at = new AffineTransform();
at.setToTranslation(300.0, 400.0);
g2d.transform(at);



g2d.fill(path);
g2d.setColor(Color.blue); // define the color



// Rotate about the origin -45 deg in radians
at.setToRotation(-Math.PI/4.0));   
g2d.transform(at);
g2d.fill(path);
}

Figure 2-4 Compositing


2.6.2 Defining Custom Composition Rules

You can create an entirely new type of compositing operation by implementing the Composite and CompositeContext interfaces. A Composite object provides a CompositeContext object that actually holds the state and performs the compositing work. Multiple CompositeContext objects can be created from one Composite object to maintain the separate states in a multi-threaded environment.



CONTENTS | PREV | NEXT
Copyright © 1997 Sun Microsystems, Inc. All Rights Reserved.