Swing paint问题
问题描述:
我有一个自定义scrollbarUI,我画滚动条的拇指和轨迹。但是当滚动时,它保留了一些我不想要的线条。 :Swing paint问题
绘画代码如下:
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);
}
答
您需要使用
g.drawRoundRect(..., thumbBounds.height - 1, 2, 2)
,而不是
g.drawRoundRect(..., thumbBounds.height, 2, 2)
012的底部边缘位于y + height
,但repaint(thumbBounds)
的底部边缘位于thumbBounds.y + thumbBounds.height - 1
。所以1px行仍然没有重绘。
为了更快提供更好的帮助,请发布[SSCCE](http://sscce.org/)。 – 2013-05-14 04:55:00
我想你需要使用g.drawRoundRect(...,thumbBounds.height - 1,2,2)''而不是'' '。 – aterai 2013-05-14 05:35:15
工作。 。 。!谢谢aterai ..我可以知道清楚的原因吗? – 2013-05-14 05:56:21