Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Using Swing in game development.  (Read 487 times)
0 Members and 1 Guest are viewing this topic.
Offline AvivC

Junior Devvie


Projects: 1



« Posted 2014-02-08 17:47:17 »

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
Offline trollwarrior1
« Reply #1 - Posted 2014-02-08 17:49:40 »

I don't know if this is swing, but it has to do something with Swing or AWT dunno which one Cheesy
http://www.youtube.com/watch?v=GFYT7Lqt1h8
Offline mikoch
« Reply #2 - Posted 2014-02-08 18:57:00 »

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?).

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
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").

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pploco1996

Junior Devvie


Medals: 2
Exp: 1 year



« Reply #3 - Posted 2014-02-08 22:28:12 »

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.
Offline HeroesGraveDev

JGO Kernel


Medals: 292
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #4 - Posted 2014-02-08 23:10:22 »

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.

Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #5 - Posted 2014-02-09 00:08:55 »

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...

My question is:
How can I develop games safely, using Swing?

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?


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?

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.  Pointing

Offline gouessej
« Reply #6 - Posted 2014-02-09 00:31:41 »

Hi

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 a library called JogAmp (JOGL + JOAL + JOCL), it's a set of bindings, it has the same "role" than its main competitor (LWJGL) 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.

Offline HeroesGraveDev

JGO Kernel


Medals: 292
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #7 - Posted 2014-02-09 00:37:37 »

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.

Online SHC
« Reply #8 - Posted 2014-02-09 03:48:03 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!