[java] alpha blending in java?

This topic is 3943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Alpha blending in java anyone? I can't find any helpful resources on the web. Searched a few forums as well. I basically just need to tell it to make a certain RGB value transparent.

Share on other sites
I few classes you might want to google are Graphics2D and the AlphaComposite class. Basically what you do is cast the regular Graphics object to a Graphics2D object and use that for rendering.

Graphics2D g2d = (Graphics2D)g;Composite composite = g2d.getComposite();// 0.5f is alphaAlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);				g2d.setComposite(alphaComposite);

Also, I'm not sure about this, but either the regular graphics class or only the graphics2d class will obay image transparencies. For example, if I set a certain color in my image as transparent, using g2d.drawImage() will blit the image while keeping the transparency.

Share on other sites
I prefer to just use the standart java.awt or awt.images if possible. Is that an option I have?

Share on other sites
Hmmm it seems Graphics2D is just a class within the awt, my mistake.

Thanks a bunch for pointing me in the right direction!

Share on other sites
The Graphics2D class is in java.awt so it will work with java.awt.images.

Share on other sites
A few concerns

Graphics2D g2d = (Graphics2D)g

I'm going to assume that this is a typecast (I haven't seen too much typecasting in Java, but I've been using 1.1-ish code so I'm guessing this is more common in 1.2). Just not sure why it is necessary. Why not replace paint() entirely?

Also... it seems that this will make the object have an "alpha" value. What I am looking to do is to make a portion of an image file (i.e. bitmap, gif, png) - say the white background - transparent, while the rest of the image still renders. This is a common technique very easy to achieve with OpenGL or DirectX. I need to target a *specific* RGB value and tell my program to ignore it in paint.

Share on other sites
It is very common because the image is already set up with transparency. gif supports transparency and png supports an alpha channel. Both are images that Java can load normally and will treat these transparent areas properly.

Share on other sites
If you want to use transparency with common jpeg images as a basis, you
have to roll your own. You could use GIF or PNG format, but GIF isn't
full color, and PNG has crummy compression and spotty support. My solution
is to use pairs of jpeg images and composite them as part of the image
setup.
http://www.andromeda.com/people/ddyer/java/imagedemo/transparent.html

Share on other sites
I had problems with this. You can't just cast a Graphics object to a Graphics2D object. In Swing this seems to be possible, but it isn't, because the Graphics object passed to some Swing methods is already a Graphics2D object.

You can draw the java.awt.Image onto a BufferedImage. Then you can call
yourBImg.createGraphics(). Now you will be able to use all the new Graphics2D functionalities.

Hope this will help!

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• Forum Statistics

• Total Topics
633650
• Total Posts
3013119
×