A way to draw a Swing hierarchy over an external OpenGL context

This is not a question, because I’ve done it and it’s working now :wink: I’m drawing an OpenGL scene (what you want, it might even be a 3D scene) in a C program, then I’m adding an overlay containing a Swing hierarchy over it (the JVM is created through this C program). There are many little tricks there, but to be quick:

  • The Swing widgets hierarchy is painted in an OpenGL texture (by a slightly modified TextureRenderer class), which is in turn painted over the OpenGL context
  • It’s better to set the ortho before painting, to be sure that you draw in a 2D world, over the 3D scene.
  • To be able to paint the widgets, they have to be in a JFrame or JWindow, so you have to create this container and add your Swing hierarchy in it, even if you will not ultimately use it. if you don’t do it, they will not have peers at the end, and Swing will decide that nothing has to be painted ;

It would have saved me a lot of time to have these informations, so I think I will blog about this soon to help others :wink:

Nice method, i was wondering about the performance of such a mix?

seems not too slow to me, but I need to bench it a little before answering your question :wink: