Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Mixing light-weight (Swing) and heavy-weight (AWT) components?  (Read 1724 times)
0 Members and 1 Guest are viewing this topic.
Offline cookie

Junior Member


Medals: 1



« Posted 2013-04-27 14:43:56 »

Hello people,
I have learned that mixing mixing light-weight (Swing) and heavy-weight (AWT) components is generally considered bad programming style, but in some cases it seems to be practicable. In the following example, the graphics are drawn on a canvas while for the rest of the GUI Swing is used. Why that? Why not AWT?

I am talking about that example:
http://www.java-gaming.org/topics/basic-game/21919/view.html

Other questions I have: What is the best way to put the game loop into a separate class? Maybe a singleton GameLoop class which provides a loop() method that is later called in the run() method? Does it make sense to use a scene graph for 2D games and if so, how is it implemented?

Thanks for any answer and have a good weekend.
Offline davedes
« Reply #1 - Posted 2013-04-27 17:21:28 »

Most people choose Swing since it is more powerful, usually looks nicer (themes etc) and more standard. The main problem with mixing light and heavy weight components is render order, e.g. for popups like menu items. Sometimes JPopupMenu.setDefaultLightWeightPopupEnabled solves the problem.

Singletons are generally not a good design pattern. There are many ways of decoupling your game loop, one easy choice:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class AbstractGame {

    public void run() {
         create();

         ... start game loop ...
             render();

         dispose();
    }

    public abstract void create();   //allocate resources
   public abstract void render();  //render resources
   public abstract void dispose(); //destroy resources
}


Another common solution is to use a "listener" interface. So, instead of calling methods in AbstractGame, you pass a listener to the Game class at creation time, and then the Game class calls the listener's methods during the game loop. This makes it easy to support multiple backends and distributions.

Ultimately though, if you plan to make a relatively complex game (i.e. one that requires a scene graph, in-game UI, tiled maps, sprite sheet animations, physics, etc) you should not use Java2D. Not only is it slow, bulky to use, and doesn't give you anything "out of the box," but it only distributes to desktop and applets (which are now defunct, for all intents and purposes).

Instead it would make sense to use a game-specific framework built on OpenGL rendering, like LibGDX. Smiley

Online SHC
« Reply #2 - Posted 2013-04-27 17:24:33 »

1  
2  
-    public class AbstractGame {
+    public abstract class AbstractGame {

Class needs to be declared as
abstract
for it to have abstract methods.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2013-04-27 17:24:57 »

Yep... Typo. Grin

Offline cookie

Junior Member


Medals: 1



« Reply #4 - Posted 2013-04-27 20:09:49 »

Thanks. The central point is that I want to use BufferStrategy, because according to some threads and tutorials this is more powerful than BufferedImage. However, it's part of AWT and not Swing, hence I think that it might make sense to use only AWT components to avoid any problems. At least for noobs that would be the less error-prone decision, wouldn't it?
Offline davedes
« Reply #5 - Posted 2013-04-27 21:14:15 »

1. Mixing AWT and Swing isn't that bad; it's probably worse having to deal with AWT itself than the subtle z-order problems that can occur when you mix it with Swing. If you get problems, you can use the JPopupMenu utility method to fix most of them.

And generally speaking your game won't need JMenuBar or JPopupMenu anyways, since that will be built as an in-game GUI.

2. BufferStrategy and BufferedImages are apples and oranges. You will be using both for an active rendering loop.

3. If you are looking for "good performance" than Java2D isn't the right choice in the first place. Like I said; build your game on OpenGL -- this means using LWJGL, JOGL, or something higher level like LibGDX, lwjgl-basics, jPCT, jMonkeyEngine, etc.

Offline cookie

Junior Member


Medals: 1



« Reply #6 - Posted 2013-04-27 23:15:13 »

In-game-gui means that all messages and preferences/settings panels are written directly to the screen without using any Swing/awt GUI components? Seems like a lot of work, but maybe it's easier than I think. Do you know any example for such a GUI?
Offline davedes
« Reply #7 - Posted 2013-04-27 23:53:42 »

If you use Java2D it means you need to roll your own UI.

If you use LibGDX it would be really easy, since it includes in-game GUI for you. For example:
http://www.java-gaming.org/?action=pastebin&id=556

Which leads to:


Like I keep saying... Swing/AWT/Java2D is not a good option for games. Smiley

Offline cookie

Junior Member


Medals: 1



« Reply #8 - Posted 2013-04-28 01:00:36 »

Thank you :-)
My goal is to make a simple, very basic tetris-like game, but it should have a solid, efficient fundament which can be used to build more complex 2D games. I used OpenGL many years ago and have never used it since then, so I think it would be quite hard to implement. Most people don't recommend to use OpenGL for 2D, some do, but I don't have enough experience to evaluate the pros and cons. I also haven't found any tutorials on 2D OpenGL games in Java.
Doesn't Java2D use OpenGL under the hood? OS window managers are also 2D, but sometimes based on OpenGL.
Offline davedes
« Reply #9 - Posted 2013-04-28 02:21:20 »

Most modern 2D games use OpenGL -- and virtually any 2D game on iOS/Android will use OpenGL ES. It's the best way to deliver high performance and provide flexible graphical effects (blurs, post-processing, etc). I'm not sure who told you that OpenGL is not good for 2D -- maybe that was true in 1991, but we have come a long way since then.

Java2D may or may not use OpenGL for rendering; on many systems it will default to software, and in many cases the hardware-acceleration it does take advantage of is not exactly reliable or performant. Basically, you can't rely on Java2D to take advantage of hardware.

LibGDX code abstracts a lot of OpenGL rendering for you; and isn't very difficult to work with. In this sense, it's a bit like Java2D in its abstraction of image/shape rendering.

This discussion is a bit silly.. Just try LibGDX for yourself before you continue to wonder about how difficult it may be. Roll Eyes I can assure you that it will be easier and less painful to make a complex game in LibGDX than to program it entirely from the ground up with Java2D. Most importantly; it will be much more performant.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline relminator
« Reply #10 - Posted 2013-04-28 04:11:11 »

Thank you :-)
My goal is to make a simple, very basic tetris-like game, but it should have a solid, efficient fundament which can be used to build more complex 2D games. I used OpenGL many years ago and have never used it since then, so I think it would be quite hard to implement. Most people don't recommend to use OpenGL for 2D, some do, but I don't have enough experience to evaluate the pros and cons. I also haven't found any tutorials on 2D OpenGL games in Java.
Doesn't Java2D use OpenGL under the hood? OS window managers are also 2D, but sometimes based on OpenGL.

