小编Alc*_*hms的帖子

当许多精灵在屏幕上时,Java Swing游戏的性能很差

我正在Swing中制作一个简单的塔防游戏,当我尝试在屏幕上放置许多精灵(超过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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Target类simple在其当前位置绘制BufferedImage.getImage方法不会创建新的BufferedImage,它只返回Controller类的实例:

public void paintTarget(Graphics g){
    g.drawImage(con.getImage("target"), getPosition().x - 20, getPosition().y - 20, null);
}
Run Code Online (Sandbox Code Playgroud)

每个目标都运行一个Swing Timer来计算它的位置.这是它调用的ActionListener:

public void actionPerformed(ActionEvent e) {
    if (!waypointReached()){
        x += dx;
        y += dy; …
Run Code Online (Sandbox Code Playgroud)

java animation swing

3
推荐指数
1
解决办法
701
查看次数

标签 统计

animation ×1

java ×1

swing ×1