Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


[java] alpha blending in java?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Drizzt DoUrden   Members   -  Reputation: 100

Like
0Likes
Like

Posted 06 February 2008 - 01:40 PM

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.

Sponsor:

#2 terloon   Members   -  Reputation: 122

Like
0Likes
Like

Posted 06 February 2008 - 02:01 PM

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.

#3 Drizzt DoUrden   Members   -  Reputation: 100

Like
0Likes
Like

Posted 06 February 2008 - 02:12 PM

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

#4 Drizzt DoUrden   Members   -  Reputation: 100

Like
0Likes
Like

Posted 06 February 2008 - 02:17 PM

Hmmm it seems Graphics2D is just a class within the awt, my mistake.

Thanks a bunch for pointing me in the right direction!

#5 terloon   Members   -  Reputation: 122

Like
0Likes
Like

Posted 06 February 2008 - 02:17 PM

The Graphics2D class is in java.awt so it will work with java.awt.images.

#6 Drizzt DoUrden   Members   -  Reputation: 100

Like
0Likes
Like

Posted 06 February 2008 - 02:29 PM

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.


#7 CaptainJester   Members   -  Reputation: 523

Like
0Likes
Like

Posted 06 February 2008 - 03:11 PM

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]

#8 ddyer   Members   -  Reputation: 234

Like
0Likes
Like

Posted 12 February 2008 - 10:25 AM

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


#9 AbelCorver   Members   -  Reputation: 112

Like
0Likes
Like

Posted 22 February 2008 - 07:49 AM

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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS