[java] alpha blending in java?

Started by
7 comments, last by AbelCorver 16 years, 2 months ago
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.
------------------------------Put THAT in your smoke and pipe it
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 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.
towerofinfinity.blogspot.comgamecodemonkey.blogspot.com
I prefer to just use the standart java.awt or awt.images if possible. Is that an option I have?
------------------------------Put THAT in your smoke and pipe it
Hmmm it seems Graphics2D is just a class within the awt, my mistake.

Thanks a bunch for pointing me in the right direction!
------------------------------Put THAT in your smoke and pipe it
The Graphics2D class is in java.awt so it will work with java.awt.images.
towerofinfinity.blogspot.comgamecodemonkey.blogspot.com
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.
------------------------------Put THAT in your smoke and pipe it
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.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
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

---visit my game site http://www.boardspace.net - free online strategy games

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!

This topic is closed to new replies.

Advertisement