FileDocCategorySizeDatePackage
RichJLabel.javaAPI DocExample3020Mon Jan 09 11:01:58 GMT 2006None

RichJLabel

public class RichJLabel extends JLabel

Fields Summary
private int
tracking
private int
left_x
private int
left_y
private int
right_x
private int
right_y
private Color
left_color
private Color
right_color
Constructors Summary
public RichJLabel(String text, int tracking)

 
        super(text);
        this.tracking = tracking;
    
Methods Summary
public java.awt.DimensiongetPreferredSize()

        String text = getText();
        FontMetrics fm = this.getFontMetrics(getFont());
        
        int w = fm.stringWidth(text);
        w += (text.length()-1)*tracking;
        w += left_x + right_x;
        
        int h = fm.getHeight();
        h += left_y + right_y;

        return new Dimension(w,h);
    
public static voidmain(java.lang.String[] args)

        RichJLabel label = new RichJLabel("76", 0);
        
        
        /*
        // drop shadow w/ highlight
        label.setLeftShadow(1,1,Color.white);
        label.setRightShadow(2,3,Color.black);
        label.setForeground(Color.gray);
        label.setFont(label.getFont().deriveFont(140f));
        */
        
        
        // subtle outline
        label.setLeftShadow(1,1,Color.white);
        label.setRightShadow(1,1,Color.white);
        label.setForeground(Color.blue);
        label.setFont(label.getFont().deriveFont(140f));
        
        
        /*
        // 3d letters
        label.setLeftShadow(5,5,Color.white);
        label.setRightShadow(-3,-3, new Color(0xccccff));
        label.setForeground(new Color(0x8888ff));
        label.setFont(label.getFont().deriveFont(140f));
        */

        JFrame frame = new JFrame("RichJLabel hack");
        frame.getContentPane().add(label);
        frame.pack();
        frame.setVisible(true);
    
public static voidp(java.lang.String str)

        System.out.println(str);
    
public voidpaintComponent(java.awt.Graphics g)

        ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        
        char[] chars = getText().toCharArray();

        FontMetrics fm = this.getFontMetrics(getFont());
        int h = fm.getAscent();
        LineMetrics lm = fm.getLineMetrics(getText(),g);
        g.setFont(getFont());
        
        int x = 0;
        
        for(int i=0; i<chars.length; i++) {
            char ch = chars[i];
            int w = fm.charWidth(ch) + tracking;

            g.setColor(left_color);
            g.drawString(""+chars[i],x-left_x,h-left_y);
            
            g.setColor(right_color);
            g.drawString(""+chars[i],x+right_x,h+right_y);

            g.setColor(getForeground());
            g.drawString(""+chars[i],x,h);

            x+=w;
        }
        
    
public voidsetLeftShadow(int x, int y, java.awt.Color color)

        left_x = x;
        left_y = y;
        left_color = color;
    
public voidsetRightShadow(int x, int y, java.awt.Color color)

        right_x = x;
        right_y = y;
        right_color = color;