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?
# 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.
# 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);
}
}
# 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?