2

I am developing a shoot'em up type video game using Java Swing and I'm using a Java Swing Timer to control all of the screen updating. The main screen uses a BorderLayout, and I perform my graphics on a Panel contained within the BorderLayout, and use the AWT Graphics calls to draw.

Now as my game progresses, I would like to speed up the movement of my screen objects (ships), yet I still want them to smoothly cross the screen. I thought I could speeed things up by dropping the timeout value for the Java Swing Timer, down to around 5ms. But, I've noticed when I set it to anything less than 15ms, there does not seem to be much difference. Once you cross that threshold, there is almost no noticeable difference in performance. -Why is that?-

Another option would be to increase how many pixels each ship moves per update, but anything beyond 3 or 4 pixels, and things start to look jumpy.

Any ideas? And really want to keep the game in Swing, would prefer at this point not porting to a 3rd party library.

Thanks.

user1104028
  • 381
  • 7
  • 18
  • The problem would be the Operating System and on how accurate the clock is. – camickr Dec 19 '14 at 16:17
  • 2
    Your screen only refreshes so fast -- so if you have, say, a 60Hz screen, the minimum delay before it starts drawing multiple frames per update (and therefore only showing every few) is ~16ms. If you want, I'll put this as an answer. – Nic Dec 19 '14 at 16:19
  • Class is about to end, so I'm posting the answer now. – Nic Dec 19 '14 at 16:27
  • A *Swing Timer* has a *minimum resolution* of .. wait for it .. about 15ms on x86/x64 Windows (this may vary by OS/implementation). The only guarantee is that the Timer will delay *"at least"* the specified time. Ref http://stackoverflow.com/questions/1245388/timer-accuracy-in-java , https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks - There are additional issues with Swing and the rendering pipeline it uses that make it not-well-suited for "fast 2D" games. I recommend looking at a game library. – user2864740 Dec 19 '14 at 16:45
  • Anyway, if insisting on using Swing for a "fast 2D game", a simple approach is to keep requesting repaints and then in the repaint handle the motion (and the drawing to graphics) based on *delta time calculations*. This will run at the "maximum repaint speed" supported and the delta time calculations will ensure smoother / more consistent overall movement. – user2864740 Dec 19 '14 at 16:53
  • Please choose whichever answer helped you the most. – Nic Dec 20 '14 at 13:13

2 Answers2

1

In all likelihood, this isn't a software issue, nor is it fixable with software. Your screen probably only refreshes about 60 times a second, meaning that the frames are only drawn 60 times per second, or once every (approximately) 16 milliseconds. Since this is a hardware barrier, there's no way to get it to update faster with software. You can probably count on your users only having 60Hz monitors, too, so it's more worthwhile to look into other solutions.

One solution that pops to mind is adding in motion blur, to make it seem like the ships are moving faster when they really aren't. It'll allow you to 'jump' a greater distance before it looks jumpy, since the blur tricks the eye into thinking it's going really fast instead of hopping across the screen. Unfortunately, the only things that I see to do motion blur are third-party libraries, though you may have better luck Googling.

Nic
  • 6,211
  • 10
  • 46
  • 69
  • While the refresh rate (and delay) ultimately affects the user experience, the Swing Timer is *independent* of the monitor refresh rate. – user2864740 Dec 19 '14 at 16:45
  • @user2864740 That's interesting. Nonetheless, what I said is still true, and given that both are around the same time, it's a tossup as to which it actually is. – Nic Dec 19 '14 at 16:47
-1

I advise you that you change your Swing engine for JavaFX (at least), a technology with better performance and more tools to your disposal. Swing nowadays is considered a dead technology, as well as with the AWT before it. A great place to study and start JavaFX would be this one:

http://docs.oracle.com/javase/8/javase-clienttechnologies.htm

The ideal for the development of games, would be to use a library prepared for it, such as libGDX:

http://libgdx.badlogicgames.com/

But if your desire is to create a game in Swing, who am I to judge? Sometimes it is good to see old things. I myself still like Swing, even being obsolete compared to other things.

I think you may be implementing a wrong gameloop. Swing Timer does not really seem like a good way to update game logic. Even with different settings between different computers, is generally practical and easy to implement a gameloop to work properly, especially for the correct movement of the characters.

You see, a gameloop is the heart of a game, and needs to be implemented straight otherwise the game is developed wrong and with several limitations. Generally, in gameloop we have to take into account the desired FPS. Among game updates, we must take the time elapsed between the last update and the current update, so-called delta by many developers. You will find many materials about it on the internet, which I recommend:

http://dewitters.koonsolo.com/gameloop.html

http://www.java-gaming.org/index.php?topic=21919.0

https://www.youtube.com/watch?v=9dzhgsVaiSo

The past links will help you for sure, but if you want more about it, I recommend that you take a look at this book:

http://www.brackeen.com/javagamebook/

Even the material being old (as Swing is) you'll find details of how to implement a good gameloop with a keyboard and mouse iinput system, sound, the use of the best possible performance swing has to offer, among other cool things. And one more thing... To give you a better result with respect to the movement of the characters, I advice you to use position variables (X and Y) with decimal types such as float or double.

I hope you can do what you want. Good luck!

Loa
  • 2,117
  • 3
  • 21
  • 45
  • While I agree JavaFX is going to replace Swing, and everyone agrees Swing isn't a good choice for implementing an action game, it's highly inaccurate to call Swing a "dead technology." Swing is still vastly more feature complete. – VGR Dec 19 '14 at 20:31
  • Swing has many cool things, but unfortunately it is considered something of the past: http://www.oracle.com/technetwork/java/javafx/overview/faq-1446554.html (see item 6). See also: http://en.wikipedia.org/wiki/JavaFX It is well advised that developers migrate to JavaFX, it just offers much better things than Swing. When in doubt, please look for the JavaFX Ensemble demo and see for yourself: http://www.oracle.com/technetwork/java/javase/overview/javafx-samples-2158687.html My advice as someone who works in the market, is that people migrate as soon as possible to JavaFX. – Loa Dec 19 '14 at 22:21