Draw ten rectangles with different levels of transparency : AlphaComposite « 2D Graphics « Java Tutorial






import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TransparentRectangles extends JPanel {

  public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    for (int i = 1; i <= 10; i++) {
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, i * 0.1f));
      g2d.fillRect(50 * i, 20, 50, 50);
    }
  }

  public static void main(String[] args) {

    JFrame frame = new JFrame("Transparent Rectangles");
    frame.add(new TransparentRectangles());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 100);
    frame.setVisible(true);
  }
}








16.46.AlphaComposite
16.46.1.Composite Demo
16.46.2.Alpha Composite with BufferedImageAlpha Composite with BufferedImage
16.46.3.AlphaComposite.SRCAlphaComposite.SRC
16.46.4.AlphaComposite.SRC_OVERAlphaComposite.SRC_OVER
16.46.5.AlphaComposite.DST_OVERAlphaComposite.DST_OVER
16.46.6.AlphaComposite.SRC_INAlphaComposite.SRC_IN
16.46.7.AlphaComposite.DST_INAlphaComposite.DST_IN
16.46.8.AlphaComposite.SRC_OUTAlphaComposite.SRC_OUT
16.46.9.AlphaComposite.DST_OUTAlphaComposite.DST_OUT
16.46.10.Draw ten rectangles with different levels of transparency
16.46.11.Compositing is the combining of elements from separate sources into single images
16.46.12.Composition technique in this animation.