import java.awt.event.*;
import java.awt.*;
import java.util.Random;
class rock extends Frame implements MouseListener {
Image rockImg, paperImg, scissorImg;
Canvas rockCanvas, paperCanvas, scissorCanvas;
Label resultLabel,computerLabel;
String userChoice, computerChoice;
Random random;
rock() {
random = new Random();
setSize(600, 600);
setTitle("Rock Paper Scissor Game");
setLayout(null);
setBackground(new Color(135, 206, 250));
Toolkit toolkit = Toolkit.getDefaultToolkit();
rockImg = toolkit.getImage("rock.png");
paperImg = toolkit.getImage("paper.png");
scissorImg = toolkit.getImage("scissors.png");
rockCanvas = new Canvas() {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawRect(0, 0, 100, 100);
g.drawImage(rockImg, 0, 0, 100, 100, this);
}
};
rockCanvas.setBounds(50, 250, 100, 100);
rockCanvas.addMouseListener(this);
add(rockCanvas);
paperCanvas = new Canvas() {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawRect(0, 0, 100, 100);
g.drawImage(paperImg, 0, 0, 100, 100, this);
}
};
paperCanvas.setBounds(250, 250, 100, 100);
paperCanvas.addMouseListener(this);
add(paperCanvas);
scissorCanvas = new Canvas() {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawRect(0, 0, 100, 100);
g.drawImage(scissorImg, 0, 0, 100, 100, this);
}
};
scissorCanvas.setBounds(450, 250, 100, 100);
scissorCanvas.addMouseListener(this);
add(scissorCanvas);
resultLabel = new Label("");
resultLabel.setBounds(300, 450, 200, 50);
resultLabel.setBackground(new Color(255, 215, 0));
resultLabel.setForeground(Color.RED);
resultLabel.setFont(new Font("Arial", Font.BOLD, 16));
resultLabel.setAlignment(Label.CENTER);
add(resultLabel);
computerLabel = new Label("");
computerLabel.setBounds(100, 450, 200, 50);
computerLabel.setBackground(Color.RED);
computerLabel.setFont(new Font("Arial", Font.BOLD, 16));
computerLabel.setAlignment(Label.CENTER);
add(computerLabel);
setVisible(true);
setComputerChoice();
}
private void setComputerChoice() {
int b = random.nextInt(3);
if (b == 0)
computerChoice = "rock";
else if (b == 1)
computerChoice = "paper";
else
computerChoice = "scissor";
}
public void mouseClicked(MouseEvent e) {
if (e.getSource() == rockCanvas) {
userChoice = "rock";
} else if (e.getSource() == paperCanvas) {
userChoice = "paper";
} else if (e.getSource() == scissorCanvas) {
userChoice = "scissor";
}
String result = determineWinner(userChoice, computerChoice);
resultLabel.setText(result);
computerLabel.setText(computerChoice+" ");
setComputerChoice();
}
public String determineWinner(String userChoice, String computerChoice) {
if (userChoice == computerChoice) {
return "It's a tie!";
}
switch (userChoice) {
case "rock":
return (computerChoice == "scissor") ? "You win!" : "You lose!";
case "paper":
return (computerChoice == "rock") ? "You win!" : "You lose!";
case "scissor":
return (computerChoice == "paper") ? "You win!" : "You lose!";
default:
return "Invalid choice!";
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public static void main(String args[]) {
new rock();
}
}