I wrote this code to make the choices a user has to make to choose the game modes:
JButton btnNewButton = new JButton("Start Game");
JRadioButton beginner = new JRadioButton("Beginner");
JRadioButton intermedie = new JRadioButton("Intermedie");
JRadioButton expert = new JRadioButton("Expert");
JRadioButton custom = new JRadioButton("Custom");
JRadioButton mineFullRandom = new JRadioButton("Mine full random");
JRadioButton minePartialRandom = new JRadioButton("Mine partial random");
The first three are used to choose the difficulty of the game while the last two to choose the mode. I set just selected the beginner difficulty and the mine full random modality.The Start Game
button, as you can understand from the name it is necessary to start the game
Once I have created the JRadioButton
and the JButton
I am going to add them on a GroupLayout
.
Controller.java
This class I thought to handle all button events.
@Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton)e.getSource();
JRadioButton difficulty = (JRadioButton)e.getSource();
JRadioButton choice = (JRadioButton)e.getSource();
if(source.getText().equals("Start Game")){
if(difficulty.getText().equals("Beginner") /*&& choice.getText().equals("Mine full random")*/){
fullRandom = new FullRandomGrid(ROW_BEGINNER, COLUMN_BEGINNER, MINE_BEGINNER);
View view = new View(fullRandom);
//Minesweeper.game.container.add(view);
/*Minesweeper.game.container.add(new View(fullRandom), BorderLayout.CENTER);
Minesweeper.game.container.remove(Minesweeper.menu);
Minesweeper.game.setVisible(true); */
view.setVisible(true);
}
else if(difficulty.getText().equals("Beginner") && choice.getText().equals("Mine partial random")){
partialRandom = new PartialRandomGrid(ROW_BEGINNER, COLUMN_BEGINNER, MINE_BEGINNER);
Minesweeper.game.container.add(new View(partialRandom));
Minesweeper.game.container.remove(Minesweeper.menu);
}
else if(difficulty.getText().equals("Intermedie") && choice.getText().equals("Mine full random")){
fullRandom = new FullRandomGrid(ROW_INTERMEDIE, COLUMN_INTERMEDIE, MINE_INTERMEDIE);
}
else if(difficulty.getText().equals("Intermedie") && choice.getText().equals("Mine partial random")){
partialRandom = new PartialRandomGrid(ROW_INTERMEDIE, COLUMN_INTERMEDIE, MINE_INTERMEDIE);
}
}
}
How do I do when you select a specific JRadioButton, do you deselect the one previously selected? How do I get the game started and close the menu when the 'Start Game` button is pressed?