Intermittent black screen

I’ve got my application to the point where it is drawing two red triangles to my screen. So far, so good. Except that, about one in four times, it doesn’t draw - I get a black screen instead. This is without changing any code or anything - I start the program once, it draws fine; next time, black screen.

Now, it’s a rather large application, so I’m reluctant to make you all wade through the whole code. Can anyone think of some common newbie error that might cause this? If not, I’ll post the rendering code for you all to pick through. I am myself totally baffled; computers should be deterministic, curse it! ???

I had something like this happen recently aswell, it turns out that i was calling setSize() on the JFrame that contained the GLCanvas, instead of directly on the GLCanvas.
Presumably there’s some kind of crazy unpredictable thread scheduling deal going on when sizing windows

It sounds like a race condition between setVisible(true) and setSize(). Once your widget hierarchy has been realized via pack() or setVisible(true)/show() then you should only make modifications to it via either SwingUtilities.invokeAndWait/invokeLater() or EventQueue.invokeAndWait/invokeLater().

Ah, I found the problem! Thanks, people, you put me on the right track. I’ll describe the problem in case any other newbie can learn from it :

I have some initialisation code for my renderable objects; this code is called by the initialisation method of my RenderManager class, which in turn is called by my GLEventListener’s init method. But the latter is called somewhere in the constructor of my main GUI class, which sometimes occurs (threads, right enough) before my renderable objects have been registered with the RenderManager. Hence they never get initialised, with predictable results.

Hmm. While I found the problem, it wasn’t so easy to fix. I eventually kludged my way around it by checking in the render method of my renderables whether or not it is initialised. Clearly this is not on for the long term!

What I’d like to do is create my GUI object, and then wait until the GL context is ready to add and intialise my renderables. Unfortunately I can’t find a way to interrogate a GLDrawable as to whether it is ready or not. Does anyone have a good idea on this?

I have tried code of the form

 
while (theGLDrawable.getGL() != null) {}

but I still could not initialise my objects properly after this was complete. Specifically, when I ask for glGenLists, it returns 0.

It sounds to me like you should probably initialize your renderable objects in your GLEventListener’s init() method. You really shouldn’t depend on whether a GLDrawable is “ready to render” or not because in the general case it can be removed from and added back to the component hierarchy multiple times. If you have multiple threads doing work, you should probably put your objects in a queue that will be consumed during your next GLEventListener’s display().

[quote]It sounds to me like you should probably initialize your renderable objects in your GLEventListener’s init() method.
[/quote]
Ah, of course. The original problem was that I was doing just this, but the RenderManager class relied on the GUI class instance being constructed, and the init method got called in that GUI class constructor; hence I couldn’t guarantee the Renderables being registered by the time the init got called. The obvious solution is to separate rendering management from GUI, which is better design anyway; I guess I was to close to the problem. Also I will explicitly initialise the GUI class instance, instead of relying on a static instance.