Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (763)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
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  
  Swing in Game  (Read 4093 times)
0 Members and 1 Guest are viewing this topic.
Offline Juriy

Junior Devvie





« Posted 2007-08-30 11:30:39 »

Hi, I've got a question about usage of Swing controls in a game.

For example, if I need several buttons in main menu I have two options: to implement my own custom control and program it's behavior or to use swing implementation with some customization.

Second way seems to be better, since I do not have to reinvent the wheel, but I see that most games use first approach. My question is: why? Are there any traps or pitfalls that I can fall into if I'll go that way?

Actually, it is possible to implement quite nice UI with swing, still nobody does this in the game dev.

http://voituk.kiev.ua - java tutorials, tips and tricks (Russian)
Offline CommanderKeith
« Reply #1 - Posted 2007-08-30 15:25:42 »

Hi,

Yes, I see everyone using their own custom UI as well, so we're all re-inventing the wheel all the time!

The problem is that Swing has a threading model that sucks.  See this thread for a way to use Swing in an active-rendering game: http://www.java-gaming.org/forums/index.php?topic=15140.0

The other way is to only paint and update swing components in Swing's Event Dispatch Thread using SwingUtilities.invokeAndWait(...).

Best wishes,  Smiley
Keith

Offline askoruli

Senior Newbie





« Reply #2 - Posted 2007-08-31 00:38:17 »

I had a look at using swing to do the UI for the last game I was developing. In the end I gave up and created my own UI. Since I don't really need all the features that swing has and I only use simple components it took less time to build my own than figure out how to make swing do what I wanted. I've reused the same UI in 3 games now so it was worth the time spent developing it.

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

« JGO Spiffy Duke »


Medals: 35
Projects: 11


Age of Conquest makes your day!


« Reply #3 - Posted 2007-08-31 06:58:29 »

I am not getting all the hype  Huh

I am using the code sample by Andrew Davison:
http://fivedots.coe.psu.ac.th/~ad/jg/ch1/index.html

Seems to work quite alright or am I missing the point?

Offline Orangy Tang

JGO Kernel


Medals: 57
Projects: 11


Monkey for a head


« Reply #4 - Posted 2007-08-31 08:50:11 »

I am using the code sample by Andrew Davison:
http://fivedots.coe.psu.ac.th/~ad/jg/ch1/index.html
My Swing is rusty, but IIRC repaint() is unpredictable - multiple requests to repaint may be merged meaning it's useless to try and get a consistant framerate. And it's causing unnessessary usage of multiple threads (and all their associated problems) when only one should be needed here. Ideally you'd be using a BufferStrategy for your display to get vsync and page flipping, and Swing controls *really* don't like being put on one of those.

Although you can change the appearence of Swing controls, they still won't look good enough for a game IMHO. Remember look and feel - you can change the colours and the graphics but they'll still feel the same way. For a game you want all sorts of animation and behaviour going on on buttons which is pretty much impossible to do with Swing controls.

For 90% of games all you actually need is buttons and static text anyway, and you could write that yourself in a few days. Only if you're doing some kind of stat-heavy management sim would you need anything more complicated, in which case you might be able to get away with using Swing.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline CommanderKeith
« Reply #5 - Posted 2007-08-31 09:04:58 »

I am not getting all the hype  Huh

I am using the code sample by Andrew Davison:
http://fivedots.coe.psu.ac.th/~ad/jg/ch1/index.html

Seems to work quite alright or am I missing the point?

That's an interesting approach which will work and is thread-safe but it is not active rendering since the rendering takes place on Swing's EventDispatchThread (EDT) - The javax.swing.Timer fires ActionEvents which are executed on the EDT - see the docs: http://java.sun.com/javase/6/docs/api/javax/swing/Timer.html

The hype is all about getting the rendering to happen in your game loop rather than in the EDT.  The approach using SwingUtilities.invokeAndWait(...) is the same as yours and Andrew Davison's approach here- the rendering is done on the EDT so it is not done straight away.

My Swing is rusty, but IIRC repaint() is unpredictable - multiple requests to repaint may be merged meaning it's useless to try and get a consistant framerate. And it's causing unnessessary usage of multiple threads (and all their associated problems) when only one should be needed here. Ideally you'd be using a BufferStrategy for your display to get vsync and page flipping, and Swing controls *really* don't like being put on one of those.

That's right about repaint(), you can't use it all all for games since the painting could happen at any time (it just causes the EDT to repaint the component at its leisure), but using paintComponents() is OK if you call it from the EDT.  About BufferStrategy, I've got it to work with Swing controls, there's an example in the link I posted.

For 90% of games all you actually need is buttons and static text anyway, and you could write that yourself in a few days. Only if you're doing some kind of stat-heavy management sim would you need anything more complicated, in which case you might be able to get away with using Swing.
I often find that I want tables, lists, trees, spinners, layout managers, and lots of things that Swing has which would be a pain to build on my own. 

There's a JGO user Schabby who has built a GUI for games but it's only for JOGL or LWJGL games - it's called Fengui.

But I still think the way to go for java2D games is to do your game's painting in the game loop thread and the Swing buttons etc painting in SwingUtilities.invokeAndWait(...) or by synchronizing the EDT using the methods in that link.

Good luck with it  Smiley
Keith

Offline noblemaster

« JGO Spiffy Duke »


Medals: 35
Projects: 11


Age of Conquest makes your day!


« Reply #6 - Posted 2007-08-31 18:39:20 »

Thanks Commander for the clarification  Smiley

I got around 37 fps so far - let's find out if I can get more/get it to work with the EDT type code...

Quote
I often find that I want tables, lists, trees, spinners, layout managers, and lots of things that Swing has which would be a pain to build on my own.
I fully agree - also there are lots of ways to customize the components including animation: e.g. blinking button. Building them on my own would take lots of time.




Offline trembovetski

Senior Devvie




If only I knew what I'm talking about!


« Reply #7 - Posted 2007-09-02 16:36:49 »

Just one comment: invokeAndWait is expensive as it involves
thread switch. May be an issue for some.

Dmitri
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (523 views)
2018-06-10 19:43:48

EgonOlsen (627 views)
2018-06-10 19:43:44

EgonOlsen (424 views)
2018-06-10 19:43:20

DesertCoockie (770 views)
2018-05-13 18:23:11

nelsongames (1016 views)
2018-04-24 18:15:36

nelsongames (981 views)
2018-04-24 18:14:32

ivj94 (1545 views)
2018-03-24 14:47:39

ivj94 (557 views)
2018-03-24 14:46:31

ivj94 (1327 views)
2018-03-24 14:43:53

Solater (560 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!