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

274
|
Chapter 7, Text
#51 Write Backward Text
HACK
(flipping them around the axis, so that the pixels furthest to the right are
now furthest to the left), and translating by the width of the component (so
the pixels move from negative coordinates, where they wouldn’t be seen,
back into positive space). This transformation is illustrated in Figure 7-8.
Mirror, Mirror on the Wall
The following main( ) method provides a trivial setup of a frame in which
you can view the backward
JLabel. The result is shown in Figure 7-9.
public static void main (String[] args) {
BackwardsJLabel field =
new BackwardsJLabel ("Through the looking glass");
JFrame frame = new JFrame("Backwards Text");
frame.getContentPane( ).add (field);
frame.pack( );
frame.setVisible(true);
}
Because the transformation is simple and limited to the paint( ) method, it
seems like something you could do anywhere. You can easily create a
BackwardsJTextField that uses exactly the same paint( ) method in a trivi-
ally overridden
JTextField—honestly, it’s so trivial that it’s not worth list-
ing here; just set up the trivial “call
super constructors, copy over this
paint( ) method, and tweak the main( ) method to show the text field
instead of the label. The result will look like Figure 7-10.
Figure 7-8. Mirror image as scale-and-translate AffineTransform
Figure 7-9. JLabel rendered as mirror image
Figure 7-10. JTextField rendered as mirror image

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