我是使用java2D和java Graphics的新手,我在使用 java 时遇到了一些问题BufferStrategy
,我尝试解决但它不起作用,而创建BufferStrategy
它时会出现这样的错误......
Exception in thread "Thread-0" java.lang.IllegalStateException: Component must have a valid peer
at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:4006)
at java.awt.Component$FlipBufferStrategy.<init>(Component.java:3980)
at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Component.java:4503)
at java.awt.Component.createBufferStrategy(Component.java:3857)
at java.awt.Canvas.createBufferStrategy(Canvas.java:194)
at java.awt.Component.createBufferStrategy(Component.java:3781)
at java.awt.Canvas.createBufferStrategy(Canvas.java:169)
at code.Main.render(Main.java:84)
at code.Main.run(Main.java:31)
at java.lang.Thread.run(Thread.java:745)
BUILD SUCCESSFUL (total time: 24 seconds)
我在YouTube 教程中获得了这个项目的大部分代码,"at code.Main.render(Main.java:84)"
指向这个渲染方法
private void render()
{
bufferStrategy= display.getCanvas().getBufferStrategy(); //getting bufferstrategy
if(bufferStrategy==null)
{
display.getCanvas().createBufferStrategy(3);// creating bufferstrategy, output says error in this line
return;
}
g= bufferStrategy.getDrawGraphics();
g.fillRect(1,1,width,height);//draw a rectangle
bufferStrategy.show();//show all things (build-in method)
g.dispose();
}
第二个错误render()
是调用方法时
public void run() //override run to Runnable
{
initialize(); //create and initialize Display frame and canvas
while (runningStatues) //works when game is already running
{
tick(); //update variables, have no code here
render(); // here is contain error
}
stop();
}
我觉得这对我来说太复杂了,谢谢你的帮助:)