Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
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  
  JFrame/Frame and the Event Dispatch Thread  (Read 955 times)
0 Members and 1 Guest are viewing this topic.
Offline PixelPop

Senior Newbie




Wootage to you!


« Posted 2013-02-17 21:30:48 »

Greetings all!

First post here on JGO. Looking forward to learning and contributing  Grin

I am using a Canvas within a JFrame and was wondering, should I be using a Frame instead of a JFrame? Also, should the creation and showing of either (Frame/JFrame) be done on the EDT thread?

Thanks!

May you live as long as you want, but never want as long as you live.
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2013-02-17 21:56:54 »

If you are only use Canvas, then stick with Frame. No need to initialize the entire Swing mess.

And yes, that is preferable. Smiley

Offline PixelPop

Senior Newbie




Wootage to you!


« Reply #2 - Posted 2013-02-17 22:12:49 »

Ok, Frame it is then. Thanks Ra4king

May you live as long as you want, but never want as long as you live.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KevinWorkman

JGO Wizard


Medals: 52
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #3 - Posted 2013-02-18 18:58:38 »

I want to ask a dumb question.. what's the advantage of using Frame and Canvas instead of, say, JFrame and JPanel or JComponent?

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-02-18 19:28:54 »

I want to ask a dumb question.. what's the advantage of using Frame and Canvas instead of, say, JFrame and JPanel or JComponent?
Kinda outdated article here: http://www.ibm.com/developerworks/grid/library/os-swingswt/
What I picked up was just that they're thread-safe and are (mostly) automatically disposed of.
Offline jonjava
« Reply #5 - Posted 2013-02-18 19:35:54 »

ばかなしつもんがない。

Frame and Canvas are part of AWT and JFrame and JPanel are part of Swing.

AWT has been in java since 1.0 and Swing came in at 1.1 or something.

Swing has lots of built-in stuff to handle common GUI (Graphical User Interface) elements such as buttons, window bars, radio selectors, drop down selectors, input boxes for keyboard, text displaying field (proper interactive ones you can either type in, copy and paste text in etc) etc.

Simple AWT doesn't have these features, it just draws things (pixels) on screen. Swing is built on top of AWT.

So if you are doing your own drawing in the game, for example, there's no need to use Swing. You can use AWT in Swing so you can simply add a Canvas in your Swing application as a component.

http://docs.oracle.com/javase/tutorial/uiswing/components/index.html

google-fu:
http://www.jguru.com/faq/view.jsp?EID=232717
http://www.dreamincode.net/forums/topic/185591-canvas-v-jpanel/

Offline KevinWorkman

JGO Wizard


Medals: 52
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #6 - Posted 2013-02-18 19:39:34 »

Kinda outdated article here: http://www.ibm.com/developerworks/grid/library/os-swingswt/
What I picked up was just that they're thread-safe and are (mostly) automatically disposed of.

Thanks. From that article:
Quote
Like AWT, Swing supports automatic disposal of GUI components. Swing also supports the AWT bottom-up and top-down construction methods.

Unlike AWT, Swing components are not thread-safe, which means you need to be concerned as to which thread in your application updates the GUI. If you err in thread use, unpredictable behavior, including user interface glitches, can occur. Utility routines exist to help manage threading issues.

So I see what you're saying with AWT being thread-safe (which I did not know, interesting), but not the automatic disposal (because Swing also does the same thing). Am I missing something?

