O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Anti-Aliased Text with a Custom Look and Feel #55
Chapter 7, Text
|
285
HACK
that
SwingUtilities2
is an undocumented and hidden class for which no
support will ever be provided. This means that it can be changed without
notice. Using it might break your applications in a future release of Java.
—Romain Guy
H A C K
#55
Anti-Aliased Text with a Custom
Look and Feel Hack #55
Another way to get smooth text is to use a custom Look and Feel to avoid the
fragile Java 5.0 APIs.
When the Java 5.0 anti-aliasing trick [Hack #54] was first discovered, some dis-
cussions arose on the Web. Many people strongly disagree with using it
because it can be broken at any time by Sun Microsystems. Should we be
deprived of anti-aliased text because of such a silly problem? Frédéric Lav-
igne, author of the famous Skin L&F and webmaster of www.javootoo.com,
a great repository of Look and Feels for Swing, didn’t think so and found an
elegant and clever way to get the same result.
The Wrap Look and Feel
His idea is to use a custom Look and Feel whose sole purpose is to enable
anti-aliasing hints on the
Graphics instances used to draw the UI. He imple-
mented his idea in the Wrap Look and Feel, which can be downloaded at
wraplf.l2fprod.com. Another Look and Feel, SmoothMetal, enables anti-
aliasing in your application. Yet, you are stuck with Metal Look and Feel
when using it. Wrap Look and Feel acts as a decorator for the current Look
and Feel. Thus, you can choose any Look and Feel you want and wrap it
with Wrap Look and Feel to enable anti-aliasing. Doing so requires a single
line of code:
import com.l2fprod.common.swing.plaf.wrap.Wrapper;
Wrapper.wrap( );
Figure 7-21. Anti-aliased text without J2SE 5.0 swing.aatext property
286
|
Chapter 7, Text
#55 Anti-Aliased Text with a Custom Look and Feel
HACK
No matter which Swing Look and Feel you set, the wrap( ) method will han-
dle it properly. One line of code is good, but not good enough. We’d be bet-
ter off with no line of code at all. Frédéric feels the same way and provides
the excellent
Wrapit
class you can use to install the Wrap Look and Feel at
runtime:
java -classpath wraplf.jar;. Wrapit WebHunter
The Wrapit class contains a main( ) entry point that will install the Wrap
Look and Feel and then call the
main( ) entry point of the class passed as the
first argument on the command line. This Look and Feel is a powerful tool
you can use to enhance the appearance of any Java application, whether you
have the source code or not.
—Romain Guy

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required