Game menu

I am developing a game and have the game pretty much coded at least to an extent. I want to add a Title screen and menu that says something like

1)Start new game

2)Load level

3)Read Rules

Based on the response it should go to each different area of interest.

Right now when I execute my application it goes right into gameplay.

So what I would like to know is how to insert this menu before the gameplay. Does this menu run in it's own thread which ends as soon as game play starts? Or does it just get injected logically before my game play starts in some loop that ends only when the person selects 1 or 2?

[654 byte] By [steeveesasa] at [2007-11-27 11:06:53]
# 1

open with a JDialog, similar to a login screen (include a quit option), with

perhaps JRadioButtons for the selection options

Michael_Dunna at 2007-7-29 13:18:27 > top of Java-index,Desktop,Core GUI APIs...
# 2

What I have is a graphic that I want to display as the game title. Underneath this graphic is where I would have the choices...SO:

-

||

|GRAPHIC|

||

-

Start new game

Load Level

Read Rules

Quit

If the user chooses start new game then it starts level 1

If the user chooses load level, then it asks for the password

If the user selects to read the rules, then I would like them to be able to shuffle back and forth between say 6 or 7 screens of rules - a screen per rule with some kind of previous rule and next rule button.

If the user selects quit, it exits.

Can the JDialog do all of this?

Basically I'm wondering if there is some standard template for menus like this, because the way I want it to function is like many games I have seen.

steeveesasa at 2007-7-29 13:18:27 > top of Java-index,Desktop,Core GUI APIs...
# 3

here's one of the ways (this is quick'n'dirty, idea purposes only)

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

public void buildGUI()

{

JPanel p = new BackgroundImagePanel("test.gif");

p.setLayout(new GridBagLayout());

p.setPreferredSize(new Dimension(400,300));

JPanel buttonPanel = new JPanel(new GridLayout(1,4));

JButton newBtn = new JButton("New Game");

JButton loadLevelBtn = new JButton("Load Level");

JButton rulesBtn = new JButton("Rules");

JButton quitBtn = new JButton("Quit");

buttonPanel.add(newBtn);

buttonPanel.add(loadLevelBtn);

buttonPanel.add(rulesBtn);

buttonPanel.add(quitBtn);

p.add(buttonPanel,new GridBagConstraints());

final JDialog d = new JDialog();

d.getContentPane().add(p);

d.pack();

d.setLocationRelativeTo(null);

d.setVisible(true);

d.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent we){

System.exit(0);

}

});

newBtn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

d.dispose();

new GameFrame().buildGUI(0);

}

});

loadLevelBtn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

d.dispose();

//code to supply password for level attained, hard-code 3 for e.g.

new GameFrame().buildGUI(3);

}

});

rulesBtn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

d.dispose();

new RulesFrame().buildGUI();

}

});

quitBtn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

System.exit(0);

}

});

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

class BackgroundImagePanel extends JPanel

{

Image img;

public BackgroundImagePanel(String file)

{

super();

try

{

img = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource(file), file));

}

catch(Exception e){}//do nothing

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

if(img != null) g.drawImage(img, 0,0,this.getWidth(),this.getHeight(),this);

}

}

class GameFrame

{

public void buildGUI(int level)

{

JFrame f = new JFrame();

f.setTitle(level == 0? "New Game":"Level "+level);

f.setSize(400,300);

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

}

class RulesFrame

{

public void buildGUI()

{

JPanel p = new JPanel();

p.setBorder(BorderFactory.createTitledBorder("CardLayout Panel - Wizard Style"));

JFrame f = new JFrame();

f.setTitle("Rules");

f.getContentPane().add(p);

f.setSize(400,300);

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

}

Michael_Dunna at 2007-7-29 13:18:27 > top of Java-index,Desktop,Core GUI APIs...
# 4

Michael - Thanks for the quick response! I have to leave town for the weekend, and won't get a chance to work on this until Monday evening, but I am eager to implement the menu. I'll let you know how it works out as soon as I get back and get started.

Steve

steeveesasa at 2007-7-29 13:18:27 > top of Java-index,Desktop,Core GUI APIs...
# 5

Okay, so I finally got around to testing out some of the code you put up here. First off thanks, it was the perfect start. I am going to make some changes. For example instead of opening a new JFrame for each menu choice, I will put logic in the action listener to just repaint the screen(the current JFrame) with the new info that I want up(for example my rules are just stored as a .PNG file which I will just set as the image.

But basically the code helped me get there, so thanks!

It's been a while since I have seen my game(and java at all), and needed some help getting back to it...

Steve

steeveesasa at 2007-7-29 13:18:27 > top of Java-index,Desktop,Core GUI APIs...
# 6

One more question. I have basically gotten the logic in for the menu to do what I want, now I am just having a formatting issue.

I spent about 2 hours last night trying to read about Gridbaglayouts and Gridlayouts and playing with some of the variables...What happens as you know, is that the buttonPanel is centered in the middle of the screen, as is the default for Gridbaglayout. What I want to do is move it down on the screen(although not all the way down like "SOUTH" does).

I tried

buttonPanel.setLocation(X1,Y1)

//and I tried moving the individual buttons as well:

newBtn.setLocation(X1,Y1)

rulesBtn.setLocation(X1,Y1)

//etc...

//I tried taking Gridbaglayout out of the code i.e.

//commenting out the line that says:

p.setLayout(new GridBagLayout());

//when I do this, the button panel moves to the top of the screen

//(centered middle (left to right) still)

Anyhow I have tried many variations of the above ideas, and none seem to get me what I want.

What should I do?

steeveesasa at 2007-7-29 13:18:27 > top of Java-index,Desktop,Core GUI APIs...