PDA

Click to See Complete Forum and Search --> : How to draw transparent image in java???


vruben
Jul 23rd, 2003, 02:19 AM
Hello,
Does anyone know how to draw a transparent image in Java. For example I have an image and want to set its black color transparent. I mean that I want black color not to be drawn.

CornedBee
Jul 23rd, 2003, 12:56 PM
I fear color keying is not included in the library.
Unless you find something on the net (try searching for Java, Graphics and color keying or some such) you'll have to write a class that implements java.awt.Composite and that does the color keying for you.

CornedBee
Jul 23rd, 2003, 12:57 PM
Alternatively you can write a java.awt.image.ImageFilter that converts the image by setting the alpha for the black areas to 0. However I think this is harder.

Phenix
Aug 2nd, 2003, 07:25 PM
I thought it would be as simple as using the alpha in a "new Color" because it does have such a constructor with Alpha, Red, Green, Blue, but I never figured it out.

If you find out (and again, I think it is as simple as defining a new Color with alpha), please post your solution here.

VB_GOD
Aug 17th, 2003, 07:32 PM
Here is code for a instantiable class that will help you draw transparent images/shapes onto an offscreen buffer which you can render and return an image. (See file attachment)

How to use:
Instantiate it, giving it width and height properties of the final image you wish to create (and optional background colour). If you have loaded a transparent GIF from file and have stored it as an Image type, you can use addImage function and it will render with its transparent properties intact. You can also use addAlphaImage function to control its alpha compositing properties (blending images over each other with translucency). The alpha parameter is a double value between 0.0 and 1.0 (0.0 being completely invisible, 1.0 being completely opaque).

I wrote this class myself, it's fast and efficient, I have made many 2D java games with it. Hope it'll help you out.

ps
Don't forget to set the attachment's extension back to ".java"! :D