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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10866269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:53:34  来源:igfitidea点击:

Illegal State Exception when creating new Bufferstrategy

javabufferillegalstateexception

提问by matthewtory

When I am trying to figure out how to use bufferstrategies, and overall just improving how I write my code and cleaning things up. When I run the following code, I get an error when I "createBufferStrategy(3)"

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



}

The Base is then added with:

然后将 Base 添加为:

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

I get the following error:

我收到以下错误:

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)

Can someone please tell me why this is happening? and maybe a solution to this problem?

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

Thanks

谢谢

回答by Jeffrey

Taking a look at the API, this exception is thrown if the component is not displayable. In this case, that's when Canvas.peeris null. Taking a look at the peerfield reveals that

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

The peer is set when the Componentis added to a container that also is a peer

Component被添加到同时也是对等的容器时设置对等

Since you are starting the update thread from your component's constructor, rendercould be called before your component is ever added to another container which would mean the peeris null, and then an IllegalStateExceptionwould be thrown.

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

回答by Stanley Dharan

In my experience with this error and with the code you writing you missing a frame function.

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

Add where your frames are (ex: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);) and add the function frame.add(game);

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

In this example, mine is Display game = new Display();but depending on what your variable for the new display is, it might vary.

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

回答by Luke

I had exactly the same exception, but i found out that it was because my JFrame visibility was accidentally set to false.

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

so puting in setVisible(true); fixed it.

所以放入 setVisible(true); 解决它。