buffered image with transparency
This question is actually two parts.
Hello I am writing a simple game in which I move a little graphics character across the screen. The character is a car, and the background image that the car moves across is a picture of some mountains. How do I set a transparent color for the car windows, and the rectangular region that surrounds the graphics image of the car?. I have done this in C++ but not in Java. So in other words a certain color needs to be designated as the transparent color. I need to know how to set the tranparent color using Java methods/attributes. Not through an image editor.
Also, the image is currently a gif, but I also need to ask what the best choice for a graphics image on the web is?. Should I use .gif, .jpg, .pcx. I am looking for something really portable. One of my concerns is that I once heard that Compuserve tried to patent gifs.
Also, I am not using Swing.
Thanks in advance!!! Val
You're maybe just searching for the AlphaComposite Class.
You'd create a AlphaComposite through the static getInstance(int rule), getInstance(int rule, float alpha) methods
alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25f);
then a call of getComposite() on your object should to the trick
refer to the api for info about rules available
hf,
dani
class Transparency {
public static Image makeColorTransparent
(Image im, final Color color) {
ImageFilter filter = new RGBImageFilter() {
// the color we are looking for... Alpha bits are set to opaque
public int markerRGB = color.getRGB() | 0xFF000000;
public final int filterRGB(int x, int y, int rgb) {
if ( ( rgb | 0xFF000000 ) == markerRGB ) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
}
else {
// nothing to do
return rgb;
}
}
};
ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
}
That should filter the image for you, also remember with class java.awt.image.BufferedImage, in order to use alpha-transparency you must create a BufferedImage of type ARGB or "Alpha-RGB" or something similar in order to read palette alpha entries.