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.

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.

Got a question about Java and game programming? Just new to the Java Game Development Community? Try my FAQ. Its likely you'll learn something!

OK tnx for the info. 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

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