![]() |
|
java - additive color blender |
Matthew Leverton
Supreme Loser
January 1999
![]() |
I have to give a presentation for a Java class, and I'm thinking about changing my topic last minute since somebody else who goes before me decided to speak on the same topic I chose. A long time ago I ported Shawn's SPEED game to a Java applet. I didn't really know much about Java then (and I still don't The problem is I don't know how to do anything other than solid color drawing. I'm looking for a simple way to essentially duplicate this Allegro functionality (additive color blending), preferably just using regular Graphics2D in a JPanel:
E.g., the green grid lines are RGB(0,51,0), so when they intersect, they should be RGB(0,102,0). |
Hard Rock
Member #1,547
September 2001
![]() |
You'll want to be using AlphaComposite to do the effects. I'm not sure if it does everything Allegro does out of the box, but a quick google finds this library: http://www.jroller.com/gfx/entry/new_blendings_modes_for_java2d Which will do all the allegro blending modes. As a bonus it works exactly like AlphaComposite, so you just do
(credit to above code from: http://java.sun.com/products/jfc/tsc/articles/swing2d/index.html) But replace alphacomposite with blendcomposite from the link I gave earlier and specify your blending mode. (It's BSD so no license issues). May be a bit slow, but since you're just drawing lines it should run just fine. [edit] _________________________________________________ |
Matthew Leverton
Supreme Loser
January 1999
![]() |
I trimmed down the example to just:
So basically: g.setComposite(new BlendComposite());
And my additive blending works as expected. But there's one big problem. It's horribly slow. Just drawing a few lines is enough to bring it crawling to its knees. Is it because I'm drawing straight to a JPanel? I thought those were double buffered automatically. I can make the above code more efficient, but I don't want to be going down a dead end lane... Update: I switched to drawing to a BufferedImage and it's much faster now. Hopefully it's fast enough to get 30fps. |
Archon
Member #4,195
January 2004
![]() |
Quote: Just drawing a few lines is enough to bring it crawling to its knees. Is it because I'm drawing straight to a JPanel? I thought those were double buffered automatically. In my drawing applet, I draw to a BufferedImage object and in the paint method, I draw that BufferedImage to the JPanel. It runs fine. Quote: I switched to drawing to a BufferedImage and it's much faster now. Hopefully it's fast enough to get 30fps.
I wrote the above before I read this quote |
Matthew Leverton
Supreme Loser
January 1999
![]() |
I've got four simultaneously displayed projections (just the grids) being drawn via the additive blender and it is running fast enough for demonstration purposes. The bad guys might slow it down some since they are filled polygons, but I can just limit their numbers if it is a problem. |
|