Compositing is the combining of elements from separate sources into single images : AlphaComposite « 2D Graphics « Java Tutorial






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

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

public class CompositingDST extends JPanel {
  public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.DST, 0.5f);

    BufferedImage buffImg = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB);
    Graphics2D gbi = buffImg.createGraphics();

    gbi.setPaint(Color.red);
    gbi.fillRect(0, 0, 40, 40);
    gbi.setComposite(ac);

    gbi.setPaint(Color.green);
    gbi.fillRect(5, 5, 40, 40);

    g2d.drawImage(buffImg, 20, 20, null);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("Composition");
    frame.add(new CompositingDST());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 120);
    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.