在 JPanel 上绘制线条而不重新绘制它

发布于 2024-11-08 20:58:59 字数 267 浏览 4 评论 0原文

我想在 JPanel 上绘制一条垂直线,并使其在其上滑动,而无需此过程调用 JPanelpaintComponent()代码>.我曾考虑过使用 GlassPane 但我认为这不是正确的方法,因为框架中还有其他组件包含 JPanel,所以它不是具体到它(而且我实际上不确定它不会调用 paintComponent() )。

有什么想法吗?

I would like to have a vertical line drawn over a JPanel, and make it glide over it, without this process invoking the paintComponent() of the JPanel. I have thought of using the GlassPane but I don't think it is the correct way since there are other components in the frame containing the JPanel, and so it isn't specific to it (and I'm not actually sure it wouldn't call the paintComponent() anyway).

Any ideas?

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

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

发布评论

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

评论(3

罗罗贝儿 2024-11-15 20:58:59

如果您只是想隔离,也许您应该使用 分层窗格其余绘画代码中的线条绘画代码。

如果您的绘画代码很昂贵,那么也许您应该创建一个 BufferedImage,然后在您的 PaintComponent() 代码中重新绘制图像。这比每次都从头开始重新绘制要快。

Maybe you should be using Layered Panes if you just want to isolate the line painting code from the rest of your painting code.

If your painting code is expensive, then maybe you should be creating a BufferedImage and then just redraw the image in your paintComponent() code. This is faster than repainting from scratch every time.

眼眸里的那抹悲凉 2024-11-15 20:58:59

即使使用 GlassPane,底层组件也必须在某个时候重新绘制。没有一个好的方法可以避免对 JPanel 的 paintComponent 调用。

但是,JPanel 不应该在 paintComponent 中执行除绘画之外的操作。如果您试图避免调用它,那么听起来 paintComponent 方法中的某些内容需要以某种方式进行更改或缓存。

Even with a GlassPane the underlying component would have to be repainted at some point. There is not a nice way to avoid a paintComponent call to JPanel.

However, the JPanel should not be doing things in paintComponent other than painting. If you are trying to avoid calling it, then it sounds like you have something in the paintComponent method that needs to be changed or cached somehow.

紧拥背影 2024-11-15 20:58:59

您是否有不想在 JPanel 上调用 PaintComponent() 方法的原因?重新绘制对象以渲染在其上滑动的线很可能是最简单的解决方案。

Is there a reason why you do not want to call the paintComponent() method on the JPanel? Repainting the object to render the line gliding over it will most likely be the easiest solution.

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