0

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

1 Answers1

0

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • i read the links, i'm using canvas to create `BufferStrategy`, and my frame is created before creating `BufferStrategy`, it creates frame in `initialize()` before calling `render()` that creates `BufferStrategy` – mhamad arsalan Sep 07 '18 at 17:17
  • i would ask if there is an easier way to render a simple rectangle instead of using `BufferStrategy`? – mhamad arsalan Sep 07 '18 at 17:25
  • oh, i found the bug! in `Display` class(contains frame), i was trying create buffers before adding canvas to my `JFrame`, i guess you want to tell me this by >"make the window (visible) BEFORE you create the `BufferStrategy`", i thought you mean `frame.setVisible(true);`. thanks for your help, i wasn't understand till i read your answer in depth, really thank you =) – mhamad arsalan Sep 07 '18 at 20:34
  • @mhamadarsalan Yeah, I was suggesting `setVisible`, but so long as the window has been attached to a native peer it should work (`setVisible` and I think `setSize` trigger the creation of the native peer) – MadProgrammer Sep 07 '18 at 21:58