在 JPanel 中重置图形

发布于 2024-12-09 11:00:02 字数 108 浏览 5 评论 0原文

以下代码绘制了分子的结构。如果我不传递分子,最后一个结构将继续显示在 JPanel 中。当没有分子时,如何将其重置为空白画布。换句话说,我应该在drawMolecule()方法的else{}中放入什么?

Following Code Draws a structure of a molecule. If I don't pass a molecule the last structure continue to show up in the JPanel. How do I reset this to a blank canvas when there is no molecule. In other words what should I put inside else{} of drawMolecule() method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

半﹌身腐败 2024-12-16 11:00:02

您可以在 else 中创建一个新的 BufferedImage,也可以获取 BufferedImage 的 Graphics2D 组件,通过 setBackground(Color c) 设置其背景颜色,然后通过 clearRect(...) 清除图像 然后通过其 dispose() 方法处理 Graphics2D 对象。

事实上,在审查您的代码时,我没有看到您这样做。如果您自己创建了 Graphics 或 Graphics2D 资源,请不要忘记在使用完它们后将其释放(如果是通过其 Paint 或 PaintComponent 方法从 JVM 获取的,则不是)。

You could create a new BufferedImage in the else or you could get the BufferedImage's Graphics2D component, set its background color via setBackground(Color c) and then clear the image via clearRect(...) then dispose of the Graphics2D object when done via its dispose() method.

In fact, on review of your code I don't see you doing this. Don't forget to dispose of the Graphics or Graphics2D resource when done using them, if you've created them yourself (not if obtained from the JVM say via its paint or paintComponent method).

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