BufferStrategy Illegal State Exception

╄→гoц情女王★ 提交于 2019-12-10 23:08:39

问题


After a few hours of trying to work this out I am stumped. I am somewhat new to Java and could use some help.

The Stack Trace:

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 com.mime.crystalnova.Display.render(Display.java:148)
at com.mime.crystalnova.Display.run(Display.java:112)
at java.lang.Thread.run(Unknown Source)

The error appears to be with this line of code here:

BufferStrategy bs = getBufferStrategy();
if (bs == null) {
    createBufferStrategy(3);
    return;
}

The odd thing is that it gives me this error while debugging and then continues to run flawlessly. It works fine when launched via runnable jar file as well.


回答1:


It's probably got to do with the component not being visible yet at the time you call createBufferStrategy() (it's been a while). Try calling it at a later time.

The fact that your app continues to run probably means the component defaulted to a non-buffered strategy.



来源:https://stackoverflow.com/questions/10158209/bufferstrategy-illegal-state-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!