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?

有帮助吗?

解决方案

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.

其他提示

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.

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top