/* A component for displaying a mirror-reversed line of text. The text will be centered in the available space. This component is defined as a subclass of JPanel. It respects any background color, foreground color, and font that are set for the JPanel. The setText(String) method can be used to change the displayed text. Changing the text will also call revalidate() on this component. */ import java.awt.*; import javax.swing.*; public class MirrorLabel extends JPanel { // Constructor and methods meant for use public use. public MirrorLabel(String text) { // Construct a MirrorLable to display the specified text. this.text = text; } public void setText(String text) { // Change the displayed text. Call revalidate // so that the layout of its container can be // recomputed. this.text = text; revalidate(); repaint(); } public String getText() { // Return the string that is displayed by this component. return text; } // Implementation. Not meant for public use. private String text; // The text displayed by this component. private Image OSC; // An off-screen image holding the non-reversed text. private int widthOfOSC, heightOfOSC; // Size of the off-screen image. public void paintComponent(Graphics g) { // The paint method makes a new OSC, if necessary. It writes // a non-reversed copy of the string to the the OSC, then reverses // the OSC as it copies it to the screen. (Note: color or font // might have changed since the last time paint() was called, so // I can't just reuse the old image in the OSC.) if (OSC == null || getSize().width != widthOfOSC || getSize().height != heightOfOSC) { OSC = createImage(getSize().width, getSize().height); widthOfOSC = getSize().width; heightOfOSC = getSize().height; } Graphics OSG = OSC.getGraphics(); OSG.setColor(getBackground()); OSG.fillRect(0, 0, widthOfOSC, heightOfOSC); OSG.setColor(getForeground()); OSG.setFont(getFont()); FontMetrics fm = OSG.getFontMetrics(getFont()); int x = (widthOfOSC - fm.stringWidth(text)) / 2; int y = (heightOfOSC + fm.getAscent() - fm.getDescent()) / 2; OSG.drawString(text, x, y); OSG.dispose(); g.drawImage(OSC, widthOfOSC, 0, 0, heightOfOSC, 0, 0, widthOfOSC, heightOfOSC, null); } public Dimension getPreferredSize() { // Compute a preferred size that will hold the string plus // a border of 5 pixels. FontMetrics fm = getFontMetrics(getFont()); return new Dimension(fm.stringWidth(text) + 10, fm.getAscent() + fm.getDescent() + 10); } } // end MirrorLabel