Using Swing in game development.

I am relatively new to game development using Java. I have been developing games and studying game development for 2-3 months now.

I have always used Swing for my graphics (aka, the entire game is displayed on a JPanel, using a Graphics2D object). I had no trouble with this up until now.

Very recently, I came across a problem in my most recent project. Some method has problems with being called in consistent time intervals (sometimes it runs every 15-16 milliseconds, as it should, and sometimes it starts to run every 3000 (!) milliseconds).

I did some frustrating debugging and some research, and found out that the reason this happens is probably because I’m not handling Swing and threads right.

My entrie game loop runs inside the run() method of a thread (which is not the EDT). So I’m modifying Swing elements outside of the EDT all of the time. Obviously this was bound to cause problems.

When I found out this was the problem was, I thought:

“Hey, I’ll simply use SwingUtilities.invokeLater() in order to run the gameloop inside the EDT!”

But then I remembered, that just as it’s ‘forbidden’ to manipulate Swing elements from outside of the EDT, it’s also problematic to manipulate non-Swing objects from inside the EDT (I think… is this correct?).

If so, then I have no idea how to develop games in Java using Swing without running into weird problems.

My question is:

How can I develop games safely, using Swing? What would be good guidelines for me to be strict about in order to avoid problems involving Swing and threading? Any instructions every game developer who uses Swing should know?

This is very important for me to understand, since I really want to progress with game development using Java, but if I won’t understand this, I’m always going to run into weird problems and won’t be able to progress.

Thank you for your help

I don’t know if this is swing, but it has to do something with Swing or AWT dunno which one :smiley:

[quote]But then I remembered, that just as it’s ‘forbidden’ to manipulate Swing elements from outside of the EDT, it’s also problematic to manipulate non-Swing objects from inside the EDT (I think… is this correct?).
[/quote]
It is “problematic” because writing correct code that accesses shared state from more than one thread concurrently is tricky. Apart from that, it is doable.

It would help if you paste your code here. It is easier to help when we both look at the same code.

Assuming that you have one thread that runs your game loop that updates game state and second thread (EDT - event dispatching thread) that reads game state and draws a frame then simplest solution with proper synchronization would look like this.

public class MyGame {
  // game state goes here
  ....

  public synchronized void step() {
    // advance game state
    ....
  }
  public synchronized void draw(Graphics2d graphics) {
    // draw game state
    ....
  }
}

You should be calling draw() from EDT and step() from game loop thread.
‘synchronized’ keywords in method signatures make sure that at most one synchronized method in MyGame object will be executed at given time AND that each thread (when it enters synchronized method) will see all changes of game state made by the other thread.

If you really want to understand java concurrency you should read http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601. Apart from explaining every single bit of concurrency problems in java it has a whole section devoted to concurrency in GUI applications.
This is a book that every java programmer should read (just after “effective java”).

My advice is simple: Don’t re-invent the wheel.

Use an engine or library such as LibGDX or Slick. I’d recommend LibGDX. It takes just a few days to get used to it and it has pretty useful tools and has JBox2D integrated.

So yeah, just keep it simple and everything will go just fine.

LibGDX is not simple. LibGDX is a complex library.
LWJGL is simple. It is just a wrapper for OpenGL.

There is a difference between simplicity and ease-of-use.

@OP: No-one here is going to recommend using Swing/Java2D for games. It’s just not designed for it. As far as learning how to get things moving on a screen goes, it’s great, but making a games with it is not worth the struggle. So instead, everyone will suggest to you that you use LibGDX.

LibGDX is fine. It’s a library which eliminates having to write boilerplate OpenGL code, and it also includes lots of helpful libraries for gamedev. It also gives you the ability to (relatively) easily export to HTML5 and Android, if that’s what you’re looking for. Unfortunately, if you plan at any point in the future to move away from Java, or if LibGDX goes under like Slick2D did (it’s not very likely, but it is possible), then all your knowledge of LibGDX will be useless. Also there is the off-chance that you end up wanting to work on a team project that doesn’t use LibGDX.

