Another Java SE 6 Treat

I use Java2D's AlphaComposite a lot. And when I mean a lot it's can be compared the amount of beer a football fan can drink when his team wins the Superbowl (sometimes you get the same hangover the next morning by the way). Anyway, as much as I like this class, it is one of the most verbose classes to use in Swing/Java2D alongside java.awt.Cursor. See how you can create a new alpha composite with a value of your choice:

Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, animatedTranslucency);

It is so tedious I set up macros for that in my IDEs (and likewise for SwingUtilities.invokeLater() and the cast of Graphics to Graphics2D). It also clutters painting code. Hopefully, I found a better way to do that in Java SE 6 by reading Chris' source code the other day:

Composite c = AlphaComposite.SrcOver.derive(animatedTranslucency)

This new method comes straight from java.awt.Font and makes the code not only a bit shorter but a heck lot easier to read.

Comments are closed.