2
\$\begingroup\$

[summary: Is the performance of Swing at least in the same dimension as openGL [through LWJGL]? If yes, what percentage would you give it? What possible other problems can I expect from Swing in the long run, that I would avoid with openGL?]


So for quite some time I wanted to make my own simple java 2D game engine.

On my first try I was following a tutorial that used openGL (through LWJGL) Because said tutorial wasn't complete I was kind of lost at the end, which caused me to use code that was so inefficient that a simple pong game crashed after 10 seconds because the jvm ran out of memory.

On my second try I, quite successfully, used Swing to render to a jPanel, which significantly reduced the complexity involved (Why can't lwjgl have ANYTHING similar to JPanel.render(Image)?) and allowed me to create some simple games that so far don't seem to take up excessive memory / cpu.

Now my question is whether this could bite me in the ass in the long run, or if it eventually only comes down to a negligible performance hit that I would trade for a more intutive way to code. (Note that I don't care about 3D for now)

\$\endgroup\$
  • \$\begingroup\$ You should also consider JavaFX as a third option. \$\endgroup\$ – msell Jan 26 '15 at 11:15
3
\$\begingroup\$

OpenGL performance will outperform Swing quite heavily once you start doing something a little more advanced. I dont have hard numbers to back my claim, but I have experience. I too started out with pure Java2D, with BufferStrategy and the works. And it was ok for Pong, Snake, Tetris, and so on. But once venturing beyond in graphical effects (blending, lighting as examples), Java2D code started having both performance issues, but more pointedly it's just hard to do correctly.

If you think LWJGL is hard to learn, and you're not really interesting in learning all the gritty bits of OpenGL from the start, I recommend looking into Slick2D which is buildt on LWJGL, but made to create a more Java2D'ish experience. There is also LibGDX which has been gaining traction lately, and is a good alternative if you're also diving into the mobile world.

\$\endgroup\$
1
\$\begingroup\$

Like @Terje says, the short answer is "Yes, OpenGL can outperform Swing."

You've seen modern video games, you know current graphics hardware can do truly amazing things.

Pragmatically, what you can do is look at the CPU load of your current "Pong" game, or whatever it is. Based on the CPU percentage, and what your ultimate goal is, maybe Swing is just fine. If Pong takes 3% of the CPU, and your final intended game is equal to "Twenty pongs", then you're good to go.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.