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.
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.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
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.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
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.
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"!