KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > ui > JFastLabel


1 package prefuse.util.ui;
2
3 import java.awt.Font JavaDoc;
4 import java.awt.FontMetrics JavaDoc;
5 import java.awt.Graphics JavaDoc;
6 import java.awt.Graphics2D JavaDoc;
7 import java.awt.Insets JavaDoc;
8 import java.awt.RenderingHints JavaDoc;
9
10 import javax.swing.JComponent JavaDoc;
11 import javax.swing.SwingConstants JavaDoc;
12
13 /**
14  * Swing component that acts much like a JLabel, but does not revalidate
15  * its bounds when updated, making it much faster but suitable only for
16  * use in situations where the initial bounds are sufficient.
17  *
18  * @author <a HREF="http://jheer.org">jeffrey heer</a>
19  */

20 public class JFastLabel extends JComponent JavaDoc {
21
22     private String JavaDoc m_text;
23     private int m_valign = SwingConstants.TOP;
24     private int m_halign = SwingConstants.LEFT;
25     private int m_fheight = -1;
26     private boolean m_quality = false;
27     
28     /**
29      * Create a new JFastLabel with no text.
30      */

31     public JFastLabel() {
32         this(null);
33     }
34     
35     /**
36      * Create a new JFastLabel with the given text.
37      * @param text the label text.
38      */

39     public JFastLabel(String JavaDoc text) {
40         m_text = text;
41         setFont(getFont());
42     }
43
44     /**
45      * Get the label text.
46      * @return the label text
47      */

48     public String JavaDoc getText() {
49         return m_text;
50     }
51
52     /**
53      * Set the label text
54      * @param text the label text to set
55      */

56     public void setText(String JavaDoc text) {
57         this.m_text = text;
58         repaint();
59     }
60     
61     /**
62      * @see java.awt.Component#setFont(java.awt.Font)
63      */

64     public void setFont(Font JavaDoc f) {
65         super.setFont(f);
66         m_fheight = -1;
67     }
68     
69     /**
70      * Set the vertical alignment.
71      * @param align the vertical alignment
72      * @see javax.swing.SwingConstants
73      */

74     public void setVerticalAlignment(int align) {
75         m_valign = align;
76         m_fheight = -1;
77     }
78     
79     /**
80      * Set the horizontal alignment.
81      * @param align the horizontal alignment
82      * @see javax.swing.SwingConstants
83      */

84     public void setHorizontalAlignment(int align) {
85         m_halign = align;
86     }
87     
88     /**
89      * Get the quality level of this label. High quality results in
90      * anti-aliased rendering.
91      * @return true for high quality, false otherwise
92      */

93     public boolean getHighQuality() {
94         return m_quality;
95     }
96     
97     /**
98      * Set the quality level of this label. High quality results in
99      * 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 JavaDoc g) {
110         Insets JavaDoc 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 JavaDoc 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 JavaDoc fm = g.getFontMetrics(getFont());
137             w = w-ins.right-fm.stringWidth(m_text);
138             break;
139         } case SwingConstants.CENTER: {
140             FontMetrics JavaDoc fm = g.getFontMetrics(getFont());
141             w = ins.left + w/2 - fm.stringWidth(m_text)/2;
142             break;
143         } default:
144             w = ins.left;
145         }
146         if ( m_quality ) {
147             ((Graphics2D JavaDoc)g).setRenderingHint(
148                     RenderingHints.KEY_TEXT_ANTIALIASING,
149                     RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
150         }
151         g.drawString(m_text, w, h);
152     }
153     
154 } // end of class JFastLabel
155
Popular Tags