1

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 exception, 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

John R Doner
  • 2,242
  • 8
  • 30
  • 36

1 Answers1

1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I moved the last 3 lines of code out of the panel constructor, and now it works. Incidentally, what is wrong with using AWT components with Swing? I've never had any troubles with other programs where I've done it. – John R Doner Nov 29 '10 at 20:46
  • It only works in the latest release (or 2) of the JDK: http://java.sun.com/developer/technicalArticles/GUI/mixing_components/. Also, custom painting is done differently in Swing than in AWT so learn the proper way for Swing and don't mix the two. Tere is no need to use a Canvas. – camickr Nov 29 '10 at 20:51