problems with listeners

Hello.
I’m writing a simple soccer game. I have a JFrame as main container for stuff and a JPanel object in seperate thread for animation. Problem is when I try to build a proper start menu. JPanel has mouse listeners. The problem is when I add listeners to JFrame also and put a code to start a game when rectangle over start button is clicked. If JPanel object is created first (even if I don’t need it right away) then JFrame mouse events don’t happen, well actually JPanel’s listener methods have still control of it even if I didn’t added it yet. So I don’t get it, there can be only one listener? For easier programming I want to divide code that needs to be executed into their own classes, trying to avoid to put all listener code to JFrame (main class). Thanks for the help.

You are experiencing some of the resons why serious game prgrammers dont build ontop of widget systems.

They write their own menuing ontop of the framebuffer or 3D API/

  1. Don’t use threads for animation
  2. Don’t use multiple panels and/or frames
  3. Don’t use multiple mouse listeners

For your own sanity, have a single method of retriving the state of the input (such as a single mouse listener) and pipe all of your input though a central point. Distribute to the various sections of your game as required.

ok tnx, but how should I do it then? I’m actually reading Killer Gamer Programming in Java, publisher O’Reilly. There this methods are used (except listeners). I would gladly do it any other, better way if I know about it. OK I will use central input and pass it to the needed parts of game, hopefully that will solve the problem. Tnx again.

Most serious games go directly to the graphcis hardware to draw everything and build upon that.

There are 3 good paths today for that: JOGL and LWJGL (used for 3D but you can also do 2D with them. Cas has done some nice LWJGL based 2D games you can see at Puppy Games, Kev has one at his site too) and the BufferStrategy system in AWT (for 2D games. I did an isometric exampel awhile back called Scroller I’m going to try to get into the projects this week. There ar other examples around too, I think Kev did one this way as well, for instance.)

In terms of input, they read the controllers directly by polling. JInput allows you to do that. LWJGL has ist own API (but uses JInput uderneath.) Menus are done wth your own logic. You put a menu graophic on the screen. When the suer clicks, you get the location of the click and compare it to where the active parts of your gaphic are and react as necessary.

ALL of this is done once per frame ina tight loop, usually called the “game loop.” Any of the example games I mentioned will be based around a game-loop.

OK tnx for the info. :wink:
About start menu… isn’t it better to just draw once the menu and then act upon click’s location, then puting him in game loop? This way it uses much less cpu’s time. Maybe if you have animated start menu you might do that, but I’m working only with few pictures and am long way from fancy animated menus ::slight_smile:

Well…

in many games even the cursor is game-loop animation…

Certainly if you want to have visual resposne to user event your going to need to do some kind of basic blt based animation eventually…