Transparency in Java

I am using a new class com.sun.awt.AWTUtilitiesand intrigued. I got com.sun.awt.AWTUtilities/setWindowOpacity(java.awt.Window window, float f)to work fine, but now I wonder if there is a way to change the opacity of a single component, for example, javax.swing.JInternalFrameor javax.swing.JButton.

+3
source share
1 answer

Try the following:

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(); 
        } 
}
+1
source

Source: https://habr.com/ru/post/1774852/


All Articles