1 package prefuse.util.ui; 2 3 import java.awt.Font ; 4 import java.awt.FontMetrics ; 5 import java.awt.Graphics ; 6 import java.awt.Graphics2D ; 7 import java.awt.Insets ; 8 import java.awt.RenderingHints ; 9 10 import javax.swing.JComponent ; 11 import javax.swing.SwingConstants ; 12 13 20 public class JFastLabel extends JComponent { 21 22 private String 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 31 public JFastLabel() { 32 this(null); 33 } 34 35 39 public JFastLabel(String text) { 40 m_text = text; 41 setFont(getFont()); 42 } 43 44 48 public String getText() { 49 return m_text; 50 } 51 52 56 public void setText(String text) { 57 this.m_text = text; 58 repaint(); 59 } 60 61 64 public void setFont(Font f) { 65 super.setFont(f); 66 m_fheight = -1; 67 } 68 69 74 public void setVerticalAlignment(int align) { 75 m_valign = align; 76 m_fheight = -1; 77 } 78 79 84 public void setHorizontalAlignment(int align) { 85 m_halign = align; 86 } 87 88 93 public boolean getHighQuality() { 94 return m_quality; 95 } 96 97 102 public void setHighQuality(boolean b) { 103 m_quality = b; 104 } 105 106 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 } case SwingConstants.CENTER: { 140 FontMetrics 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 )g).setRenderingHint( 148 RenderingHints.KEY_TEXT_ANTIALIASING, 149 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 150 } 151 g.drawString(m_text, w, h); 152 } 153 154 } | Popular Tags |