问题
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.
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.
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.
回答1:
My
Tiles
aren't any type ofJComponent
, they're just data. I call their container theMapPane
, which draws all of the tiles in itspaintComponent
.
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.
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.
来源:https://stackoverflow.com/questions/24481823/java-swing-heavy-slow-paintcomponent-any-advice