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

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.