8

当我试图弄清楚如何使用缓冲策略时,总体上只是改进我编写代码和清理东西的方式。当我运行以下代码时,当我“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)

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

谢谢

4

3 回答 3

11

查看API,如果组件不可显示,则会引发此异常。在这种情况下,那是什么Canvas.peer时候null。看看这个peer领域就会发现

Component添加到同样是对等点的容器时设置对等点

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

于 2012-06-02T22:22:57.053 回答
2

根据我对这个错误的经验以及您编写的代码,您缺少一个框架函数。

添加框架所在的位置(例如:)frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);并添加功能frame.add(game);

在这个例子中,我的是,Display game = new Display();但取决于新显示器的变量是什么,它可能会有所不同。

于 2012-07-14T00:47:17.033 回答
0

我有完全相同的异常,但我发现这是因为我的 JFrame 可见性被意外设置为 false。

所以放入 setVisible(true); 修复。

于 2019-02-02T16:37:14.327 回答