Jump to content
  • Advertisement
Sign in to follow this  
gcsaba2

[java] How to make an image transparent?

This topic is 4886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 = 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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!