问题描述
我有两个 JPanel
我不知道如何说明这一点,所以请耐心等待。 > s在一个容器 JPanel
中加入一个 OverlayLayout
。容器中的 JPanel
s覆盖。
底部 JPanel
是不透明的,并且绘制了一些相当复杂的图形,因此需要很长的时间(10s或100s毫秒)才能呈现。
顶部 JPanel
是透明的,只需根据鼠标输入绘制一个矩形或线条或简单的形状,所以它非常快。
有没有办法设置,所以当我改变上面板中的简单形状时,它不会重绘底部面板?(例如,它以某种方式缓存底部面板)
我很熟悉像w /概念,如bitblt,双缓冲和异或绘图,但不知道该怎么做在这里申请。
您最好使用一个 JComponent
和cre注意 BufferedImage
来存储底部图像。当 paintComponent
操作发生在 JComponent
上时,您只需将底部图像粘贴并使用 Graphics
对象可以进一步绘制(从存储状态)。应该是相当高效的。
您需要为另一个线程底部的 BufferedImage
执行复杂的绘制操作,因为另一张海报提到(遗漏这个意外,对不起:))。但是,您不希望在此图像上引起争用,因此您必须为此另存 BufferedImage
,并在同一时间将其同步闪烁到另一图像绘图操作已完成。
I'm not quite sure how to phrase this, so bear with me.
I have two JPanel
s in a container JPanel
with an OverlayLayout
. Both JPanel
s in the container override paint(Graphics)
.
The bottom JPanel
is opaque and draws some fairly complicated graphics, so it takes a "long" time (10s or 100s of milliseconds) to render.
The top JPanel
is transparent and just draws a rectangle or line or simple shape based on mouse input, so it's really quick.
Is there a way to set things up so when I change the simple shape in the upper panel, it doesn't redraw the bottom panel? (e.g. it somehow caches the bottom panel)
I'm vaguely familiar w/ concepts like bitblt, double-buffering, and XOR-drawing but not really sure what to apply here.
You'd be best off using a single JComponent
and creating a BufferedImage
to store the bottom image. When the paintComponent
operation happens on the JComponent
, you just blit the bottom image and use the Graphics
object to do any further drawing on top of that (from a stored state). Should be fairly efficient.
You'll want to do the complex drawing operations for the bottom BufferedImage
in another thread, as the other poster mentions (omitted this by accident, sorry :)). However, you don't want to cause contention on this image, so you must store an additional BufferedImage
for this, and blit it synchronously to the other image at the very moment the drawing operations are complete on it.
这篇关于java + Swing:矩形或其他“精灵”的高效叠加。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!