0

While testing the following simple code I see a strange behavior: the button which has a transparent background color, gets more and more opaque, while the mouse enters or exits out of the bounds of the button. What is this strange behavior for? The code is as follows:

public class TransparentColorTest
{
    public static void main( String[] args )
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setLayout( new FlowLayout() );
        frame.setSize( 200 , 200 );
        Color transparentColor = new Color( 0 , 0 , 255 , 20 );
        JButton testButton = new JButton();
        testButton.setPreferredSize( new Dimension( 100 , 100 ) );
        testButton.setBackground( transparentColor );
        frame.add( testButton );
        frame.setVisible( true );
    }
}

The fourth argument to the Color constructor, determines the transparency of the color.

  • That doesn't happen on Mac O/SX with Java 1.8.0_66. What are you running? – stdunbar May 17 '16 at 20:51
  • I'm using windows 10, with java 1.8.0_102. – Hedayat Mahdipour May 17 '16 at 20:54
  • Swing only knows how to paint opaque and non-opaque components, it doens't know how paint components with alpha based colors, instead, you need to fake it, [for example](http://stackoverflow.com/questions/14201634/how-contents-are-made-translucent-in-a-jpanel/14206130#14206130 – MadProgrammer May 17 '16 at 20:56
  • See [Backgrounds With Transparency](https://tips4java.wordpress.com/2009/05/31/backgrounds-with-transparency/) for an explanation of the problem and a simple solution. – camickr May 17 '16 at 21:47

0 Answers0