Reputation: 27
I'm teaching myself Java and I want to create a rock, paper, scissors game with a GUI. I have created a text based version with scanners, but I have a lot of work to go.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
public class RPS extends JFrame {
JRadioButton rock, paper, scissors;
ButtonGroup choices;
public static void main(String[] args) {
new RPS();
}
public RPS() {
super("Rock, Paper, Scissors");
setSize(400,300);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p=new JPanel();
rock = new JRadioButton("Rock");
paper = new JRadioButton("Paper");
scissors = new JRadioButton("Scissors");
choices = new ButtonGroup();
choices.add(rock);
choices.add(paper);
choices.add(scissors);
p.add(rock);
p.add(paper);
p.add(scissors);
add(p);
setVisible(true);
}
}
Here is my code. I already have it creating a window and displaying 3 radio buttons that will only allow one choice to be selected. From here, I want to implement a next button, and create logic to produce an answer based on both choices. I believe I need a card layout, but the Oracle documentation doesn't help me. I also have no idea how I can go around implementing logic. Any help is appreciated, sorry for the long post. Thanks again!
I'm sorry I didn't make it clear, I want to design this for one person to take a turn, press the nest button, then the second person will take a turn, press finish and get the results. I will present this to my 8th grade class.
Upvotes: 0
Views: 1779
Reputation: 209052
Here's hot to implement CardLayout
into your program. The purpose of this layout is to layer components. In your case, you would need a panel for each player. So you would need two panels.
The above two are what comprise the Card(layered)Layout
(you would stack one on top of the other.
So all together, you layout should look like this
Wrapped in JPanel(new BoderLayout())
-------------------------------------
| label to show status | BorderLayout.NORTH
-------------------------------------
| |
| CardLayout holding |
| two JPanels with RBs | BorderLayout.CENTER
|___________________________________|
|(JPanel) Next JButton | BorderLayout.SOUTH
-------------------------------------
When you click the next button, you can call the next()
method of the CardLayout
to show the next panel
Example
private CardLayout cardLayout = new CardLayout(10, 10); // hgap and vgap args
private JPanel cardPanel = new JPanel(cardLayout);
JPanel panel1 = new JPanel(); // holds first player
JPanel panel2 = new JPanel(); // holds second player
cardPanel.add(panel1);
cardPanel.add(panel2);
nextButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
cardLayout.next(cardPanel);
}
});
See CardLayout docs for other movement methods
For the logic part
Example
JRadioButton p1Scissors = new JRadioButton("Scissors");
JRadioButton p1Paper = new JRadioButton("Paper");
JRadioButton p1Rock = new JRadioButton("Rock");
// group them
JRadioButton p2Scissors = new JRadioButton("Scissors");
JRadioButton p2Paper = new JRadioButton("Paper");
JRadioButton p2Rock = new JRadioButton("Rock");
// group them
JLabel statusLabel = new JLabel(" ");
JButton checkWinner = new JButton("Check Winner"); // You can add to bottom panel
checkWinner.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (p1Scissors.isSelected() && p2Rock.isSelected()){
statusLabel.setText("Player 2 wins: Rock beats Scissors");
} else if ( ..... ) {
...
}
...
}
});
Upvotes: 2