CompositingDST_ATOP.java Source code

Java tutorial

Introduction

Here is the source code for CompositingDST_ATOP.java

Source

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_ATOP extends JPanel {
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 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_ATOP());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 120);
        frame.setVisible(true);
    }
}