java + Swing:矩形或其他" sprite& quot;的有效叠加; [英] java+Swing: efficient overlay of rectangle or other "sprite"

查看:52
本文介绍了java + Swing:矩形或其他" sprite& quot;的有效叠加;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定该怎么说,所以请忍受.

我在具有 OverlayLayout 的容器 JPanel 中有两个 JPanel .容器中的两个 JPanel 都覆盖

顶部的 JPanel 是透明的,仅根据鼠标输入即可绘制矩形或直线或简单形状,因此速度非常快.

是否可以进行设置,以便在上面板中更改简单形状时不会重绘下面板?(例如,它以某种方式缓存了下面板)

我对bitblt,双缓冲和XOR绘图等概念有些陌生,但我不确定在此应用什么.

最好使用单个 JComponent 并创建一个 BufferedImage 来存储底部图像.当在 JComponent 上执行 paintComponent 操作时,您只需将底部图像变白并使用 Graphics 对象在该图像之上进行进一步绘制(从存储状态开始).应该相当有效.

您将要在另一个线程中对底部 BufferedImage 进行复杂的绘制操作,正如其他海报所提到的那样(偶然地省略了此操作,很抱歉:)).但是,您不希望在此图像上引起争用,因此必须为此存储一个附加的 BufferedImage ,并在完成绘制操作后立即将其与其他图像同步化为blit.

I'm not quite sure how to phrase this, so bear with me.

I have two JPanels in a container JPanel with an OverlayLayout. Both JPanels 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:矩形或其他" sprite& quot;的有效叠加;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