1

I have a rounded JButton in my JFrame but it has the sharp corners of which I assume is the default background/border for the JButton itself. I would like to remove that "shaded" tint so that it blends well with the frame color.

For example, I've changed the background color of the button to RED and the corners of the borders are still visible.

Red button with visible background/border

Anyway to go about this?

startButton = new JButton("Start!");
startButton.setPreferredSize(new Dimension(150,80));
startButton.setBorder(new RoundedButton(20));
startButton.setBackground(Color.RED);
startButton.setFont(new Font(null, Font.BOLD, 20));
startButton.addActionListener(this);

class RoundedButton implements Border 
{
    private int roundRadius;
    
    RoundedButton(int roundRadius) 
    {
        this.roundRadius = roundRadius;
    }

    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
    {
        g.drawRoundRect(x, y, width-1, height-1, roundRadius, roundRadius);
    }

    public Insets getBorderInsets(Component c) 
    {
        // TODO Auto-generated method stub
        return new Insets(this.roundRadius+1, this.roundRadius+1, this.roundRadius+2, this.roundRadius+2);
    }

    public boolean isBorderOpaque()
    {
        return true;
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1) Have a look at [this Q&A](https://stackoverflow.com/q/15025092/418556) which does much the same thing (I think). 2) Use tags wisely. This one will get a lot more views with the Swing and Java tags added. 3) For better help sooner, [edit] to add a [mre]. – Andrew Thompson Jan 14 '22 at 09:25

1 Answers1

0

Please see below. Use it as you normally do in your provided example, i.e., startButton.setBorder(new RoundedButton(20)); passing the radius parameter.

Also, optionally, you can add the next line of code, i.e., startButton.setFocusPainted(false); to disable the focus border of the JButton.

class RoundedButton implements Border {
    private int roundRadius;

    RoundedButton(int roundRadius) {
        this.roundRadius = roundRadius;
    }

    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int r = roundRadius;
        int w = width - 1;
        int h = height - 1;

        Area round = new Area(new RoundRectangle2D.Float(x, y, w, h, r, r));
        Container parent = c.getParent();
        if (Objects.nonNull(parent)) {
            g2.setPaint(parent.getBackground());
            Area corner = new Area(new Rectangle2D.Float(x, y, width, height));
            corner.subtract(round);
            g2.fill(corner);
        }
        g2.draw(round);
        g2.dispose();
    }

    public Insets getBorderInsets(Component c) {
        return new Insets(this.roundRadius + 1, this.roundRadius + 1, this.roundRadius + 2, this.roundRadius + 2);
    }

    public boolean isBorderOpaque() {
        return true;
    }
}
Chris
  • 18,724
  • 6
  • 46
  • 80