7

I've been designing a card game in Java on Windows. It runs really well on my laptop and a few others, but on a lot of other systems (even a few newer ones both Mac and Windows) the animation is incredibly slow.

I've found User Interface Toolkits for Java to be the best resource so far, but haven't been able to make a significant improvement. I'm using the AWT/Swing libraries.

Question:
Looking at my game, (<1.5Mb), how could it be that on some computers (of similar spec) the performance seems to be significantly less that what it is on my laptop? The entire app is event-driven and I've done most of the optimization that I reckon could be done given the implementation.

I have a feeling it is memory-related. I create (compatible) and then store all my images into an array at the start, and then reference them there.

Note: I decided to make this game so that I can learn and practice some new ideas, so I'm not just trying to share it - I'm really interested to find out what's going on here.

rtheunissen
  • 7,347
  • 5
  • 34
  • 65
  • Could you isolate a snippet and share it? Also, did you think of creating [compatible images](http://download.oracle.com/javase/6/docs/api/java/awt/GraphicsConfiguration.html#createCompatibleImage%28int,%20int%29) on every platform? – Rom1 Jul 21 '11 at 14:28
  • Correct me if I am wrong, but this code is called only once at game startup, to prepare the set of cards? Assuming startup is not the problem, where is the code that actually draws the cards within the game? – Rom1 Jul 22 '11 at 11:33

4 Answers4

5

On not all operating systems, the Java 2D rendering pipeline supports hardware acceleration by the GPU. It depends on the Java implementation that you're using.

One of the new features for Oracle's Java SE 7 implementation (which is coming out at the end of the month) is this: XRender pipeline for Java 2D which means that it will have much better performing 2D graphics on Linux.

For Windows, in Java SE 6 update 10 there were some improvements to make Java 2D perform better by using Direct3D hardware acceleration (source).

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Thanks for the source. I'm still quite new to most of this, but if software doesn't use hardware acceleration, would most of the work be done by the CPU and not the GPU? – rtheunissen Jul 21 '11 at 14:36
  • Yes, that means that everything is done in software running on the CPU instead of using the special hardware in the GPU to do things like copying the contents of windows, etc. – Jesper Jul 21 '11 at 14:38
  • It seems illogical to not port all rendering through the GPU then anyway? (Forgive some of these naive questions please). – rtheunissen Jul 21 '11 at 14:40
  • Yes, but you'd need to use special platform-specific APIs for it such as DirectX (on Windows), I guess it's easier, and at least more portable, to write plain code that does all the work on the CPU. – Jesper Jul 21 '11 at 14:54
  • Fair enough, I suppose developers assume that when you reach the stage when you really need GPU acceleration, you'd be venturing into such technologies anyway. Would it be safe to say that a 2000-line 2D Java 2D core app like my game shouldn't have performance issues on decent machines if coded well? – rtheunissen Jul 21 '11 at 14:59
  • I haven't looked at your game but Java 2D is quite capable, it should not have performance issues if you have a decent machine. – Jesper Jul 21 '11 at 20:15
3

For the Mac, you should try the system propeties and rendering hints described in this document, particularly the one that tells Java to use the native Quartz renderer rather than the Sun renderer.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
3

..why could it be that on some computers the performance seems to be at least half of what it is on my laptop?

Video drivers. I was involved in a thread on usenet a long while ago, where there was a phenomenal difference in rendering speed on machines with similar spec. Ultimately it was concluded that some machines were using outdated video drivers, and that was the root cause of the difference.

If that turns out to be the case here, your best bet is probably to do a rendering test. Check the FPS, and if it is too low, advise the user to update the drivers.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Okay on my mate's new Macbook Pro, it was incredibly slow on Mac OSX, but running the same .jar on his Windows 7 partition was perfectly smooth. And it's very new, he bought it a few months ago. Both running newest JRE updates. So I'm thinking the main issue here is how java operates on different OS's. Although I'm thinking I might do a test to check your suggestion out anyway. Thanks. – rtheunissen Jul 30 '11 at 13:22
1

Try popping it in a profiler and see what comes up. I'd do it on both windows and mac and compare where exactly you're getting the problem. JProfiler is my favorite, but YourKit is good as well. You can get a trial version for 30 days which should be plenty of time to figure this out.

It's probably graphics hardware related, hardware acceleration vs software, since there is such a difference between Mac and Windows noted on this thread.

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138