[java] How to make an image transparent?

Started by
3 comments, last by mako_5 18 years, 10 months ago
Hi, This topic was asked about a month ago, and I was rellying on finding it here once I needed it, but now that I need it I see the old threads got deleted [depressed] So could someone explain to me again how to make an image transparent? I remember that I need to get an array of int from the Raster, and then I need to go through it and if some pixel is, for example, white, then I set array = 0 and then it'll be transparent. I tried like this:

gc - GraphicsConfiguration
offscreen,picture - BufferedImage

offscreen = gc.createCompatibleImage(500,500,Transparency.TRANSLUCENT);

for (int y=0; y<picture.getHeight(); y++)
{
  for (int x=0; x<picture.getHeight(); x++
  {
     int pixel = picture.getRGB(x,y);
     if (new Color(pixel) == Color.WHITE)
     {
       picture.setRGB(x,y,0);
     }
  }
}
This turns the white area into black [oh]
offscreen.getGraphics().drawImage(10,20,picture,null);
Advertisement
Somehow, it is setting the RGB { 0, 0, 0 } value (black) to the pixel.

And maybe this is the thread you're looking for?

Son Of Cain
a.k.a javabeats at yahoo.ca
Yeah that's it thanks

You can also use a .gif for transparent images. I use transparent .gifs in my Java code. Just as fast as non-transparent images.

You can use .png for translucent images. Make the edges translucent for an anti-aliasing effect.

Stats:
Opaque: 5550.599 images/sec
Transparent: 5478.6953 images/sec
Translucent: 85.2197 images/sec
Translucent (Anti-Aliased): 113.18243 images/sec

Stats from "Developing Games in Java by David Brackeen"
Thanks to whoever referred me to that book!

-Jebediah
One suggestion is that when you are working with tranparency in images to use translucency only when you have to. Using simple transparent images can speed up rendering time for the game rather than a translucent image for the same thing.

offscreen = gc.createCompatibleImage(500,500,Transparency.BITMASK);

rather than

offscreen = gc.createCompatibleImage(500,500,Transparency.TRANSLUCENT);


Translucency is cool to use however if you need it for a certain effect, but it can kill your frame rate if you use it too much (like I did once myself).

This topic is closed to new replies.

Advertisement