Swing Performance

First blood! :)

I'm planning on developing a visual engine for a 2D real-time strategy game. How much overhead does Swing incur? Is it practical for Swing to deal with a couple hundred Components visible while still maintaining a decent framerate?

Please let me know if you have any experience with the performance of Swing/AWT in this manifest.

Thanks!

[386 byte] By [tvynr] at [2007-9-27 13:53:15]
# 1
let me get this straight, you are going to write a game using swing components?
Abuse at 2007-7-5 21:42:21 > top of Java-index,Other Topics,Java Game Development...
# 2

A couple hundred Swing components may be pushing it for a game. What's the reason for it? I've used a JPanel to draw on instead of a Canvas or Panel, I'm no expert on the subject, but that's not recommended, correct? But I'll say that I've seen many successful games using Swing components. Of course when programming applets, generally, stay away from Swing, otherwise I'm not too sure on the subject either.

Ceranith at 2007-7-5 21:42:21 > top of Java-index,Other Topics,Java Game Development...
# 3

*nods* Understood. I'm aware of the draw-on-the-Panel technique, but it'd be nice (and a lot easier) if I could afford the overhead that Swing needs and take advantage of its listener system. In a top-down action game, for example, hovering the mouse over the target and getting statistics relating to the game object could be useful.

I'll have to take a closer look. Thanks. ;)

tvynr at 2007-7-5 21:42:21 > top of Java-index,Other Topics,Java Game Development...
# 4
does cutting the legs off a horse make it run slower?
Abuse at 2007-7-5 21:42:21 > top of Java-index,Other Topics,Java Game Development...
# 5

Where it's going to slow way down is when things have to be re-laidout. We have a pretty busy Swing GUI in our application with a JTree, JTable and lots of JPanels with various context specific stuff. It chokes when the JTree and JTable both need to be completely redrawn due to tons of data coming in all at once. As long as your just changing colors, dealing with mouse clicks, etc, Swing can easily handle 100s of components, in my experience.

beggarstune at 2007-7-5 21:42:21 > top of Java-index,Other Topics,Java Game Development...
# 6
I'd suggest not using listeners on every game object. That's very slow because of all the event queues. Just listen on the main panel and check whether the mouse was above any game object.
Matei at 2007-7-5 21:42:21 > top of Java-index,Other Topics,Java Game Development...
# 7
Ceranith, why using a JPanel is not recommended?For my game I've used it and it worked fine. Should I use Panel instead? http://www.geocities.com/ddmusc/SpaceCommanders -- the game where I have used JPanelDaniel
Rabbi at 2007-7-5 21:42:21 > top of Java-index,Other Topics,Java Game Development...
# 8

> Ceranith,

> why using a JPanel is not recommended?

> For my game I've used it and it worked fine. Should I

> use Panel instead?

> http://www.geocities.com/ddmusc/SpaceCommanders --

> the game where I have used JPanel

>

> Daniel

I remember reading this somewhere, but I was sort of asking the question if it was ok or not. It doesn't mean that you shouldn't use JPanel, even I did the same and it works fine. I'll try and find where I read that.

Ceranith at 2007-7-5 21:42:21 > top of Java-index,Other Topics,Java Game Development...
# 9

Just to let you know, Swing is is part of the JFC, which contains alot more than just components. It has Accessability APIs and loads of other stuff. If your making a 2d engine of what ever, use Java 2d. Their is an example that comes with JDK1.2 that demonstrates java 2d. it is probably with 1.3+ . It is pretty cool.

javatypo at 2007-7-5 21:42:21 > top of Java-index,Other Topics,Java Game Development...
# 10

> Just to let you know, Swing is is part of the JFC,

> which contains alot more than just components. It has

> Accessability APIs and loads of other stuff. If your

> making a 2d engine of what ever, use Java 2d. Their is

> an example that comes with JDK1.2 that demonstrates

> java 2d. it is probably with 1.3+ . It is pretty cool.

Guess what that example draws on... JPanel ;)

Ceranith at 2007-7-5 21:42:22 > top of Java-index,Other Topics,Java Game Development...
# 11
hey, I tried... (lol)
javatypo at 2007-7-5 21:42:22 > top of Java-index,Other Topics,Java Game Development...
# 12
> Of course when programming applets, generally, stay away from Swing,> otherwise I'm not too sure on the subject either.Why shouldn't Swing be used for applets?
Jleaf at 2007-7-5 21:42:22 > top of Java-index,Other Topics,Java Game Development...
# 13

Because most people who go looking for online video games are really stupid. Trust me there are a whole lot of grade 7s last year who stopped at nothing to play the most retarded games at lunch. So, when they come accrss something that uses Swing online, they #1 Don't have the Java Plug-in

and 2 don't waste their time trying to get it. They will probably just their reloading until it works which it never will, so they end up going to find a pile of crappy shockwave games.

javatypo at 2007-7-5 21:42:22 > top of Java-index,Other Topics,Java Game Development...
# 14
hehe - they would be the potencial market for more phone games ;]
Abuse at 2007-7-5 21:42:22 > top of Java-index,Other Topics,Java Game Development...
# 15
******!*correction* more==mobile
Abusea at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 16
I see, so it's more true in the context of games.
Jleafa at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 17

Yes. Until Sun finds a way to distribute the Java Plug-in without normal computer users, (people who know how to, surf, download mp3s and porn(lol), etc) having to go to some java technology site and download the Java Plug-in when the 1) don't really know what the hell Java is, and 2) have to find the download throughout all the other **** that is in there.