Actually, most people would recommend opngl.  Once you get past the boilerplate code, everything should be a breeze and if you use libgdx, boilerplate code is handled by libgdx for you.

The only reason to do java2d for games these days it's fun learning it(at least for me).
Offline StumpyStrust
« Reply #11 - Posted 2013-04-28 04:54:56 »

The only reason to do java2d for games these days it's fun learning it(at least for me).

By fun you mean like taking nutcrackers to ones testies. *whisper* Masochist *whisper*

Offline cookie

Junior Member


Medals: 1



« Reply #12 - Posted 2013-04-30 12:58:50 »

Thanks. I am just thinking about how to decouple the game loop from the display update and game state update methods. In most cases you have a display class like

1  
2  
3  
4  
5  
6  
7  
public class GameScreen extends Canvas implements Runnable {

  run() {
  // game loop here with update and render methods
 }

}


A separate generic game loop class doesn't know which methods are the render and update methods, so I have to inform the game loop class about the methods it should use. How could this be done? Maybe by wrapping the render and update methods in commands?

Another option could be to make an game loop class that implements Runnable and that contains abstract render and update methods that have to be implemented in the GameScreen class. I think this was the idea that was mentioned in this thread before. However, if I want to extend Canvas in my game class I cannot extend the abstract game loop class, but maybe I should better use Canvas as a property?

Which one is better design?
Offline StumpyStrust
« Reply #13 - Posted 2013-04-30 15:38:11 »

Does it work? What works is what is right.

Like I always say with optimizations and designing the "perfect" system. All you are doing is getting nowhere faster and more elegantly. Get something that works and then if it is slow or too hard to use just then, you should consider a better more efficient design. As you code and recode you will get better and better. The over all design and structure of the stuff you write will naturally improve. Get something that does something first.

You don't really need to extend canvas. All a canvas is, is what its name implies. Just need to grab the graphics object in the render loop from it.

Offline relminator
« Reply #14 - Posted 2013-04-30 16:19:45 »

The only reason to do java2d for games these days it's fun learning it(at least for me).

By fun you mean like taking nutcrackers to ones testies. *whisper* Masochist *whisper*

Learning a new thing is always fun.  The more you know the more tools you have at your disposal.

BTW, it's testes(try gonads for a change).
Offline StumpyStrust
« Reply #15 - Posted 2013-05-01 08:29:59 »

Learning stuff in java2d is a useless tool. Sure maybe you get better at coding but that comes with any form of coding.

And thanks for the correction but I think I got the point across.  Wink

Offline cookie

Junior Member


Medals: 1



« Reply #16 - Posted 2013-05-01 14:16:46 »

Thank you guys. Do you wrap your classes in design patterns? You could make a singleton of the game screen or wrap methods like start game, stop game, pause game, resume game etc. in command classes, but I don't know if that is making sense.
Offline relminator
« Reply #17 - Posted 2013-05-01 14:26:40 »

Learning stuff in java2d is a useless tool. Sure maybe you get better at coding but that comes with any form of coding.

And thanks for the correction but I think I got the point across.  Wink


Useless in what sense?  It I already used it to help my nephew with his java projects.

Granted it's not as fast as ogl but they also say the sane with c++ vs. java.  If I listened to them then I wouldn't be here learning java and having fun coding in it.


@cookie: I usually don't get really enamored whether I use a certain design pattern.  Just code what you think is "natural" and patterns would take care of themselves.
Offline cookie

Junior Member


Medals: 1



« Reply #18 - Posted 2013-05-01 18:10:34 »

Does anyone here use the observer pattern to give the game loop the ability to inform other classes about its update cycle? I don't know if that's useful, maybe it slows the application down.
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.

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

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

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

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

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

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
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

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24: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!