Two Laptop Bag
The moose likes Game Development and the fly likes Color transparancy? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Color transparancy?" Watch "Color transparancy?" New topic

Color transparancy?

Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Hi evrybody, and a happy new year!

So, right to the subject, im working on my first game project (the first im going to finnish all the way), and i want to use some pictures that i have drwan i paint. When i load the picture, theres a white aquare around. I tried to remove this square with this code:

But as i want to set the alpha value to zero, making the white colors invisible, they just turn black???
All the code for the class looks like this:

Can anyone tell me what im doing wrong?

Note: some the code might look unnecessary, but that is just desperate trys to make it work. (by the way "Tag" and "taget" are danish for "roof" and "the roof" jsut to avoid confussion)
Lance Colton
Ranch Hand

Joined: Nov 25, 2010
Posts: 64

I would just open the images in gimp, remove the white and save them as png's, It will save you code and resources.

open the file, hit Colors -> Color to alpha, and select white then save it with a .png extension.

~tak - tak, - tak11, - tak11
Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 119
Hi Hans -

This CAN be done in Java and learning the way opens the door to lots of other neat transformations. The way I learned to edit images was from "Core Java II" by Horstmann, in the "Advanced AWT" chapter.

Since you want to edit the alpha values, construct the BufferedImage of the type TYPE_INT_ARGB. Then make a WritableRaster. With that you can inspect and set the individual pixels.

Then you can use raster.getPixel(i,j, pixel) and raster.setPixel(i,j,pixel) where pixel is an int[4], with
pixel[0] = red, pixel[1]=green, pixel[2]=blue, and pixel[3]=alpha (with values from 0-255).

You only need to alter the alpha, nothing is gained by setting the others to black as far as I can see. If the alpha is 0, it's invisible anyway.

Looking at some code of mine, where I read an image file, I am noticing that I am loading the data into a temporary array for use in the construction of an entirely new BufferedImage. So, there may be a way to work directly on the file you have loaded in, but I don't seem to have done exactly what you are trying.

Hope this helps! I am also working on finishing my first game. Good luck to us both!
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Thanks for thge help, the transparancy now worls just the way it should
I agree. Here's the link:
subject: Color transparancy?
It's not a secret anymore!