Man 2day I'm really making sense. hehe

But after everyone who has the internet knows about the java plug-in, then Sun should work on making sure everyone has an up2date version of it.

One way , if a Sun employee reads this, that Sun could easily distribute the java plug-in is that, say someone without it tries to use a Swing applet online, one of those little popup things comes up and says "You need to download the Java Plug-in 1.3"

That was a little of the topic of this topic but hey...

javatypoa at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 18
In general, stay away from multithreading and such. It's baaad. Using listeners uses multithreading. Basically, you want to maintain arrays of all your game objects and have one big loop iterating through them, getting user input, and processing logic based on input and/or object states.
St0rma at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 19
if only java gave you getKeyState ;]
Abusea at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 20
listeners are not bad at all - and they certainly dont involve multi-threading!
Abusea at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 21

> In general, stay away from multithreading and such.

> It's baaad. Using listeners uses multithreading.

> Basically, you want to maintain arrays of all your

> game objects and have one big loop iterating through

> them, getting user input, and processing logic based

> on input and/or object states.

WTF? why is multithreading bad? I haven't done much graphical stuff but for my many network MUDS I have many threads:

o The server accepting connections

o Each player reading/writing their actions

o Each mob

o Weather

o Some other stuff I can't remember

on the client:

o read from network and act upon read commands

o read from GUI input and send commands

I find with network IO stuff multithreading is a must, else the game hangs when the socket is open and OK but nothing is read.

I agree with iterating through arrays on a smaller scale though, eg each player object takes care of moving it's own projectiles etc

===============================

Also getting back to the topic I mostly use custom components extending java.awt.Canvas (the lowest useful graphical component), but beta versions of things often use AWT to get the main engine working.

For overlaying stuff with transparency I have an array of drawable objects, sort them into z-order then call each's paint(Graphics) method onto a Buffered/VolatileImage

Shish

====================

Support the Water Cooler!

shishthemoomina at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 22
> if only java gave you getKeyState ;]what is that? Or what does it do? If I guess correctly, you can do it easily.
Kayamana at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 23

> > if only java gave you getKeyState ;]

>

> what is that? Or what does it do? If I guess

> correctly, you can do it easily.

its a basic windows api function, you can use it to scan particular keys to find their state, and yeah, u could write the equivalent feature in java - infact, in a way you do (if your keyPressed event sets a flag, and the flag is later checked in the game loop, this is essentially the same functionality)

and answer to the above... multi threading is only applicable when operating with slow external devices, networking, file writing etc etc.

in the game loop itself there should be no multi threading, as it generally requires more synchronisation (which is slow) and task swapping on the processor (which is wastful)

rob,

Abusea at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 24
> if only java gave you getKeyState ;]It's not that difficult to implement getStaeKey() . If any one wants I can explain how I've implemented my one getStateKey() function
Rabbia at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 25

> > if only java gave you getKeyState ;]

>

> It's not that difficult to implement getStaeKey() . If

> any one wants I can explain how I've implemented my

> one getStateKey() function

But you can't write a getKeyState() equivalent, that doesn't go through the event dispatcher.

Thats the arguement for having a getKeyState type method - if we had such a method, games could disable the event dispatch altogether, and simply poll the keyState registry directly.

This would be far more efficient.

rob,

Abusea at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 26
My assumption would be don't use Swing. Use the VolotileImage API, as it is hardware accelerated, which you need for most games these days.
javanovaa at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 27
*may be* hardware accelerated :!but yeah, i agree - if u want an instant speed boost, use VolatileImage.
Abusea at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 28

> *may be* hardware accelerated :!

>

> but yeah, i agree - if u want an instant speed boost,

> use VolatileImage.

>

yep, *may be* hardware accelerated on windows, *almost definately won't be* on linux or solaris.

but, yeah like you say - its probably still your best bet if you have a win32 target audience.

gusgormana at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 29

> Because most people who go looking for online video

> games are really stupid. Trust me there are a whole

> lot of grade 7s last year who stopped at nothing to

> play the most retarded games at lunch. So, when they

> come accrss something that uses Swing online, they #1

> Don't have the Java Plug-in

>

> and 2 don't waste their time trying to get it. They

> will probably just their reloading until it works

> which it never will, so they end up going to find a

> pile of crappy shockwave games.

What are you talking about? It takes 2 seconds to get the Java plug-in, and in most cases is automatically downloaded and installed for you, you just click one button. And by the way, most web browsers come with the JRE. Systems are built like this BECAUSE OF STUPID PEOPLE. And, if the person isn't on Windows, using something like Unix or Linux, well then that person is probably smart enough to install Java themself.

javanovaa at 2007-7-18 11:29:38 > top of Java-index,Other Topics,Java Game Development...
# 30
if your clever enough to install the JRE plugin - your also clever enough to not use Applets in the 1st place.hence, Applets using 1.2+ JREs are pointless!I luv my logic ^_^rob,
Abusea at 2007-7-18 11:29:43 > top of Java-index,Other Topics,Java Game Development...
# 31
yeah, and now that they have disposable cell phones, what's next, disposable gameboy?!?!
javanovaa at 2007-7-18 11:29:43 > top of Java-index,Other Topics,Java Game Development...
# 32
disposal of the gameboy [advance] sounds like a move in the right direction :Pthen get rid of the game cube....might eventually lead to the death of the console - we can only hope....
Abusea at 2007-7-18 11:29:43 > top of Java-index,Other Topics,Java Game Development...