Java Buffer Strategy: compiler doesn't like it
I am attempting to use double buffering with a canvas, something I've never done before. I took advantage of the tutorials online, and set up the following code to instantiate a canvas and set up the buffering for it. I coded the complete process including the rendering graphiocs (not shown here), and the compiler accepts it.
volCanvas = new VolCanvas();
volCanvas.setBackground(Color.black);
volCanvas.setBounds(10, 380, 1180, 125);
add(volCanvas);
volCanvas.createBufferStrategy(2); (Program blows up here)
offScreen = volCanvas.getBufferStrategy();
ofsg = (Graphics2D) offScreen.getDrawGraphics();
But the program blows up at the flagged line in the code below. The runtime throws an illegal state ex开发者_Go百科ception, with the explanation "Component must have a valid peer".
So far as I can tell, the source code is essentially as I've seen it in several examples, so I haven't a clue what is going on here. Any help would be greatly appreciated.
Thanks,
John Doner
Component must have a valid peer".
It basically means your GUI is not visible or you haven't added your component to a visible GUI.
Your other questions on the forum deal with Swing applications. You should not use an AWT component (Canvas) in a Swing application. Do custom painting on a JComponent or JPanel. Swing is double buffered by default.
精彩评论