Java 摇摆油漆问题

Java 摇摆油漆问题,java,swing,drawing,java-2d,Java,Swing,Drawing,Java 2d,我有一个自定义的scrollbarUI,我画了scrollbar的拇指和轨迹。但当滚动时,它会保留一些我不想要的行,如下面的行 喷漆代码如下: protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) { Graphics2D g2d = (Graphics2D) g; GradientPaint gradient = new GradientPaint(new Point(thumb

我有一个自定义的scrollbarUI,我画了scrollbar的拇指和轨迹。但当滚动时,它会保留一些我不想要的行,如下面的行

喷漆代码如下:

protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
    Graphics2D g2d = (Graphics2D) g;

    GradientPaint gradient = new GradientPaint(new Point(thumbBounds.x, thumbBounds.y), gray, new          Point(thumbBounds.x + width, thumbBounds.y), white);

    g.setColor(white);
    g.fillRoundRect(thumbBounds.x + 1, thumbBounds.y + 1, thumbBounds.width - 3, thumbBounds.height - 1, 2, 2);

    g2d.setPaint(gradient);
    g2d.fillRoundRect(thumbBounds.x + 2, thumbBounds.y + 2, thumbBounds.width - 4, thumbBounds.height - 3, 3, 3);

    //Draw middle lines:
    if ((getMinimumThumbSize().height + 10) < thumbBounds.height) {
        g.setColor(new Color(167, 167, 167));
        int w = ((thumbBounds.width > 16) ? 8 : (int) ((thumbBounds.width / 2.0) + 0.5));
        int x = (thumbBounds.width > 0) ? (thumbBounds.x + ((thumbBounds.width - w) / 2) - 1) : thumbBounds.x;

        g.drawLine(x, (thumbBounds.y + (thumbBounds.height / 2) - 3), (x + w), (thumbBounds.y + (thumbBounds.height / 2) - 3));
        g.drawLine(x, (thumbBounds.y + (thumbBounds.height / 2) - 1), (x + w), (thumbBounds.y + (thumbBounds.height / 2) - 1));
        g.drawLine(x, (thumbBounds.y + (thumbBounds.height / 2) + 1), (x + w), (thumbBounds.y + (thumbBounds.height / 2) + 1));
    }

    g.setColor(color_1);
    g.drawRoundRect(thumbBounds.x, thumbBounds.y, thumbBounds.width - 2, thumbBounds.height, 2, 2);
}
受保护的void paintThumb(图形g、JComponent c、矩形指界){
Graphics2D g2d=(Graphics2D)g;
GradientPaint gradient=新GradientPaint(新点(thumbBounds.x,thumbBounds.y),灰色,新点(thumbBounds.x+宽度,thumbBounds.y),白色);
g、 设置颜色(白色);
g、 fillRoundRect(thumbBounds.x+1,thumbBounds.y+1,thumbBounds.width-3,thumbBounds.height-1,2,2);
g2d.setPaint(梯度);
g2d.fillRoundRect(thumbBounds.x+2,thumbBounds.y+2,thumbBounds.width-4,thumbBounds.height-3,3);
//画中间线:
如果((getMinimumThumbSize().height+10)16)?8:(int)((thumbunds.width/2.0)+0.5));
intx=(thumbBounds.width>0)?(thumbBounds.x+((thumbBounds.width-w)/2)-1):thumbBounds.x;
g、 抽绳(x,(thumbBounds.y+(thumbBounds.height/2)-3),(x+w),(thumbBounds.y+(thumbBounds.height/2)-3);
g、 抽绳(x,(thumbBounds.y+(thumbBounds.height/2)-1),(x+w),(thumbBounds.y+(thumbBounds.height/2)-1);
g、 抽绳(x,(thumbBounds.y+(thumbBounds.height/2)+1),(x+w),(thumbBounds.y+(thumbBounds.height/2)+1);
}
g、 设置颜色(颜色_1);
g、 drawRoundRect(thumbBounds.x,thumbBounds.y,thumbBounds.width-2,thumbBounds.height,2,2);
}
您需要使用

g.drawRoundRect(..., thumbBounds.height - 1, 2, 2) 
而不是

g.drawRoundRect(..., thumbBounds.height, 2, 2)

drawRoundRect(x,y,宽度,高度,弧长,弧长)
的底边位于
y+高度
,但是
repaint(thumbBounds)
的底边位于
thumbBounds.y+thumbBounds.height-1
。因此,1px线无需重新喷漆

为了更快地获得更好的帮助,请发布一个。我想您需要使用
g.drawRoundRect(…,thumbBounds.height-1,2,2)
而不是
g.drawRoundRect(…,thumbBounds.height,2,2)
。这很有效。!谢谢你。。我可以知道清楚的原因吗?
drawRoundRect(x,y,width,height,arcW,arcH)
的底边位于
y+height
,但是
重绘(thumbunds)
的底边位于
thumbunds.y+thumbunds.height-1
。因此,1px线无需重新喷漆。