Adds an overlay image to the destination image. - Java 2D Graphics

Java examples for 2D Graphics:Image

Description

Adds an overlay image to the destination image.

Demo Code


//package com.java2s;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class Main {
    /**//from   ww w. j av a  2s  . c o m
     * Adds an overlay image to the destination image. The source is composited 
     * over the destination (Porter-Duff Source Over Destination rule).
     *   Cs   a color component of the source pixel in premultiplied form 
     *   Fs = 1 and Fd = (1-As), thus:
     *   
     *  Ar = As + Ad*(1-As)
     *  Cr = Cs + Cd*(1-As)
     *   
     * @param dst Destination image which will be changed
     * @param src Source image.
     * @param alpha the constant alpha to multiply with the alpha of the source
     */
    public static void addOverlay(BufferedImage dst, BufferedImage src,
            float alpha) {
        Graphics2D g2d = dst.createGraphics();

        AlphaComposite ac = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, alpha);
        g2d.setComposite(ac);
        g2d.drawImage(src, 0, 0, null);
        g2d.dispose();
    }
}

Related Tutorials