set Image Alpha - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Effect

Description

set Image Alpha

Demo Code


//package com.java2s;

import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage setImageAlpha(BufferedImage img, int alpha) {
        BufferedImage ris;//from w w  w.j  a  va 2  s  .  com

        int width = img.getWidth();

        int[] imgData = new int[width];

        for (int y = 0; y < img.getHeight(); y++) {
            img.getRGB(0, y, width, 1, imgData, 0, 1);

            for (int x = 0; x < width; x++) {
                int color = imgData[x];
                color &= 0x00FFFFFF;
                color |= alpha << 24;
                imgData[x] = color;
            }

            img.setRGB(0, y, width, 1, imgData, 0, 1);
        }

        return img;
    }

    public static int getRGB(int r, int g, int b) {
        int ris = 0;

        ris += (r << 16) & 0x00ff0000;
        ris += (g << 8) & 0x0000ff00;
        ris += (b) & 0x000000ff;

        return ris;
    }
}

Related Tutorials