BufferStrategy doesnt like me ...

all i wanna try is to get bufferstrategy work. after many tries and always getting:

java.lang.IllegalStateException: Component must have a valid peer

i read in inet, that the component has to be visible and added first and then the bufferstrategy can be created.
didnt work for me.

next step was visiting the lovely tutorial site of mr kevglass.
even copy and pasting the canvas-class-constructor for my canvas class didnt work. same error.

so im sitting here, wining … :-[

i finally got it to work, but not as i wanna have it:

this is ok:


JFrame frame = new JFrame();
frame.setBounds(0, 0, 800, 600);
frame.setVisible(true);
frame.createBufferStrategy(2);
strategy = frame.getBufferStrategy();

but this is not:


class Test extends Canvas
{
      BufferStrategy strategy;

      public Test
      {
                    setVisible(true);
                    setBounds(0,0,800,600);
                    JFrame frame = new JFrame();
                  frame.setFocusable(true);
                  frame.requestFocus();
                    frame.getContentPane().add(this);
                    frame.pack();
                  frame.setVisible(true);
                    createBufferStrategy(2);
                  strategy = getBufferStrategy();
      }

}

The second piece doesn’t work because you’re trying to display a component (Canvas, really), which is not in a displayable hierarchy yet.

So you need to first create a top-level window (which could be a Frame or a Window, but we really recommend a Frame), add that canvas to that window (or to one of its children), make the window visible, and then you’ll be able to create a buffer strategy.

but this wont work either:


setBounds(0, 0, PANEL_WIDTH, PANEL_HEIGHT);
          setVisible(true);
          
          
          //******************************** 
          // initializing the frame 
          //********************************
          JFrame frame = new JFrame();
          frame.setBounds(0, 0, PANEL_WIDTH, PANEL_HEIGHT);
          frame.setIgnoreRepaint(true);
          frame.setResizable(false);
          frame.setFocusable(true);
          frame.requestFocus();
         
          
          JPanel panel = (JPanel) frame.getContentPane();
        panel.setPreferredSize(new java.awt.Dimension(PANEL_WIDTH, PANEL_HEIGHT));
        panel.setLayout(null);
        
        panel.add(this);
        frame.setVisible(true);
          
      
          createBufferStrategy(2);
          strategy = getBufferStrategy();
          

this is within the constructor of my class which extends canvas. i get the exception that there is no valid peer.
but i first created the frame, added the canvas, made it visible and then created the strategy …

My wild guess is, that within the constructor its peer is not a valid until constructor is completed. So you cannot put this code inside the constructor.

What you may try is put some basic initialization code to a costructor and a separate init method. then call:
myclass = new MyClass();
myclass.initRestStuff();

hmm, i mainly oriented myself on kevglass tutorial, there all is done at construction time
http://www.cokeandcode.com/info/showsrc/showsrc.php?src=…/spaceinvaders/org/newdawn/spaceinvaders/Game.java

so … telephone call for mr kevglass … ;D