Java - 双缓冲的空例外?

时间:2014-04-01 00:06:12

标签: java swing nullpointerexception null

我是游戏编程的新手,我正在学习如何使用双缓冲。当我尝试设置dbImage时,无论我做什么,它总会返回“null”错误。任何帮助将不胜感激!

GamePanel类:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class GamePanel extends JPanel implements Runnable
{
private final int WIDTH = 160;
private final int HEIGHT = WIDTH/12 *9;
private final int RATIO = 3;

private Thread animator;
private volatile boolean running;
private volatile boolean gameOver;

private double FPS = 60D;
private double period = 1000/FPS;

private Image dbImage = null;
private Graphics dbg;

public GamePanel()
{
    setPreferredSize(new Dimension(WIDTH *3, HEIGHT*3));
    setBackground(Color.white);
    setFocusable(true);
    requestFocus();
    terminate();
    setVisible(true);
}

public void addNotify()
{
    super.addNotify();
    startGame();
}

public void paintComponent(Graphics g)
{
    if (dbImage != null)
    {
        g.drawImage(dbImage, 0, 0, null);
    }
}

public void startGame()
{
    System.out.println("Thread started");
    animator = new Thread(new GamePanel());
    animator.start();
}

public void stopGame()
{
    System.out.println("Thread stopped");
    running = false;
}

public void run() 
{
    System.out.println("Running.");
    long beforeTime, timeDiff, sleepTime;
    beforeTime = System.currentTimeMillis();

    running = true;
    while (running)
    {
        gameUpdate();
        gameRender();
        paintScreen();

        timeDiff = System.currentTimeMillis() - beforeTime;
        sleepTime = (long) period - timeDiff;

        if(sleepTime <= 0)
            sleepTime = 5;

        try 
        {
            Thread.sleep(sleepTime);
        } 
        catch (InterruptedException e) 
        {
            e.printStackTrace();
        }
        beforeTime = System.currentTimeMillis();
    }
    System.exit(0);
}

public void gameRender() 
{
    if (dbImage == null)
    {
        dbImage = (Image)createImage(WIDTH, HEIGHT);
        if (dbImage == null)
        {
            System.out.println("dbImage is null");
            return;
        }
        else
            dbg = dbImage.getGraphics();
    }

    dbg.setColor(Color.white);
    dbg.fillRect(0, 0, WIDTH, HEIGHT);

    // Draw game elements
}


public void gameUpdate()
{

}

private void paintScreen()
{
    Graphics g;
    try
    {
        g = this.getGraphics();
        if ((g!= null) && (dbImage != null))
            g.drawImage(dbImage, 0, 0, null);
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }
    catch (Exception e)
    {
        System.out.println("Error: " + e.getMessage());
    }
}

public void terminate()
{
    addKeyListener (new KeyAdapter()
    {
        public void keyPressed(KeyEvent e)
        {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_ESCAPE)
            {
                running = false;
            }
        }
    });
}


}

JFrame类:

import javax.swing.JFrame;


public class GameFrame extends JFrame
{
private final int WIDTH = 160;
private final int HEIGHT = WIDTH/12*9;
private final int RATIO = 3;

public GameFrame()
{
    setTitle("User Input Game");
    setSize(WIDTH*3,HEIGHT*3);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    GamePanel gp = new GamePanel();
    add(gp);
    setVisible(true);
}
}

亚军类:

public class Main 
{

public static void main(String[] args) 
{
    GameFrame myFrame = new GameFrame();
}

}

打印出的错误是“Error:null”和“dbImage为null” 提前谢谢!

0 个答案:

没有答案