Search code examples
javaswingborderjbuttoncustom-painting

How to remove "stretch marks" on custom button border?


When performing some custom painting on a button's Graphics2D object, I get the following results:

enter image description here

The leftmost button is untoggled and the other is toggled. As you can see, the toggled button has these white "stretch marks." Why are these there and how do I remove them?

And here is the code I use to draw the border:

// Draw border of button
if(!getModel().isSelected())
{
    g2.fillRoundRect(2, 2, getWidth() - 5, getHeight() - 5, ARC_WIDTH, ARC_HEIGHT);
    g2.setColor(Color.BLACK);
    g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, ARC_WIDTH, ARC_HEIGHT);
    g2.setColor(Color.WHITE);
    g2.drawRoundRect(1, 1, getWidth() - 3, getHeight() - 3, ARC_WIDTH, ARC_HEIGHT);
    g2.setColor(Color.WHITE);
    g2.drawRoundRect(2, 2, getWidth() - 5, getHeight() - 5, ARC_WIDTH, ARC_HEIGHT);
    g2.setColor(Color.BLACK);
    g2.drawRoundRect(3, 3, getWidth() - 7, getHeight() - 7, ARC_WIDTH, ARC_HEIGHT);
}
else
{
    g2.fillRoundRect(2, 2, getWidth() - 5, getHeight() - 5, ARC_WIDTH, ARC_HEIGHT);
    g2.setColor(Color.BLACK);
    g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, ARC_WIDTH, ARC_HEIGHT);
    g2.setColor(Color.BLACK);
    g2.drawRoundRect(1, 1, getWidth() - 3, getHeight() - 3, ARC_WIDTH, ARC_HEIGHT);
    g2.setColor(Color.BLACK);
    g2.drawRoundRect(2, 2, getWidth() - 5, getHeight() - 5, ARC_WIDTH, ARC_HEIGHT);
    g2.setColor(Color.BLACK);
    g2.drawRoundRect(3, 3, getWidth() - 7, getHeight() - 7, ARC_WIDTH, ARC_HEIGHT);
}

Solution

  • Why are you drawing so many rounded rectangles around the button? As far as I can see, the correct way to do this would be to use setStroke() API while drawing the kind of border you like.