Java setClip 似乎重画

发布于 2024-08-06 15:48:52 字数 606 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

書生途 2024-08-13 15:48:52

按照汤姆的建议,从旧图形对象创建一个新图形对象对我来说很有效。

Creating a new Graphics object from the old one did the trick for me, as adviced by Tom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文