我正在 Swing 中制作一个简单的塔防游戏,当我尝试在屏幕上放置许多 Sprite (超过 20 个)时遇到了性能问题。
整个游戏发生在具有 setIgnoreRepaint(true) 的 JPanel 上。 这是 paintComponent 方法(con 是 Controller ):
public void paintComponent(Graphics g){
super.paintComponent(g);
//Draw grid
g.drawImage(background, 0, 0, null);
if (con != null){
//Draw towers
for (Tower t : con.getTowerList()){
t.paintTower(g);
}
//Draw targets
if (con.getTargets().size() != 0){
for (Target t : con.getTargets()){
t.paintTarget(g);
}
//Draw shots
for (Shot s : con.getShots()){
s.paintShot(g);
}
}
}
}
Target 类简单地在其当前位置绘制一个 BufferedImage。 getImage 方法不会创建新的 BufferedImage,它只是返回 Controller 类的实例:
public void paintTarget(Graphics g){
g.drawImage(con.getImage("target"), getPosition().x - 20, getPosition().y - 20, null);
}
每个目标都运行一个 Swing Timer 来计算其位置。这是它调用的 ActionListener:
public void actionPerformed(ActionEvent e) {
if (!waypointReached()){
x += dx;
y += dy;
con.repaintArea((int)x - 25, (int)y - 25, 50, 50);
}
else{
moving = false;
mover.stop();
}
}
private boolean waypointReached(){
return Math.abs(x - currentWaypoint.x) <= speed && Math.abs(y - currentWaypoint.y) <= speed;
}
除此之外,repaint() 仅在放置新塔时调用。
如何提高性能?
最佳答案
Each target runs a Swing Timer to calculate its position. This is the ActionListener it calls:
这可能是您的问题 - 让每个目标/子弹(我假设?)负责跟踪何时更新自身和绘制自身听起来像是一项相当大的工作。更常见的方法是沿着
while (gameIsRunning) {
int timeElapsed = timeSinceLastUpdate();
for (GameEntity e : entities) {
e.update(timeElapsed);
}
render(); // or simply repaint in your case, I guess
Thread.sleep(???); // You don't want to do this on the main Swing (EDT) thread though
}
本质上,链上游的对象有责任跟踪游戏中的所有实体,告诉它们更新自己并渲染它们。
关于当屏幕上有许多 Sprite 时,Java Swing 游戏性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11432773/