java - BufferStrategy没有创建状态? (空指针异常) [英] java - BufferStrategy is not creating stategy? (nullpointerexception)

查看:216
本文介绍了java - BufferStrategy没有创建状态? (空指针异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定开始了解我的图形的BufferStrategy。
我不知道如果以静态形式使用我的jframe是什么原因,但它看起来对我好。我缺少什么?

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

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

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();
    }

}

我的Player.java只包含一个方法:

My Player.java only contains one method:

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

错误:

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)


推荐答案

在创建缓冲策略后,您不要尝试获取缓冲区策略:

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();

还要注意@MadProgrammer的观点,即缓冲策略属于不同的组件。如果您打算创建一个AWT游戏(我建议使用swing),那么您应该使用 Canvas 及其 createBufferStrategy()

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().

这篇关于java - BufferStrategy没有创建状态? (空指针异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