I have two classes, one where I create the frame and all the other things, and the class "RoundedButton", that is extended to JButton, where I modify the buttons in order to make it Rounded. When I run the programme and hover the mouse over the button the bottom background disappears. This is the class RoundedBotton:
public class RoundedButton extends JButton {
private Color backgroundColor;
private Color foregroundColor;
private Color hoverBackgroundColor;
public RoundedButton(String label, Color backgroundColor, Color foregroundColor) {
super(label);
this.backgroundColor = backgroundColor;
this.foregroundColor = foregroundColor;
this.hoverBackgroundColor = backgroundColor.darker();
setBorderPainted(false);
setBackground(backgroundColor);
setForeground(foregroundColor);
addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
setBackground(hoverBackgroundColor);
}
@Override
public void mouseExited(MouseEvent e) {
setBackground(backgroundColor);
}
});
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
RoundRectangle2D.Float roundRect = new RoundRectangle2D.Float(0, 0, getWidth(), getHeight(), 50, 50);
g2.clip(roundRect);
super.paintComponent(g2);
}
@Override
protected void paintBorder(Graphics g) {
g.setColor(backgroundColor);
g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 50, 50);
}
}
This is the method, where I create the frame and all other stuff:
void create(){
JFrame f = new JFrame("Rubrica");
f.setSize(600,750);
f.setResizable(false);
Font titFont = new Font("Oswald", Font.PLAIN,35); //Font per il titolo
Color bgColor = Color.decode("#333b41");
Color bColor = Color.decode("#202329");
Color cancColor = Color.decode("#f42244");
Color newColor = Color.decode("#2dc05f");
JPanel p = new JPanel(new BorderLayout());
f.add(p);
//useless code
JPanel pButton = new JPanel(new GridLayout(1,2,10,10));
pButton.setPreferredSize(new Dimension(600,80));
pButton.setBackground(bColor);
p.add(pButton, BorderLayout.SOUTH);
RoundedButton insert = new RoundedButton("Insert", newColor, Color.white);
RoundedButton delete = new RoundedButton("Delete", cancColor, Color.white);
pButton.add(insert);
pButton.add(delete);
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
I expect to hover themouse over the buttons and still see the colour of the panel where it is posizionated