I've seen plenty of gaming folks using Canvas and Frame/Applet instead of JPanel and JFrame/JApplet, and I've never quite understood why. Is the thread-safe thing (which is solved with Swing.invokeLater() and probably doesn't matter with most games anyway) really worth sacrificing all the other benefits of Swing? In gui development, AWT is almost never used over Swing, so I'm curious about why it's backwards in game development.

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline KevinWorkman

JGO Wizard


Medals: 52
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #7 - Posted 2013-02-18 19:45:22 »

Thanks jonjava. I understand the difference between AWT and Swing; in fact my full-time day job is gui development in Swing. That's actually part of my confusion- The only time I see AWT actually used is in really old tutorials or in game development; everywhere else recommends Swing over AWT because of all the features, the built-in double buffering, etc.

I see your point that Swing might not be necessary for many games, which don't require "real" guis. But it still seems strange to see people recommending AWT over Swing.. it's not like you have to use the parts of Swing you don't need.

I don't mean to hijack the thread, but this is something that always itches at me.

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline jonjava
« Reply #8 - Posted 2013-02-18 20:14:46 »

Because Canvas is less of a pain to use.

It's like there's a shop 20m from your house. Suddenly you get a craving for cookies so you decide to go to the store. You can choose to take the car or you can walk over to the store. Taking the car most likely take more time than simply walking straight over.

On a rainy day you might avoid some rain by taking the car, but if you walk you can avoid it completely by having an umbrella.

This analogy is horrible, but whatever. :V

Basically, it doesn't make sense to botch up the game screen AND the ui / hud into the same screen. Separate them instead. And for a simple game screen Canvas is easy and flexible. Adding your Canvas into a Swing app is as simple as addComponent( canvas );

Offline KevinWorkman

JGO Wizard


Medals: 52
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #9 - Posted 2013-02-18 20:23:45 »

Because Canvas is less of a pain to use.

Can I ask how? I'm not trying to be argumentative or disagree, I just honestly can't see how Canvas is easier than JPanel or JComponent.


Basically, it doesn't make sense to botch up the game screen AND the ui / hud into the same screen. Separate them instead. And for a simple game screen Canvas is easy and flexible. Adding your Canvas into a Swing app is as simple as addComponent( canvas );

You can call add(JPanel) or add(JComponent) just as easily though, can't you?

I wouldn't suggest putting the ui and the game inside the same component. Instead, extend JPanel or JComponent (exactly like you do with Canvas), then override paintComponent() and you get double buffering for free. Then use a completely different JPanel for your ui (buttons, checkboxes, whatever), probably again exactly like you'd do with Canvas. This is my standard way of mocking up a game, and I don't see how Canvas makes that easier.

I feel like I'm missing something obvious, but I can't see what it is.

Static Void Games - Play indie games, learn game programming, upload your own games!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BoBear2681

JGO Coder


Medals: 18



« Reply #10 - Posted 2013-02-18 20:50:30 »

You're right that there's no effective "real-world" difference.  The main reason I think people suggest to use AWT over Swing for games is that there's simply less overhead.  As soon as you create a JFrame, a JPanel, etc., Swing's LookAndFeel-related classes all get loaded, but if you're not using any JComponents other than say a custom-painted JPanel, you don't actually use them.  Why load classes you don't need or use?  etc.

Again, this is a negligible "benefit", and certainly isn't noticed by users.
Offline KevinWorkman

JGO Wizard


Medals: 52
Projects: 11
Exp: 12 years


klaatu barada nikto


« Reply #11 - Posted 2013-02-18 20:53:37 »

Thanks, that makes perfect sense.

I would personally think that getting newbies into the habit of using AWT over Swing can lead to bad practices when they branch out to gui design, which is worse in the long run than any look and feel overhead. But that's a personal preference, and I can see the other side as well.

Thanks again for taking the time to answer my dumb questions!

Static Void Games - Play indie games, learn game programming, upload your own games!
Offline jonjava
« Reply #12 - Posted 2013-02-18 21:24:30 »

GUI's and games are completely different things. No need to confuse the two. There's essentially no added functionality by using JPanel over Swing. You're just "limiting" yourself to Swing.

The only thing we really need is the collection of pixels that represent the game screen. But Canvas is a good compromise since it fits neatly in both AWT and Swing. That is if you're sticking to pure java.

Throw Canvas away if you're using some better other libraries. Moreover, IIRC, JOGL could directly draw java Canvasies.

Also, learning to deal with your own double buffering is both a trivial but useful thing that might get lost in Swings mysterious world.

IMO :V

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!