making the color dissapear.
hello, i'm new to this forum, i hope to find some answers :)
my problem is that i have a .png file, and i read the content to a BufferedImage object, then i move that into a
pixel array, the file is a picture of characters that need to be in the game, the problem is that when i paint them,
i can see their background from the file and not the background of the game, i am looking for help as to how to
make the background transparent. i have looked over the web for solutions, but things just won't work,
i have managed to set the Alpha number of the specific color to zero. but still i see in the game panel the gray
color of java's usual background, here is the code:
// target - the image, rgba - the color to kill.
public void cleanImage(BufferedImage target,int r,int g,int b,int a)
int col = ((a << 24) | (r << 16) | (g << 8) | b);
int trans = ((0 << 24) | (r << 16) | (g << 8) | b);
int  pixels = ((DataBufferInt)target.getRaster().getDataBuffer()).getData();
ColorModel cm = target.getColorModel();
SampleModel sm = target.getRaster().getSampleModel();
for (int i = 0;i<pixels.length;i++)
if (pixels[i] == col) pixels[i] = trans;//-1118482;
DataBuffer db = new DataBufferInt(pixels,target.getHeight()*target.getWidth());
WritableRaster wr = Raster.createWritableRaster(sm,db,null);
target = new BufferedImage(cm, wr, false, null);
Re: making the color dissapear.
Does the image itself have a transparent background? PNG-8 and PNG-32 are the only PNGs that allow transparent backgrounds. If your image is a PNG-24, the first thing you'll need to do is probably save it as one of the others. I'm not sure if Java changes the transparent backgrounds when it imports it.
After that, I'm not too sure how to get java to make a background transparent. I just wanted to tell you something that you would want to be mindful of.