import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class RPSpanel extends JPanel implements ActionListener {
private ImageIcon rockImg = new ImageIcon("Rock.jpg");
private ImageIcon paperImg = new ImageIcon("Paper.jpg");
private ImageIcon scissorsImg = new ImageIcon("Scissors.jpg");
private ImageIcon directionsImg = new ImageIcon ("Rock_paper_scissors.jpg");
private ImageIcon exitImg = new ImageIcon("Exit1.jpg");
private JTextField userTxt, compTxt, winsTxt, lossesTxt, tiesTxt, outComeTxt;
private JLabel userLbl, compLbl, winsLbl, lossesLbl, tiesLbl, outComeLbl;
private JButton rockBtn, paperBtn, scissorsBtn, exitBtn, dirBtn;
private JTextArea outputArea;
final static int ROCK = 1;
final static int PAPER = 2;
final static int SCISSORS = 3;// class variables- will exist for duration of
// program
static int wins = 0;// also class variables- starts count as 0
static int losses = 0;
static int ties = 0;
static int userChoice;
static int computerGuess;
public RPSpanel() {
// display panel
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new GridLayout(6, 2));
new Die(3);
Font ssBold16 = new Font("SansSerif", Font.BOLD, 16);
userLbl = new JLabel(" User Chose: ");
userLbl.setFont(ssBold16);
displayPanel.add(userLbl);
userTxt = new JTextField(11);
userTxt.setFont(ssBold16);
displayPanel.add(userTxt);
compLbl = new JLabel(" Computer Chose: ");
compLbl.setFont(ssBold16);
displayPanel.add(compLbl);
compTxt = new JTextField(11);
compTxt.setFont(ssBold16);
displayPanel.add(compTxt);
outComeLbl = new JLabel(" Results: ");
outComeLbl.setFont(ssBold16);
displayPanel.add(outComeLbl);
outComeTxt = new JTextField(11);
outComeTxt.setFont(ssBold16);
displayPanel.add(outComeTxt);
winsLbl = new JLabel(" Wins: ");
winsLbl.setFont(ssBold16);
displayPanel.add(winsLbl);
winsTxt = new JTextField(11);
winsTxt.setFont(ssBold16);
displayPanel.add(winsTxt);
lossesLbl = new JLabel(" Losses: ");
lossesLbl.setFont(ssBold16);
displayPanel.add(lossesLbl);
lossesTxt = new JTextField(11);
lossesTxt.setFont(ssBold16);
displayPanel.add(lossesTxt);
tiesLbl = new JLabel(" Ties: ");
tiesLbl.setFont(ssBold16);
displayPanel.add(tiesLbl);
tiesTxt = new JTextField(11);
tiesTxt.setFont(ssBold16);
displayPanel.add(tiesTxt);
// buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
// Directions button
dirBtn = new JButton("Directions");
dirBtn.addActionListener(this);
buttonPanel.add(dirBtn);
// rock button
rockBtn = new JButton("Rock", rockImg);
rockBtn.addActionListener(this);
paperBtn = new JButton("Paper", paperImg);
paperBtn.addActionListener(this);
scissorsBtn = new JButton("Scissors", scissorsImg);
scissorsBtn.addActionListener(this);
// exit button
exitBtn = new JButton(exitImg);
exitBtn.addActionListener(this);
buttonPanel.add(rockBtn);
buttonPanel.add(paperBtn);
buttonPanel.add(scissorsBtn);
buttonPanel.add(exitBtn);
// add panels to main panel
this.setLayout(new BorderLayout());
this.add(displayPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}// constructor
public void actionPerformed(ActionEvent e) {
Die hal = new Die(3);
Object source = e.getSource();
if (source == exitBtn)
System.exit(0);
else if (source == rockBtn) {
userTxt.setText(" Rock");
userChoice = ROCK;
hal.setRoll();
computerGuess = hal.getFaceValue();
switch (computerGuess) {
case ROCK:
compTxt.setText(" Rock");
break;
case PAPER:
compTxt.setText(" Paper");
break;
case SCISSORS:
compTxt.setText(" Scissors");
break;
default:
compTxt.setText(" error");
} // switch
compare(userChoice, computerGuess);
} // if Rock
else if (source == paperBtn) {
userTxt.setText(" Paper");
userChoice = PAPER;
hal.setRoll();
computerGuess = hal.getFaceValue();
switch (computerGuess) {
case ROCK:
compTxt.setText(" Rock");
break;
case PAPER:
compTxt.setText(" Paper");
break;
case SCISSORS:
compTxt.setText(" Scissors");
break;
default:
compTxt.setText(" error");
} // switch
compare(userChoice, computerGuess);
} // if Paper
else if (source == scissorsBtn) {
userTxt.setText(" Scissors");
userChoice = SCISSORS;
hal.setRoll();
computerGuess = hal.getFaceValue();
switch (computerGuess) {
case ROCK:
compTxt.setText(" Rock");
break;
case PAPER:
compTxt.setText(" Paper");
break;
case SCISSORS:
compTxt.setText(" Scissors");
break;
default:
compTxt.setText(" error");
} // switch
compare(userChoice, computerGuess);
} // if Scissors
else {
userTxt.setText("Directions"); //
showInstructions();
}
} // actionPerformed
public void compare(int user, int comp) {
Font ssBold16 = new Font("SansSerif", Font.BOLD, 16); // change to your
// fonts and colors, etc.
if (comp == user) {
outComeTxt.setFont(ssBold16);
outComeTxt.setText(" TIE. ");
ties++;
} else if ((computerGuess == ROCK && userChoice == SCISSORS)
|| (computerGuess == PAPER && userChoice == ROCK)
|| (computerGuess == SCISSORS && userChoice == PAPER)) {
outComeTxt.setFont(ssBold16);
outComeTxt.setText(" YOU LOSE."); // feel free to find a better
// algorithm for the above
losses++;
lossesTxt.setFont(ssBold16);
} else {
outComeTxt.setFont(ssBold16);
outComeTxt.setText(" YOU WIN.");
wins++;
}
tiesTxt.setFont(ssBold16);
tiesTxt.setText(" " + ties);
lossesTxt.setFont(ssBold16);
lossesTxt.setText(" " + losses);
winsTxt.setFont(ssBold16);
winsTxt.setText(" " + wins);
} // compare
public void showInstructions() {
outputArea = new JTextArea();
String output = "";
Font ssBold14 = new Font("SansSerif", Font.BOLD, 14);
output = "\n\n Welcome to my Rock, Paper, Scissors Game. Have fun and good luck!\n\n"; // add
// instructions
outputArea.setBorder(BorderFactory.createMatteBorder(15, 15, 15, 15,
Color.yellow)); // feel free to change colors, attributes, etc.
outputArea.setBackground(Color.blue);
outputArea.setForeground(Color.white);
outputArea.setFont(ssBold14);
outputArea.setText(output);
JOptionPane.showMessageDialog(null, outputArea,
"INSTRUCTIONS FOR ROCK, PAPER, SCISSORS GAME",
JOptionPane.PLAIN_MESSAGE);
} // showInstructions
} // class