0

I've been working quite a bit on a game, not too fancy, and i was wondering on what the best way to make a menu screen would be. I am currently using a single JPanel as the foundation for "painting." I was wondering what the best way to implement Menu Screens etc. would be.

What I thought of so far:

a) Have the current JPanel take care of the Menu screen.

b) Make new JPanels for them and swap them to the next one; ex: If i hit the new game option, then switch to a new JPanel that holds the Game setup.

Extra Question:

Is it better to us a Canvas or some other component over a JPanel, given play-ability on an Applet and an Application was a concern?

Thanks!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
imbuedHope
  • 508
  • 3
  • 14

3 Answers3

3

Recommendations:

  • If this is a Swing application, then by all means stick with Swing components such as JPanels.
  • Your users will probably prefer your swapping JPanels, and if you decide to go this route, the CardLayout will make it easy to do.
  • You will probably want to swap JPanels if the views presented in them are significantly different. If it's only a matter of changing text though, then you could possibly get by with changing the text of several JLabels instead of swapping.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    +1 Good advice; implementing `Icon`, illustrated [here](http://stackoverflow.com/a/3072979/230513), is another convenient way to customize the graphics of an existing `JComponent.` – trashgod Feb 29 '12 at 02:43
2

From your question I am guessing that your game view is drawn entirely with Graphics inside paintComponent(), and there aren't any other components already in your main JPanel. In that situation it is probably cleaner to just switch which drawing code you call rather than having the other drawing code in an entirely different panel class.

Edit:

Just to clarify: The main reason you would want to swap JPanels is if the original JPanel had a lot of components, listeners, etc. installed on it that you need to swap out in order to switch it to displaying the menu. If the menu screen can be painted to the same Graphics and be run by the same MouseListener, I would use just a single panel.

Russell Zahniser
  • 16,188
  • 39
  • 30
1

Is it better to us a Canvas or some other component over a JPanel, given play-ability on an Applet and an Application was a concern?

Don't use a Canvas unless the rest of the project uses AWT. If it is Swing, and a component contains only custom painting, use a JComponent. If Swing and the area has components laid out inside it, use a JPanel.

Why did you make a distinction between an applet and application? Are you thinking of using AWT in the applet, by any chance?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I made the distinction simply to make the point that I wanted to be able to run it on both an Applet and a windowed Application. I normally use the JApplet. – imbuedHope Mar 01 '12 at 05:41