How to remove “stretch marks” on custom button border?

后端 未结 2 1735
遇见更好的自我
遇见更好的自我 2021-01-14 13:27

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

2条回答
  •  醉话见心
    2021-01-14 14:03

    it's because you draw if by drawing several 1px thick borders which misses some pixels in the arcs (draw it again with different colors and zoom in on the image to see it)

    use the fill to set the color of the border and then draw the contrasting borders

    Color fillcolor = Color.WHITE;
    Color bordercolor = Color.BLACK;
    
    if(getModel().isSelected())
    {
        fillcolor = Color.BLACK;
    }
    //fill the full rectangle
    g2.setColor(fillcolor);
    g2.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, ARC_WIDTH, ARC_HEIGHT);
    
    //draw contrasted borders
    g2.setColor(bordercolor);
    g2.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, ARC_WIDTH, ARC_HEIGHT);
    g2.setColor(bordercolor);
    g2.drawRoundRect(3, 3, getWidth() - 7, getHeight() - 7, ARC_WIDTH, ARC_HEIGHT);
    

提交回复
热议问题