AlphaCompostiting part of the Graphics context, without 100% AffineTrasform

I got an interesting question here.

The game im developing has progressed nicely. The rendering thread does all the drawing operations on a global Graphics object which is drawn on the screen after all operations in the thread are complete, and repaint() is called.

I recently added an image to be the last thing drawn before repaint() is called, but I need that small image to be transparent.

The problem lies in the fact that in order to use an AlphaComposite, I would have to do all my drawing on a Graphics2D object inside the paint(Graphics g) method. Unfortunatly the current system I use involves a seperate thread doing all the rendering on a globaly scoped Graphics context, which is then rendered by paint(Graphics g).

If I type-casted g to be a g2d, I would not be able to send the... eh lol.. nevermind! I just minimized my browser and tried an idea that just poped to mind, and it worked! Um, thanks anyway lol.

I can't let all this typing go to waste! Most click Post! Ahhhhh

[1027 byte] By [EvolvedAnta] at [2007-9-28 17:59:20]
# 1

Since someone else might have the same problem, I will post solution incase someone searches the forums for this.

All I did was:

Created a Graphics2D context by casting with my back buffer directly then grabbed the default AlphaComposite like so AlphaComposite normal = (AlphaComposite)g2d.getComposite()

Then rendered on gBuffer (My backbuffer graphics context) until I got to the image that needed transparency, and simply did the following:

g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));

g2d.drawImage(myTransparentImage, 150, 150, this);

g2d.setComposite(normal);

And it all worked out well, and without much of any performance lost that I previously was expecting.

I'm going to optimize this code so that the g2d performs like the backbuffer, just incase flicker occurs due to it's rendering being done out of the backbuffer, but then again, there object im rendering in game is static, and hence should never have that problem. Guess it depends on other factors in my game.

-Adam

EvolvedAnta at 2007-7-12 15:42:25 > top of Java-index,Other Topics,Java Game Development...