0

I decided to start understanding BufferStrategy for my graphics. I'm not sure if using my jframe in a static form is what's cause this, but it looks alright to me. What am I missing?

Main.java

package Main;

import java.awt.Toolkit;


public class Main implements Runnable {

    private Thread gameThread;
    private Game game;
    private boolean running = false;
    public static ClientFrame frame;
    public static Toolkit kit;
    public static int WIDTH = 300, HEIGHT = WIDTH*16/9, SCALE = 3;

    public Main() {
        game = new Game();
        frame = new ClientFrame(game);
        kit = frame.getToolkit();

        frame.setVisible(true);
        start();
    }
    public synchronized void start() {
        running = true;
        gameThread = new Thread(this);

        gameThread.start();
    }
    public synchronized void stop() {
        running = false;

        gameThread.interrupt();
    }

    public void run() {
        long startTime = System.nanoTime();
        double nanoSec = 1000000000/60;
        double delta = 0;

        while(running) {
            long currentTime = System.nanoTime();
            delta += (currentTime - startTime)/nanoSec;

            while(delta >= 1) {
                game.update();

                delta--;
            }
            game.render();

            startTime = currentTime;
        }

    }

    public static void main(String[] args) {
        new Main();
    }

}

Game.java

package Main;

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

import javax.swing.JPanel;

public class Game extends JPanel {

    Player player;

    int tileArea = 32;

    public Game() {
        player = new Player();
        setPreferredSize(new Dimension(Main.WIDTH*Main.SCALE, Main.HEIGHT*Main.SCALE));

    }

    public void update() {

    }

    public void render() {
        BufferStrategy bs = Main.frame.getBufferStrategy();
        if(bs == null)
            Main.frame.createBufferStrategy(3);

        Graphics g = bs.getDrawGraphics();

        player.paint(g);

        g.dispose();
        bs.show();
    }

}

My Player.java only contains one method:

public void paint(Graphics g) {
    g.fillRect(25, 25, 50, 50);
}

ERROR:

Exception in thread "Thread-2" java.lang.NullPointerException
    at Main.Game.render(Game.java:30)
    at Main.Main.run(Main.java:52)
    at java.lang.Thread.run(Unknown Source)
Vince
  • 14,470
  • 7
  • 39
  • 84
  • The stacktrace would be helpful. Specifically, at which line does the game throw the `NullPointerException`? – kiheru Sep 14 '13 at 11:34
  • Posted. When i try to initiate the graphics: `Graphics g = bs.getDrawGraphics();` – Vince Sep 14 '13 at 11:36
  • There's many reasons why this might happen, typically, the component hasn't being made displayable (ie shown on the screen). I'm also confused why you would extend from a `JPanel` and the create a `BufferStrategy` from another component and paint to it... – MadProgrammer Sep 14 '13 at 11:39
  • I never set my panel displayable before, and it worked fine. There is no `BufferStragey` for the panel. I know I could create the strategy in my main, but honestly, whats the difference? It would still be in the same place in my loop. The only difference I see would be that I would be doing `BufferStrategy bs = frame.getBufferStrategy` rather than doing it statically. – Vince Sep 14 '13 at 11:46

2 Answers2

0

You do not try to get the buffer strategy after you have created it:

BufferStrategy bs = Main.frame.getBufferStrategy();
if(bs == null)
    Main.frame.createBufferStrategy(3);

//  if bs was null before, it still is null
Graphics g = bs.getDrawGraphics();

Also note the point by @MadProgrammer that the buffer strategy belongs to a different component. If your intent is to create an AWT game (I'd recommend swing instead), you should probably use Canvas, and its createBufferStrategy().

kiheru
  • 6,588
  • 25
  • 31
  • I'm not completely sure what you mean. That looks exactly like the code I have up there (Game class, render method). I started using the Canvas class when learning this, but I prefer swing. Now that I look at this, I don't think using the `BufferStrategy` on the frame will have any effect, since I'm drawing on the panel. Maybe I should switch back to canvas.. Thanks for not being rude though :) – Vince Sep 14 '13 at 12:05
  • That is your code, I just added a comment line explaining the bug. If you prefer Swing, you should likely be using the Swing's native double buffering (ie. override `paintComponent()` for the panel and do the drawing there). More about painting in swing [here](http://docs.oracle.com/javase/tutorial/uiswing/painting/). – kiheru Sep 14 '13 at 12:08
  • Ah, I get what you mean now. Since I dont have brackets for my *if* statement, its gonna try to initialize the graphics anyways, even if it's null. What I didn't understand was why it wasnt creating the graphics. Thanks for the link, I should look more into swing painting :s – Vince Sep 14 '13 at 12:49
0

try replacing

    if(bs == null)
        Main.frame.createBufferStrategy(3);

with

    if (bs == null) {

        Main.frame.crcreateBufferStrategy(3);
        return;
    }
Qormix
  • 1
  • 3