1

I am new to Java Game Programming and I am having trouble with my game. I am making this for my class in school and for fun so I transfer my code from my computer at school and my computer at home.

Recently, when I run my program at home, the window flashes extremely fast and I cant tell what is going on. I use a buffer-strategy and have tested some values and nothing changes. When I run the code on my school computer, it runs fine.

I am wondering if my problem is in fact the buffer-strategy or maybe my Nvidia graphics card is doing something to my display.

I have also recorded my screen showing the "flickering" so you can see what I am talking about in this YouTube video.

Here is my Game class the uses the buffer-strategy.

public class Game implements Runnable{

    public int width, height;
    public String title;

    private Display display;

    private BufferStrategy bs;
    private Graphics g;

    private Thread thread;
    private boolean running = false;

    //States
    private State menuState;
    private State gameState;

    //Input
    private KeyManager keyManager;


    public Game(String title, int width, int height){

        this.title = title;
        this.width = width;
        this.height = height;
        keyManager = new KeyManager();

    }

    private void init(){

        display = new Display(title, width, height);
        display.getFrame().addKeyListener(keyManager);
        Assets.init();

        menuState = new MenuState(this);
        gameState = new GameState(this);
        State.setState(menuState);

    }


    private void update(){

        keyManager.update();

        if(State.getState() != null){
            State.getState().update();
        }

    }

    private void render(){

        bs = display.getCanvas().getBufferStrategy();

        if(bs == null){
            display.getCanvas().createBufferStrategy(1);
            return;
        }

        g = bs.getDrawGraphics();

        // Clear

        g.clearRect(0, 0, width, height);

        // Draw

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

        // End Draw

        bs.show();
        bs.dispose();

    }


    public void run() {

        init();

        int fps = 60;
        double timePerTick = 1000000000 / fps; // 1 second (in nanoseconds)
        double delta = 0;
        long now;
        long lastTime = System.nanoTime();
        long timer = 0;
        int ticks = 0;

        while (running){

            now = System.nanoTime();
            delta += (now - lastTime) / timePerTick;
            timer += now - lastTime;
            lastTime = now;

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

            if(timer >= 1000000000){

                System.out.println("Ticks and frames:" + ticks);
                ticks = 0;
                timer = 0;

            }
        }

        stop();

    }

    public synchronized void start(){

        if (running)
            return;
        running = true;
        thread = new Thread(this);
        thread.start(); // Call run()

    }

    public synchronized void stop(){

        if (!running)
            return;
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public KeyManager getKeyManager(){

        return keyManager;

    }

}
Patrick
  • 11
  • 2
  • You should give a [shorter](http://stackoverflow.com/help/mcve) example. Three could be many things wrong here, for example `State.getState()` could be `null` – DrYap Mar 04 '15 at 07:52
  • all of the code works because it runs properly on another computer. i just wanted to provide the necessary code with buffer-strategy but i guess i went overboard. – Patrick Mar 04 '15 at 17:07

0 Answers0