apply Alpha to BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

apply Alpha to BufferedImage

Demo Code


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

import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage applyAlpha(BufferedImage image, double alpha) {
        int width = image.getWidth();
        int height = image.getHeight();
        BufferedImage newImage = new BufferedImage(width, height,
                image.getType());//w  w w. jav a 2  s.  c o  m
        AlphaComposite ac = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, (float) alpha);
        Graphics2D g2d = (Graphics2D) newImage.getGraphics();
        g2d.setComposite(ac);
        g2d.drawImage(image, 0, 0, null);

        return newImage;
    }
}

Related Tutorials