
Anti-Aliased Text Without Code #54
Chapter 7, Text
|
283
HACK
H A C K
#54
Anti-Aliased Text Without Code
Hack #54
Draw anti-aliased text without any code changes at all using two clever tricks
introduced in Java 5.0.
Since Java 1.2, UI programmers can draw anti-aliased text. Unfortunately,
anti-aliasing must be enabled for every Swing component by writing a few
lines of code for each of them
[Hack #53]. This hack describes a clever way to
turn on anti-aliasing for an entire frame by adding a customized repaint
manager. As every programmer seeks for effortless solutions, we will dis-
cover how to do the same without writing any lines of code.
The Java 5.0 Trick
Sun Microsystems released Java 5.0, a.k.a. Tiger, in September 2004.
Among many improvements, like a new theme for the Metal L&F, this
release of J2SE paves the way for application-wide text anti-aliasing support
in Mustang, the upcoming release of Java. To this end, the Swing team
added a special field in the hidden class
com.sun.java.swing.SwingUtilities2.
Meant for internal purposes only, this class is left undocumented by Sun’s
engineering teams.
If you look closely at its source code, provided in src.zip with Sun’s JVM,
you’ll discover a very interesting method:
drawTextAntialiased(JComponent c).
This method returns a
boolean value used by Swing’s painting framework to
know whether the specified component must be drawn with anti-aliased
text. Here ...