Ky -
Ky -

Reputation: 32073

Transparency in Java

I've been using the new com.sun.awt.AWTUtilities class and am intrigued. I got com.sun.awt.AWTUtilities/setWindowOpacity(java.awt.Window window, float f) to work perfectly, but am now wondering if there is any way to change the opacity of an individual component, such as a javax.swing.JInternalFrame or javax.swing.JButton.

Upvotes: 2

Views: 461

Answers (1)

user372743
user372743

Reputation:

Try this:

class TransparentButton extends JButton {
        public TransparentButton(String text) { 
            super(text);
            setOpaque(false); 
        } 

        public void paint(Graphics g) { 
            Graphics2D g2 = (Graphics2D) g.create(); 
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); 
            super.paint(g2); 
            g2.dispose(); 
        } 
}

Upvotes: 1

Related Questions