Changing the alpha background of a JButton with the mouse

I want to have 2 buttons in my application with transparent background and I "almost" did it.

This is what I did:

public class PanelMenu extends JPanel{

//VARIABLES
private JButton buttonFightSimulator, buttonMatchUp;

//CONSTRUCTORS
public PanelMenu ()
{
    this.setBounds(0,0,240,768);
    this.setLayout(new FlowLayout(0, 0, 0));

    //BUTTON CREATION
    buttonFightSimulator = new JButton("FIGHT SIMULATOR");
    buttonMatchUp = new JButton("MATCH UP");

    buttonFightSimulator.setBackground(new Color(255,255,255,128));
    buttonFightSimulator.setFocusPainted(false);
    buttonFightSimulator.setBorderPainted(false);
    buttonFightSimulator.setPreferredSize(new Dimension(240,60));

    buttonMatchUp.setBackground(new Color(255,255,255,128));
    buttonMatchUp.setFocusPainted(false);
    buttonMatchUp.setBorderPainted(false);
    buttonMatchUp.setPreferredSize(new Dimension(240,60));

    add(buttonFightSimulator);
    add(buttonMatchUp);
    this.setBackground(new Color(0,0,0,90));
}

      

And this is what I visually have:

enter image description here

It's good that this is what I wanted. But when I pass the mouse over two buttons, this happens:

enter image description here

So, at first, the background becomes less and less transparent each time the mouse moves over it, and then we see that the text of both buttons is blended.

Thank you in advance for your reply.

+3


source to share


1 answer


Check out Backgrounds Transparency for an explanation of the problem and a few solutions.



The main problem is that your component is opaque, but the background has transparency, which violates the coloring contract between the swing components so that you can paint artifacts.

+2


source







All Articles