javagraphicsrenderbufferstrategy

Java - how to create bufferedStrategy to jframe


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


Solution

  • Component must have a valid peer

    What this is (trying) to tell you is, you've tried to create a BufferStrategy before the underlying window has been realised on the screen - this means, attached to a native peer. In AWT/Swing, a window becomes realised when it is either first resized or made visible.

    So, as a generalised suggestion, make the window visible BEFORE you try creating the BufferStrategy

    I would, highly, recommend that you take a look at the JavaDocs for BufferStrategy as it has an excellent example of the recommended workflow, from which you can base your solution.

    I would also recommend having a look at the BufferStrategy and BufferCapabilities trail, as it contains a number of runnable examples which you can play around with

    I would also recommend NOT using the frame's BufferStrategy, but instead using a Canvas, which is added to the frame, this will prevent you from painting beneath the window's borders

    You might consider having a look at something like What is the correct way to use createBufferStrategy()? for a runnable example