make Color Transparent - Java 2D Graphics

Java examples for 2D Graphics:Color Alpha

Description

make Color Transparent

Demo Code


//package com.java2s;

import java.awt.image.*;
import java.awt.*;

public class Main {
    public static BufferedImage makeColorTransparent(BufferedImage ref,
            Color color) {/*from   w w  w.j a v a2 s .  c  o m*/
        BufferedImage dimg = new BufferedImage(ref.getWidth(),
                ref.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = dimg.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.drawImage(ref, null, 0, 0);
        g.dispose();
        for (int i = 0; i < dimg.getHeight(); i++) {
            for (int j = 0; j < dimg.getWidth(); j++) {
                if (dimg.getRGB(j, i) == color.getRGB()) {
                    dimg.setRGB(j, i, 0x8F1C1C);
                }
            }
        }
        return dimg;
    }
}

Related Tutorials