Sign in to follow this  
gcsaba2

[java] How to make an image transparent?

Recommended Posts

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[i] = 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);

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this