在 JPanel 上绘制线条而不重新绘制它
我想在 JPanel
上绘制一条垂直线,并使其在其上滑动,而无需此过程调用 JPanel
的 paintComponent()
代码>.我曾考虑过使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只是想隔离,也许您应该使用 分层窗格其余绘画代码中的线条绘画代码。
如果您的绘画代码很昂贵,那么也许您应该创建一个 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.
即使使用 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 thepaintComponent
method that needs to be changed or cached somehow.您是否有不想在 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.