Swing绘图问题

4
我有一个自定义的滚动条UI,我会绘制滚动条的拇指和轨道。但是当滚动时,它会保留一些不需要的线条,如下图所示: enter image description here 绘制代码如下:
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);
}

3
为了更快地获得帮助,请发布一个SSCCE。需提供可运行的最小化示例代码,以便更容易理解和复现问题。 - Andrew Thompson
2
我猜你需要使用 g.drawRoundRect(...,thumbBounds.height-1,2,2) 而不是 g.drawRoundRect(...,thumbBounds.height,2,2) - aterai
成功了...!感谢aterai...不知能否告诉我原因? - Priyan Perera
3
“drawRoundRect(x,y,width,height,arcW,arcH)”方法的底部边缘位于“y + height”位置,但是“repaint(thumbBounds)”方法的底部边缘位于“thumbBounds.y + thumbBounds.height - 1”位置。因此,留出1像素的线条没有被重新绘制。 - aterai
1个回答

0
你需要使用

标签。

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

而不是

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

drawRoundRect(x,y,width,height,arcW,arcH) 的底部边缘位于 y + height,但是 repaint(thumbBounds) 的底部边缘位于 thumbBounds.y + thumbBounds.height - 1。因此,留下了 1 像素的线条未被重绘。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接