Reputation: 1251
I was wondering if there actually a difference between Graphics2D.setComposite(..., alpha)
and Graphics2D.setColor(new Color(..., alpha))
when using transparency in Java? How do they affect each other when using a combination of both, e.g.
Graphics2D.setComposite(..., 0.5f)
Graphics2D.setColor(new Color(..., 0.5f))
It seems that the result is not a transparency of 0.5, but more like 0.25. Is there any recommendation to use one of the previously mentioned approaches?
Upvotes: 2
Views: 4366
Reputation: 347314
Graphics2D.setComposite(..., 0.5f)
will effect EVERYTHING that is painted to the Graphics
context after you apply it. This includes primitives as well as images.
Graphics2D.setColor(new Color(..., 0.5f))
will only effect the painting for primitives, every thing else will painted full opaque.
You are right in in the fact that if you paint a color that is 50% transparent onto a Graphics
context that is 50% transparent will result in a color that appears to be 25% transparent. The two won't cancel each other out, but will compound.
Think of it this way.
@100% opacity, the color is 50% opaque.
@75% opacity, the color is reduced by 25%, making it 37.5% opaque
@50% opacity, the color is reduced by 50%, making it 25% opaque
@25% opacity, the color is reduced by 75%, making it 12.5% opaque
Upvotes: 3