i'm new using java2D and java Graphics i have some issue while using BufferStrategy
in java, i tried solve but it doesn't work, while creating a BufferStrategy
it gives errors like this...
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)
i'm getting most of my code for this project in YouTube tutorial, "at code.Main.render(Main.java:84)"
points to this render method
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();
}
and second error is when render()
method is called
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();
}
i think it's too complicate for me, thanks for help :)