View Javadoc

1   /*
2    * Created on 23-April-2005
3    */
4   package ca.spaz.gui;
5   
6   import java.awt.*;
7   
8   import javax.swing.*;
9   
10  public class TranslucentLabel extends JLabel {
11     double transparency = 1;
12     
13     public TranslucentLabel(double transparency, String title) {
14        super(title);
15        this.transparency = transparency;
16        setOpaque(false);
17     }
18     
19     public TranslucentLabel(double transparency, String title, int alignment) {
20        super(title, alignment);
21        this.transparency = transparency;
22        setOpaque(false);
23     }
24     
25     
26     public TranslucentLabel(double transparency, Icon icon) {
27        super(icon);
28        this.transparency = transparency;
29        setOpaque(false);
30     }
31     
32     public void setTransparency(double val) {
33        this.transparency = val;
34     }
35  
36     public void paint(Graphics g) {
37        Graphics2D g2d = (Graphics2D)g;
38        g2d.setRenderingHint(
39              RenderingHints.KEY_ANTIALIASING, 
40              RenderingHints.VALUE_ANTIALIAS_ON);
41        
42        Composite c = g2d.getComposite();
43        g2d.setComposite(AlphaComposite.getInstance(
44              AlphaComposite.SRC_OVER, (float)transparency));
45        g2d.setColor(getBackground());
46        g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 15, 15);
47        g2d.setComposite(c);
48        super.paint(g);
49     }
50     
51  }