Java Swing Classes

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Java Swing Classes

Post by ajtgarber »

So far I've seen that a lot of Java developers like to use the swing classes for their GUI programs, and even sometimes for games. I've always wondered why people would choose swing, as you could just as easily use AWT, and not have to deal with the performance issues. So I'm wondering, do the looks of the components outweigh the performance loss using Swing, or am I just missing the point entirely?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Java Swing Classes

Post by avansc »

There is not much different in performance. Using swing is fine.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: Java Swing Classes

Post by ismetteren »

Also, swing is way more cross platform than AWT, i think it is something about AWT having to be written for every new platform, while swing lets the JVM handle it... or something..
Image ImageImage Image
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Java Swing Classes

Post by programmerinprogress »

Everything I've read indicates that AWT is heavyweight and varies from platform to platform, but swing objects are lightweight and are intended to look the same on any platform you use them on, but I'm not so sure, I always just used swing in uni, and it works fine, although I've never made games in Java, yet.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Java Swing Classes

Post by avansc »

Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Java Swing Classes

Post by MrDeathNote »

Wow, that's an impressive engine!
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: Java Swing Classes

Post by ajtgarber »

Isn't it the other way around, that AWT is the lightweight while Swing is the heavy weight? Swing uses AWT internally but just adds extra features. Yes AWT is implemented natively on each different platform but thats the only way that Java can open a window. If you've ever use the JFileChooser or pretty much any Swing dialog/window you almost always notice that it takes an extra few seconds to come up, but if you were to do the equivalent in AWT it would almost instantly pop up.

Yeah that engine is amazing, I tried using it when it had a 2D version couldn't really figure too much out (I was stupid), but it helped me come up with some designs for my own engine.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Java Swing Classes

Post by wearymemory »

ajtgarber wrote:Isn't it the other way around, that AWT is the lightweight while Swing is the heavy weight?
No, Swing is referred to as lightweight, and AWT is referred to as heavyweight.

All the drawing of heavyweight components are handled by OS calls. While this can sometimes result in a faster program, there are drawbacks. The framework is not guaranteed to work identically across all platforms, one OS may contain features that another doesn't, and for a cross-platform language, development is limited to what features are shared between platforms. Classes from the AWT toolkit aren't as extensible as their lightweight counterparts, and because drawing is handled by the OS, there is no way to manipulate the way a component is painted on the screen (e.g., Rotating a button).
ajtgarber wrote:Swing uses AWT internally but just adds extra features. Yes AWT is implemented natively on each different platform but thats the only way that Java can open a window.
That's correct, the top-level Swing components rely on being heavyweight in order to create a visible space that can be drawn to, while all other Swing components are painted in pure Java, but still subclass AWT for simplicity, compatibility (mixing heavyweight and lightweight components), and ease when porting historical code.

Lightweight components allow your programs to look and feel the same across platforms. You can modify the opacity and shape of lightweight components, or transform, add to, and even completely change the look and feel of existing components using the Java 2D API. Using Swing in games can allow you to create and maintain pluggable look and feels that are attractive and compliment your game, instead of looking awkward and out of place.

The eye-candy that can be made with the Swing framework is only limited by your imagination. For example, you may call a component's paint methods while passing in a Graphics object as a parameter that may be used by a texture that has been applied to a shape within a scene graph, essentially making 3D components (with some clever event dispatching, of course), which can be useful when you really need to woo rich clients.
ajtgarber wrote: If you've ever use the JFileChooser or pretty much any Swing dialog/window you almost always notice that it takes an extra few seconds to come up, but if you were to do the equivalent in AWT it would almost instantly pop up.
In most cases, the performance increase of heavyweight components is negligible. JFileChooser on the other hand, has been reportedly slow, but you can use FileDialog if it fits your needs.

When choosing which API you want to use (and you may not always get to), you should pick the one that satisfies your projects needs the best.
Post Reply