创建新的Bufferstrategy时出现非法状态异常

时间:2012-06-02 22:02:16

标签: java buffer illegalstateexception

当我试图找出如何使用buffers策略时,总体上只是改进了我编写代码和清理内容的方法。当我运行以下代码时,我在“createBufferStrategy(3)”

时出错
    package Game1Test;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.io.IOException;

import javax.swing.*;

public class Base extends Canvas implements Runnable{

    private static final long serialVersionUID = 1L;
    private boolean running = false;
    int ticks = 0;

    public Base(JFrame f) {
        setSize(f.getWidth(),f.getHeight());
        start();
    }

    public void start(){
        running = true;
        new Thread(this).start();
    }
    public void stop(){

    }
    public void run(){
        while(running){
            ticks++;
            System.out.println(ticks);
            render();

                try {
                    Thread.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
    }
    public void render(){
        BufferStrategy bs = getBufferStrategy();
        Graphics g;
        if(bs == null){
            createBufferStrategy(3);
            requestFocus();
            return;
        }
        bs.show();
    }



}

然后添加基地:

package Game1Test;

import java.awt.*;

import javax.swing.JFrame;

public class Screen extends JFrame{

    public final int GAME_WIDTH = 400;
    public final int GAME_HEIGHT = 400;
    public Dimension gameDim = new Dimension(GAME_WIDTH,GAME_HEIGHT);
    final String gameName = "Test";

    public Screen(){
        setSize(gameDim);
        setTitle(gameName);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(new GridLayout());
        setVisible(true);
        setLocationRelativeTo(null);
    }
    public static void main(String[] args){
        Screen s = new Screen();
        s.add(new Base(s));
    }
}

我收到以下错误:

Exception in thread "Thread-2" java.lang.IllegalStateException: Component must have a valid peer
    at java.awt.Component$FlipBufferStrategy.createBuffers(Unknown Source)
    at java.awt.Component$FlipBufferStrategy.<init>(Unknown Source)
    at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Unknown Source)
    at java.awt.Component.createBufferStrategy(Unknown Source)
    at java.awt.Canvas.createBufferStrategy(Unknown Source)
    at java.awt.Component.createBufferStrategy(Unknown Source)
    at java.awt.Canvas.createBufferStrategy(Unknown Source)
    at Game1Test.Base.render(Base.java:46)
    at Game1Test.Base.run(Base.java:33)
    at java.lang.Thread.run(Unknown Source)

有人可以告诉我为什么会这样吗?也许是这个问题的解决方案?

由于

3 个答案:

答案 0 :(得分:8)

查看API,如果组件不可显示,则抛出此异常。在这种情况下,Canvas.peernull时。查看peer字段可以看出

  

Component添加到a​​时设置对等体   也是同行的容器

由于您是从组件的构造函数启动更新线程,因此可以在将组件添加到另一个容器之前调用render,这意味着peernull,然后<{1}}会被抛出。

答案 1 :(得分:3)

根据我对此错误的体验以及您编写的代码,您错过了一个框架功能。

添加框架的位置(例如:frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);)并添加函数frame.add(game);

在此示例中,我的是Display game = new Display();,但根据新显示的变量,它可能会有所不同。

答案 2 :(得分:0)

我有完全一样的例外,但我发现,这是因为我的JFrame知名度不小心被设置为false。

所以在调用setVisible(真)puting;修复它。