创建新的Bufferstrategy时出现非法状态异常 [英] Illegal State Exception when creating new Bufferstrategy

查看:143
本文介绍了创建新的Bufferstrategy时出现非法状态异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)"

    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)

有人可以告诉我为什么这是发生了什么?也许是这个问题的解决方案?

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

谢谢

推荐答案

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

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


对等体是在组件被添加到
容器中,该容器也是同行

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

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

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

这篇关于创建新的Bufferstrategy时出现非法状态异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