本文介绍了Java Swing重/慢paintComponent - 任何建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个滚动的2D地图/平铺游戏。每个图块(存储为图块[21] [11] - 每个图总共231个图块)最多可包含21个值(存储为int [3] [7])。全屏时我一次看到大约8张地图。

I'm making a scrolling 2D map/tile based game. Each tile (stored as tile[21][11] - 231 tiles total per map) can contain up to 21 values (stored as int[3][7]). While full-screen I see about 8 maps at once.

平均来说,每张地图需要大约0.03秒来绘制(使用System.nanoTime找到)。问题是,只要屏幕上有多个地图,Swing事件轮询就会明显减慢。

On average, each map takes about 0.03 seconds to draw (found with System.nanoTime). The problem is that as soon as more than 1 map is on the screen, the Swing event polling noticeably slows down.

有没有解决方法呢?我无法一次绘制地图以将其保存为图像,因为它具有涉及移动演员的透明度,因此它变化太频繁。另外我不认为我可以在paintComponent中调用一个线程来绘制地图而不会出现故障,但我不是肯定的。

Is there any solution to this? I can't draw the map a single time to save it as an image because it has transparency involving moving actors, so it changes too frequently. Also I don't think I can call a thread in paintComponent to draw the maps without it glitching out, but I'm not positive.

推荐答案

同样, JTable 单元格只是在 JComponent ; 提到的flyweight模式仍然适用:目标是省略任何渲染不可见单元格的工作。 和以优化渲染为目的; 引用的 KineticModel 中检查了一些方法。

Likewise, JTable cells are just data rendered inside a JComponent; the flyweight pattern, mentioned here, is still applicable: the goal is to omit any effort to render non-visible cells. Profile and self-time with a view toward optimizing the rendering; some approaches are examined in the KineticModel cited here.

最好不需要缩放的 BufferedImage 。如果必须缩放,请尝试与插值类型相关的 RenderingHints 。如果合成太贵,可以使用; publish()它们可用, process()它们在EDT上,如图所示。

A BufferedImage that needs no scaling is best. If you must scale, experiment with RenderingHints related to interpolation type. If composition is too expensive, construct maps in the background using SwingWorker; publish() them as they become available and process() them on the EDT, as shown here.

这篇关于Java Swing重/慢paintComponent - 任何建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 14:16