Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

10.4 Transparent Drawing

Java 2D permits you to assign transparency (alpha) values to drawing operations so that the underlying graphics partially shows through when you draw shapes or images. You set a transparency by creating an AlphaComposite object and then passing the AlphaComposite object to the setComposite method of the Graphics2D object. You create an AlphaComposite by calling AlphaComposite.getInstance with a mixing rule designator and a transparency (or "alpha") value. For example,

float alpha = 0.75f;
int type = AlphaComposite.SRC_OVER; 
AlphaComposite composite = 
  AlphaComposite.getInstance(type, alpha);

The AlphaComposite API provides eight built-in mixing rules, but the one normally used for drawing with transparency settings is AlphaComposite.SRC_OVER, a source over destination mixing rule that places the source (shape) over the destination (background). A complete definition of the mixing rule was provided by T. Porter and T. Duff in "Compositing Digital Images," SIGGRAPH 84, pp. 253–259. Alpha values can range from 0.0f (completely transparent) to 1.0f (completely opaque).

Listing 10.7 demonstrates changing the transparency setting before drawing a red square that is partially overlapping a blue square. As shown in Figure 10–4, 11 opaque blue squares are drawn, equally spaced across the panel. Partially overlapping is a red square drawn with an initial alpha value of 0.0f at the far left. The red square is repeatedly drawn at new locations across the panel with alpha values that gradually increase by a step size of 0.1f until, finally, total opaqueness is reached at the far right with an alpha value of 1.0f.

Recall from Section 10.3 (Paint Styles) that the transparency (alpha) value of a color can be changed directly. Thus, for this example, the transparency of the red box could be directly set by a new color, as in:

private void drawSquares(Graphics2D g2d, float alpha) {
  g2d.setPaint(Color.blue);
  g2d.fill(blueSquare);
  Color color = new Color(1, 0, 0, alpha); //Red 
  g2d.setPaint(color);
  g2d.fill(redSquare);
}

Here, the assumption is that the original compositing rule is AlphaComposite.SRC_OVER, which is the default for the Graphics2D object. If the alpha value is set both through an AlphaComposite object and a Color object, the alpha values are multiplied to obtain the final transparency value.

Listing 10.7 TransparencyExample.java

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

/** An illustration of the use of AlphaComposite to make
 * partially transparent drawings.
 */
public class TransparencyExample extends JPanel {
 private static int gap=10, width=60, offset=20,
           deltaX=gap+width+offset;
 private Rectangle
  blueSquare = new Rectangle(gap+offset, gap+offset, width,
                width),
  redSquare = new Rectangle(gap, gap, width, width);

 private AlphaComposite makeComposite(float alpha) {
  int type = AlphaComposite.SRC_OVER;
  return(AlphaComposite.getInstance(type, alpha));
 }

 private void drawSquares(Graphics2D g2d, float alpha) {
  Composite originalComposite = g2d.getComposite();
  g2d.setPaint(Color.blue);
  g2d.fill(blueSquare);
  g2d.setComposite(makeComposite(alpha));
  g2d.setPaint(Color.red);
  g2d.fill(redSquare);
  g2d.setComposite(originalComposite);
 }

 public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2d = (Graphics2D)g;
  for(int i=0; i<11; i++) {
   drawSquares(g2d, i*0.1F);
   g2d.translate(deltaX, 0);
  }
 }

 public static void main(String[] args) {
  String title = "Transparency example: alpha of the top " +
          "(red) square ranges from 0.0 at the left " +
          "to 1.0 at the right. Bottom (blue) square " +
          "is opaque.";
  WindowUtilities.openInJFrame(new TransparencyExample(),
                 11*deltaX + 2*gap,
                 deltaX + 3*gap,
                 title, Color.lightGray);
 }
}

Figure 10–4 Changing the transparency setting can allow the background image behind the shape to show through.

  • + Share This
  • 🔖 Save To Your Account