So, I will tell you that there is the option of LWJGL. It is a library that wraps OpenGL functions so you can use them in Java. LibGDX in fact, is based on top of LWJGL (or JOGL, if you’d prefer that). Let me warn you that OpenGL is a very simple library, and so you will need to write lots of functionality yourself if you choose to go down that path. However, it is a very common standard and most general purpose programming languages have bindings for it. Also, many libraries are based on LWJGL (as mentioned before, including LibGDX), and so you can pick those up quite quickly as well if you can see what OpenGL calls are being made under the hood.

Then there is JOGL. It’s similar to LWJGL, in that it’s an OpenGL wrapper. The library design is somewhat different though. The majority of active people on this forum aren’t familiar with it, but there is another forum somewhere… Anyway, I’m mentioning this because if I don’t, a certain member on this forum may take offense. :persecutioncomplex:

There is also Slick2D, but we don’t recommend that anymore. In fact, the developer behind it (kevglass) has switched to LibGDX. Apparently it’s still being maintained, but it’s been a while since I’ve heard anything about it.

So there, you now know all the (common) options. You can choose based on what suits you.

I use a combination of Swing and Java2D when developing games. To be honest, I think the main weakness with Java2D that everyone on here is trying to have you avoid is that you have to do a lot of extra work in order to decently maintain the FPS. I wouldn’t say it is impossible to make games using this platform, I would say that it is challenging. If your goal is to make games only, then by all means, head for LWJGL/JOGL/LibGDX. If you want to understand the Java language better, then the answers to your questions are just ahead…

You can develop games safely by only using one thread to produce games. By far, this is the safest and quickest method for game development.

[quote]What would be good guidelines for me to be strict about in order to avoid problems involving Swing and threading?
[/quote]
I wrote a very small post on game threading that pretty much applies to all game applications…

Game Threading

[quote]Any instructions every game developer who uses Swing should know?
[/quote]
You definitely want to program simple games and simple systems. Java2D is quite effective for simple prototype game making, but once you need particle effects and millions of entities on the screen, you will encounter all sorts of frame rate issues. Java2D is very good for understanding the basic limitations of the language. As for 2D gaming, I can’t think of a game you can’t create using Java2D.

However, it gets to the point where you have to use so many tricks to keep it fast it is probably better off using LWJGL/JOGL/LibGDX. So, in short, Java2D is prefect for learning the language and getting simple games done. LWJGL/JOGL/LibGDX is far superior when it comes to dealing with crazy graphical effects, and can help for pushing into the Android and HTML platform. The only weakness is that you’ll have to “learn” longer, and it has a higher math level requirement that may go over people’s heads when designing.

Best of luck with whatever you decide. :point:

Hi

There is a library called JogAmp (JOGL + JOAL + JOCL), it’s a set of bindings, it has the same “role” than its main competitor which doesn’t contain only a binding for OpenGL as far as I know.

@AvivC As some other people said, keep it simple, single-threaded, don’t use invokeLater, maybe you should look at the source code of “Breaking the tower” (by Markus Persson alias Notch, the creator of Minecraft), it’s a nice small game based on Java2D. If you need a speed boost but still with Swing, use GLG2D with it, it’s a small library that provides a fully hardware accelerated OpenGL implementation of Graphics2D, it’s based on JOGL but you don’t have to understand OpenGL to use it. Slick2D is still maintained by davedes (I read that on its dedicated forum) but LibGDX seems to be a better choice for games, especially for 2D games.

I was just talking about the graphics side of things, and I have a bad habit of sometimes using JogAmp and JOGL when I’m referring to the same thing.

Always use single thread when developing Java2D. I’ve used Java2D extensively when I was learning but I prefer LWJGL now because of it’s flexibility. It allows me to take advantage of my graphics card and learn more and more interesting OpenGL.

If you want to continue with Java2D, you can check out my game-engine made using Java2D here.

Or if you want to try LWJGL, you can check out my tutorial series here.