Placing a Swing JComponent inside an LWJGL Display

thinks to himself okay J0, this is the last time you begin a new thread in Newbie questions

Hi lads! And good afternoon! :smiley:

First a little background; I’m currently coding a game that will have an RPG part and a Puzzle part (and a few other surprises but I leave that to the featured page in which I will post about that game once I’m finished). I don’t know if this would help, but maybe you can think of it as a Pr Layton game, in which you were able to move around in town, and solve puzzles. It’s not exactly the same type of game, but you get my point.

Anyway, I already coded some time ago my Puzzle part, using Swing, which was the only graphical library I knew at that time. I more recently began coding the RPG part, but, since Swing was too weak to make that work, I switched to using LWJGL, based on the advice of some people around here :slight_smile:

So we can now say I have an LWJGL Display in which the RPG (as well as every other) part shows up pretty smoothly and looking nice to me. On the other side, I have my Puzzle JComponent, all alone in its Swing shell. Yup, that’s right, like a tiny, shy turtle. ::slight_smile:
Hence my question: how can I make my JComponent appear inside my Display? :persecutioncomplex: The Display is full screen and the JComponent was made adaptable so it should acclimate to a full screen state very easily, if that is of any help.

I searched a lot on the internet before coming here to ask how to do it, unfortunately it appears that a lot of people ask about trying to make LWJGL fit inside Swing, and not the opposite. Which is why I came to think that doing what I want to do is either completely dumb, or super easy to achieve. 8)

THANKS A LOT FOR ANY ANSWER ;D (woops capslock)

J0 :slight_smile:

Even though you sound like you ruled that option out I would still recommend you use LWJGL2’s AWTGLCanvas class to embed OpenGL into a Swing application, instead of using LWJGL2’s native Display.
With this you can show up that AWTGLCanvas whenever you are in your RPG world and simply show up your other JComponent whenever solving puzzles.
There is also the option to set a AWT Canvas as the Display’s parent, using Display.setParent().
This way the OpenGL context is created on the window handle of the supplied AWT Canvas, which you can also add to your JFrame, just like your Puzzle JComponent.

if you want to show a JComponent next to your gl-display, use what KaiHH said : make the gl-display a JComponent.

there is another nice advantage by using an AWT Canvas. it becomes a plain JComponent which means you can make your game run in “windowed-fullscreen” very easy :

you can remove the frame from the JFrame [icode]frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE)[/icode].

to make the frame overlap the taskbar you can modify the maximum bounds : [icode]frame.setMaximizedBounds( new Rectangle(0,0, width, height ) )[/icode] with [icode]width[/icode] and [icode]height[/icode] match the desktop resolution :

[icode]DisplayMode mode = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode()[/icode] holds those numbers.

if you want to show a JComponent over your gl-display, like any other gl-drawn element you can go down the rabbit hole and look into swing->gl look and feel implementations but that’s rather heavy and probably way more work than … just rewriting the old JComponent in pure GL.

o/

Thanks for your quick answers, @KaiHH and @basil_! ;D

That is exactly what I feared I would have to do :smiley:
(No particular reason for that, I just had a feeling I would, in the end, have to embed OpenGL into a JFrame, haha)

Well then, I’m off to coding that, after what I’ll have to have a look at how I want my battles made :wink:

Farewell!

J0 :slight_smile:

Problem solved! :smiley:
Well, the GL Display is maybe a little slower when it has an AWT Canvas as parent, but that is not a big deal :wink: As long as it runs smoothly! :stuck_out_tongue:

Thanks again!

J0

Sounds like an answer has already been found and is satisfying to the OP.

But I am wondering, would another possible answer be to use JavaFX? Converting a Swing component to a JavaFX correlate is usually very easy. And JavaFX implements many (what percentage?) of the elements of LWJGL, e.g., things like shaders, 3D, etc. I don’t know how difficult it would be to convert existing code using LWJGL to JavaFX.

Lastly, I don’t know if there would be a performance hit or not. But at least the components and RPG elements should play together nicely. How is this as a theoretical solution? I’m just wondering about JavaFX as an option.

Short answer: JavaFX and OpenGL bindings don’t currently play very efficiently together. It’s a crying shame.

Cas :slight_smile:

The JavaFX 3D Graphics API is closer to Java3D than any set of Java binding for the OpenGL/OpenGL ES API, I wouldn’t compare them, they don’t have the same role. A scenegraph API usually uses another API to access OpenGL instead of reinventing the wheel.

Which bindings? Spasi already posted an interesting demo, a contributor has spent a lot of time in implementing an hardware accelerated pipeline for OpenJFX/JavaFX based on JOGL 2 and the main maintainer of a scenegraph API based on JOGL showed us a smart example of interoperability between JOGL and OpenJFX/JavaFX. It will become more straightforward when Oracle releases an equivalent of JAWT for OpenJFX/JavaFX.

[quote=“gouessej,post:8,topic:55759”]
We moved to a new home and will be without internet for a week or two. So, I got bored and decided to try this approach:

The JavaFX Ensemble application running on top of LWJGL without any custom native code.

Good job, I’m impatient to see your source code.