Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java-BufferStrategy没有创建策略?(nullpointerexception)_Java_Data Structures_Graphics_Bufferstrategy - Fatal编程技术网

java-BufferStrategy没有创建策略?(nullpointerexception)

java-BufferStrategy没有创建策略?(nullpointerexception),java,data-structures,graphics,bufferstrategy,Java,Data Structures,Graphics,Bufferstrategy,我决定开始了解我的图形的缓冲策略。 我不确定在静态形式中使用jframe是否是导致这种情况的原因,但我觉得这没什么问题。我错过了什么 Main.java package Main; import java.awt.Toolkit; public class Main implements Runnable { private Thread gameThread; private Game game; private boolean running = false;

我决定开始了解我的图形的缓冲策略。 我不确定在静态形式中使用jframe是否是导致这种情况的原因,但我觉得这没什么问题。我错过了什么

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仅包含一个方法:

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)

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

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),那么您可能应该使用,并尝试替换它的
createBufferStrategy()

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


stacktrace会很有帮助。具体来说,游戏在哪一行抛出
NullPointerException
。当我尝试启动图形时:
graphics g=bs.getDrawGraphics()出现这种情况的原因有很多,通常情况下,组件没有显示出来(即显示在屏幕上)。我也很困惑,为什么你要从一个
JPanel
扩展,从另一个组件创建一个
BufferStrategy
,然后在它上面画画……我以前从来没有将我的面板设置为可显示的,它工作得很好。面板没有
BufferStragey
。我知道我可以在我的主要战略,但老实说,有什么区别?它仍然会在我的循环中的相同位置。我所看到的唯一区别是,我将执行
BufferStrategy bs=frame.getBufferStrategy
,而不是静态执行。我不完全确定您的意思。这看起来就像我上面的代码(游戏类,渲染方法)。我在学习这个时开始使用Canvas类,但我更喜欢swing。现在我看了这个,我不认为在框架上使用
BufferStrategy
会有任何效果,因为我正在面板上绘图。也许我应该换回画布。。谢谢你没有粗鲁:)这是你的代码,我刚刚添加了一行注释来解释这个bug。如果您更喜欢Swing,则可能应该使用Swing的本机双缓冲(即,为面板覆盖
paintComponent()
,并在那里绘制)。更多关于秋千上的绘画。啊,我现在明白你的意思了。因为我的if语句没有括号,所以无论如何它都会尝试初始化图形,即使它为null。我不明白的是为什么它没有创造图形。谢谢你的链接,我应该多看看swing绘画:s
    if (bs == null) {

        Main.frame.crcreateBufferStrategy(3);
        return;
    }