Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Color transparancy?

 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
Android Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Phil Freihofner
Ranch Hand
Posts: 121
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for thge help, the transparancy now worls just the way it should
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic