# [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.

## 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 on other sites
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

##### Share on other sites
Yeah that's it thanks

##### 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 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.

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).

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 69
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632967
• Total Posts
3009570
• ### Who's Online (See full list)

There are no registered users currently online

×