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)"
当我试图找出如何使用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();
}
}
The Base is then added with:
然后添加基地:
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
2 个解决方案
#1
7
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
看一下API,如果组件不可显示,则抛出此异常。在这种情况下,当Canvas.peer为null时。看一下同行领域就可以看出这一点
The peer is set when the
Component
is added to a container that also is a peer将Component添加到也是对等的容器时,将设置对等体
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.
由于您是从组件的构造函数启动更新线程,因此可以在将组件添加到另一个容器之前调用render,这意味着对等方为null,然后将抛出IllegalStateException。
#2
3
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();但是根据新显示器的变量,它可能会有所不同。
#1
7
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
看一下API,如果组件不可显示,则抛出此异常。在这种情况下,当Canvas.peer为null时。看一下同行领域就可以看出这一点
The peer is set when the
Component
is added to a container that also is a peer将Component添加到也是对等的容器时,将设置对等体
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.
由于您是从组件的构造函数启动更新线程,因此可以在将组件添加到另一个容器之前调用render,这意味着对等方为null,然后将抛出IllegalStateException。
#2
3
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();但是根据新显示器的变量,它可能会有所不同。