Kevin1031
Kevin1031

Reputation: 9

How to set alpha value of a selected BufferedImage

I was able to alter the alpha value of a BufferedImage with this line:

g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, rail0FlashList.get(fl)));

The problem here is that all the other components that came after this line were also affected. Is there a way to change the alpha value of a single, selected BufferedImage?

Upvotes: 0

Views: 48

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

I'm taking bit of guess here, but...

Graphics2D ga = (Graphics2D)g2.create();
ga.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, rail0FlashList.get(fl)));
// Use ga to render what ever needs to be painted with the alpha composite
ga.dispose();

Basically what this does is, it creates a snapshot of the Graphics state, allowing you to modify it, without affecting the state of the original context. It's very useful when dealing with transformations as well

Upvotes: 1

Related Questions