Java setClip 似乎重画
我在 Java 中使用 setClip 时遇到了一些问题。我有一个扩展 JPanel 的类。在该类中,我重写了paintComponent 方法。我的paintComponent 方法看起来像这样:
paintComponent {
//draw some lines here
Rectangle whole = g2.getClipBounds();//g2 is my Graphics2D object
Rectangle part = <some rectangle that is a part of the whole paintable area>;
g2.setClip(part);
//draw some more stuff here
g2.setClip(whole);
}
我看到的问题是剪切区域中的区域似乎被重复绘制。例如,如果我告诉它画画,它画得很好。但是,如果我切换窗口或以其他方式导致它再次绘制相同的内容,则剪切区域不会被清除,而其余区域则会被清除。这会导致剪切区域上的绘画看起来比其他可绘画区域更粗。
我想我在 setClip 的工作方式中遗漏了一些东西。
任何建议将不胜感激。预先感谢您的任何帮助。
I'm having some troubles with setClip in Java. I have a class that extends JPanel. Within that class I have overridden the paintComponent method. My paintComponent method looks something like this:
paintComponent {
//draw some lines here
Rectangle whole = g2.getClipBounds();//g2 is my Graphics2D object
Rectangle part = <some rectangle that is a part of the whole paintable area>;
g2.setClip(part);
//draw some more stuff here
g2.setClip(whole);
}
The problem that I'm seeing is that the area in the clipped region seems to be painted repeatedly. For example, if I tell it to paint, it paints just fine. But then, if I switch windows or somehow else cause it to paint the same thing again, the clipped region isn't cleared while the rest is. This results in the painting on the clipped region to appear bolder than the rest of the paintable area.
I imagine that I'm missing something in how setClip works.
Any suggestions would be much appreciated. Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按照汤姆的建议,从旧图形对象创建一个新图形对象对我来说很有效。
Creating a new Graphics object from the old one did the trick for me, as adviced by Tom.