Composite Demo : AlphaComposite « 2D Graphics « Java Tutorial






import java.awt.AlphaComposite;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class MainClass extends JFrame {
  public static void main(String args[]) {
    MainClass app = new MainClass();
  }

  public MainClass() {
    super("Draw2D");
    add("Center", new MyCanvas());

    setSize(400, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  }

  class MyCanvas extends Canvas {
    public void paint(Graphics graphics) {
      Graphics2D g = (Graphics2D) graphics;
      float alpha = 0.5f;
      AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
      g.setComposite(composite);
      Toolkit toolkit = Toolkit.getDefaultToolkit();
      Image image0 = toolkit.getImage("image0.gif");
      Image image1 = toolkit.getImage("image1.gif");
      g.drawImage(image0, 200, 100, this);
      g.drawImage(image1, 100, 150, this);
    }
  }
}








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.