001: package prefuse.util.ui;
002:
003: import java.awt.Font;
004: import java.awt.FontMetrics;
005: import java.awt.Graphics;
006: import java.awt.Graphics2D;
007: import java.awt.Insets;
008: import java.awt.RenderingHints;
009:
010: import javax.swing.JComponent;
011: import javax.swing.SwingConstants;
012:
013: /**
014: * Swing component that acts much like a JLabel, but does not revalidate
015: * its bounds when updated, making it much faster but suitable only for
016: * use in situations where the initial bounds are sufficient.
017: *
018: * @author <a href="http://jheer.org">jeffrey heer</a>
019: */
020: public class JFastLabel extends JComponent {
021:
022: private String m_text;
023: private int m_valign = SwingConstants.TOP;
024: private int m_halign = SwingConstants.LEFT;
025: private int m_fheight = -1;
026: private boolean m_quality = false;
027:
028: /**
029: * Create a new JFastLabel with no text.
030: */
031: public JFastLabel() {
032: this (null);
033: }
034:
035: /**
036: * Create a new JFastLabel with the given text.
037: * @param text the label text.
038: */
039: public JFastLabel(String text) {
040: m_text = text;
041: setFont(getFont());
042: }
043:
044: /**
045: * Get the label text.
046: * @return the label text
047: */
048: public String getText() {
049: return m_text;
050: }
051:
052: /**
053: * Set the label text
054: * @param text the label text to set
055: */
056: public void setText(String text) {
057: this .m_text = text;
058: repaint();
059: }
060:
061: /**
062: * @see java.awt.Component#setFont(java.awt.Font)
063: */
064: public void setFont(Font f) {
065: super .setFont(f);
066: m_fheight = -1;
067: }
068:
069: /**
070: * Set the vertical alignment.
071: * @param align the vertical alignment
072: * @see javax.swing.SwingConstants
073: */
074: public void setVerticalAlignment(int align) {
075: m_valign = align;
076: m_fheight = -1;
077: }
078:
079: /**
080: * Set the horizontal alignment.
081: * @param align the horizontal alignment
082: * @see javax.swing.SwingConstants
083: */
084: public void setHorizontalAlignment(int align) {
085: m_halign = align;
086: }
087:
088: /**
089: * Get the quality level of this label. High quality results in
090: * anti-aliased rendering.
091: * @return true for high quality, false otherwise
092: */
093: public boolean getHighQuality() {
094: return m_quality;
095: }
096:
097: /**
098: * Set the quality level of this label. High quality results in
099: * anti-aliased rendering.
100: * @param b true for high quality, false otherwise
101: */
102: public void setHighQuality(boolean b) {
103: m_quality = b;
104: }
105:
106: /**
107: * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
108: */
109: public void paintComponent(Graphics g) {
110: Insets ins = getInsets();
111: int w = getWidth() - ins.left - ins.right;
112: int h = getHeight() - ins.top - ins.bottom;
113: if (m_fheight == -1) {
114: FontMetrics fm = g.getFontMetrics(getFont());
115: if (m_valign == SwingConstants.BOTTOM)
116: m_fheight = fm.getDescent();
117: else if (m_valign == SwingConstants.TOP)
118: m_fheight = fm.getAscent();
119: }
120: g.setColor(getBackground());
121: g.fillRect(ins.left, ins.top, w, h);
122:
123: if (m_text == null)
124: return;
125:
126: g.setFont(getFont());
127: g.setColor(getForeground());
128: if (m_valign == SwingConstants.BOTTOM) {
129: h = h - m_fheight - ins.bottom;
130: } else {
131: h = ins.top + m_fheight;
132: }
133:
134: switch (m_halign) {
135: case SwingConstants.RIGHT: {
136: FontMetrics fm = g.getFontMetrics(getFont());
137: w = w - ins.right - fm.stringWidth(m_text);
138: break;
139: }
140: case SwingConstants.CENTER: {
141: FontMetrics fm = g.getFontMetrics(getFont());
142: w = ins.left + w / 2 - fm.stringWidth(m_text) / 2;
143: break;
144: }
145: default:
146: w = ins.left;
147: }
148: if (m_quality) {
149: ((Graphics2D) g).setRenderingHint(
150: RenderingHints.KEY_TEXT_ANTIALIASING,
151: RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
152: }
153: g.drawString(m_text, w, h);
154: }
155:
156: } // end of class JFastLabel
|