Java Tutorial/2D Graphics/RGBImageFilter
Extend RGBImageFilter ColorFilter class
import java.awt.image.RGBImageFilter;
class ColorFilter extends RGBImageFilter {
boolean red, green, blue;
public ColorFilter(boolean r, boolean g, boolean b) {
red = r;
green = g;
blue = b;
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
int r = red ? 0 : ((rgb >> 16) & 0xff);
int g = green ? 0 : ((rgb >> 8) & 0xff);
int b = blue ? 0 : ((rgb >> 0) & 0xff);
return (rgb & 0xff000000) | (r << 16) | (g << 8) | (b << 0);
}
}
Extend RGBImageFilter to create AlphaFilter class
import java.awt.image.RGBImageFilter;
class AlphaFilter extends RGBImageFilter {
int alphaLevel;
public AlphaFilter(int alpha) {
alphaLevel = alpha;
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
int alpha = (rgb >> 24) & 0xff;
alpha = (alpha * alphaLevel) / 255;
return ((rgb & 0x00ffffff) | (alpha << 24));
}
}