Jump to content
  • Advertisement
Sign in to follow this  
Drizzt DoUrden

[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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 alpha
AlphaComposite 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!