I want to have 2 buttons on my application with a transparent background and I "almost" done it.
This is what I've done :
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 :
Well that's great that's what I wanted. But when I pass my mouse over the 2 buttons, this is what happen :
So first the background get less and less transparent every time the mouse moves over it and then we can see that the text of the both buttons are mixed together.
Thank you in advance for your answer.
Check out Backgrounds With Transparency for an explanation of the problem and a couple of solutions.
The basic problem is that your component is opaque but the background has transparency which breaks the painting contract between swing components so you get painting artifacts.