AlphaComposite Example
The AlphaComposite class defines constants representing different compositing rules.The SRC_OVER rule means that whatever we're drawing (the source) should be placed over what's already there (the destination).
The floating point parameter is an alpha multiplier. 0.5F means that what we're drawing should have half its normal opacity, and thereby allow the existing drawing to show through.
// AlphaComposite Example showing transparent graphic
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
class AlphaCompositeExample extends JApplet {
@Override public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
Rectangle2D r0 = new Rectangle2D.Double(130, 80, 40, 140);
g2.setPaint(Color.yellow);
g2.fill(r0);
Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F);
g2.setComposite(c);
Rectangle2D r1 = new Rectangle2D.Double(100, 100, 100, 100);
g2.setPaint(Color.red);
g2.fill(r1);
c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3F);
g2.setComposite(c);
g2.setColor(Color.blue);
g2.drawString("seeing through it with an AlphaComposite", 25, 150);
}
}
