Creates a faded image - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Create

Description

Creates a faded image

Demo Code

/**/*from   w  ww. j ava  2 s .  co  m*/
 * Copyright 1998-2008, CHISEL Group, University of Victoria, Victoria, BC, Canada.
 * All rights reserved.
 */
//package com.java2s;
import java.awt.Color;

import java.awt.Image;
import java.awt.Toolkit;

import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;

import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;

public class Main {
    /**
     * Creates a faded image
     */
    public static Image createFadedImage(Image i) {
        ImageFilter filter = new RGBImageFilter() {
            public int filterRGB(int x, int y, int rgb) {
                Color color = new Color(rgb);
                float alpha = 0.60f;
                //float[] hsb = new float[3];
                //Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
                //hsb[1] = f; // doesn't work so well with colors close to grey, because no colour to saturate
                //hsb[2] = 85f; // if brightness goes above 1.0 we get a different hue!?
                //Color newColor = Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
                Color newColor = new Color(color.getRed() / 255.0f,
                        color.getGreen() / 255.0f,
                        color.getBlue() / 255.0f, alpha);
                return newColor.getRGB();
            }
        };
        ImageProducer prod = new FilteredImageSource(i.getSource(), filter);
        Image fadedImage = Toolkit.getDefaultToolkit().createImage(prod);
        return fadedImage;
    }
}

Related Tutorials