为什么我的java应用程序运行得这么慢

时间:2016-05-31 22:54:15

标签: java performance

所以我用java(eclipse)创建了这个游戏。这是一个简单的2-D游戏。我的mac可以运行许多更大的游戏,如星际争霸2或反恐精英,但我制作的游戏几乎不能以60 fps运行。我做错了吗?

编辑:多线程会在这里帮忙吗?感谢。

package LT.Main;



import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import LT.Display.Display;
import LT.Display.Image;
import LT.Display.Mouse;
import LT.Display.key;
import LT.Game.cam;
import LT.Game.gen;
import LT.Game.player;
import LT.States.State;
import LT.States.creditState;
import LT.States.gameState;
import LT.States.menuState;
import LT.States.optionState;



public class Game implements Runnable{
    private boolean running = false;
    private Thread thread;
    private BufferStrategy bs;
    private Graphics g;
    private Handler handler;
    private State menuState, gameState, creditState, optionState;
    private Display dp;
    private key key;
    private Mouse mouse;

    private String name;
    private int width, height;

    public Game(String name, int width, int height){
    this.name = name;
    this.width = width;
    this.height = height;
}

private void init(){
    key = new key();
    handler = new Handler(this);
    dp = new Display(name, width, height);
    dp.getFrame().addKeyListener(key);
    dp.getFrame().addMouseListener(mouse);
    Image.init();
    mouse = dp.getMouse();
    gameState = new gameState(handler);
    menuState = new menuState(handler);
    creditState = new creditState(handler);
    optionState = new optionState(handler);
    State.setState(gameState);
    State.getState().init();

}


private void update(){
    key.tick();
    mouse.tick();
    if(State.getState() != null){
        State.getState().tick();
    }
}

private void render(){
    bs = dp.getFrame().getBufferStrategy();
    if(bs == null){
        dp.getFrame().createBufferStrategy(2);
        return;
    }
    g = bs.getDrawGraphics();
    //Clear Screen
    g.clearRect(0, 0, width, height);
    //Draw Here!

    if(State.getState() != null)
        State.getState().render(g);

    //End Drawing!
    bs.show();
    g.dispose();

}


public void run() {
    init();

    int fps = 60;
    double timePerDate = 1000000000/fps;
    double delta = 0;
    long now;
    long lastTime = System.nanoTime();
    long timer = 0;
    int ticks = 0;

    while(running == true){
        now = System.nanoTime();
        delta += (now - lastTime)/timePerDate;
        timer+= now -lastTime;
        lastTime = now;

        if(delta>=1){
            update();
            render();
            ticks++;
            delta--;

        }
        if (timer >= 1000000000){
            System.out.println("FPS:" + ticks);
            ticks = 0;
            timer = 0;

        }

    }

}

public synchronized void start(){
    if(running){
        return;
    }
    thread = new Thread(this);
    running = true;
    thread.start();
}

public synchronized void stop(){
    if(running == false){
        return;
    }
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public key getKey(){
    return key;
}

public Mouse getMouse(){
    return mouse;
}

public int getWidth(){
    return width;
}

public int getHeight() {
    return height;
}

}

0 个答案:

没有答案