show Colors on BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Color

Description

show Colors on BufferedImage

Demo Code


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

public class Main {
    public static void showColors(BufferedImage image, boolean alpha,
            boolean red, boolean green, boolean blue) {
        int a, r, g, b, rgb;
        for (int y = 0; y < image.getHeight(); ++y) {
            for (int x = 0; x < image.getWidth(); ++x) {
                rgb = image.getRGB(x, y);

                if (alpha) {
                    a = ((rgb >> 24) & 0xff);
                } else {
                    a = 255;/* ww w. j  av  a  2 s .c  om*/
                }
                if (red) {
                    r = ((rgb >> 16) & 0xff);
                } else {
                    r = 0;
                }
                if (green) {
                    g = ((rgb >> 8) & 0xff);
                } else {
                    g = 0;
                }
                if (blue) {
                    b = (rgb & 0xff);
                } else {
                    b = 0;
                }

                rgb = (a << 24) | (r << 16) | (g << 8) | (b << 0);
                image.setRGB(x, y, rgb);
            }
        }
    }
}

Related Tutorials