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;
char userChoice, computerChoice;
Random random;
rock() {
random = new Random();
setSize(600, 600);
setTitle("Rock Paper Scissor Game");
setLayout(null);
// Set vibrant background color
setBackground(new Color(135, 206, 250)); // Light Sky Blue background
// Load images
Toolkit toolkit = Toolkit.getDefaultToolkit();
rockImg = toolkit.getImage("rock.png"); // Load rock image
paperImg = toolkit.getImage("paper.png"); // Load paper image
scissorImg = toolkit.getImage("scissors.png"); // Load scissor image
// Create canvases to display images with borders
rockCanvas = new Canvas() {
public void paint(Graphics g) {
g.setColor(Color.BLACK); // Border color
g.drawRect(0, 0, 100, 100); // Border around image
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); // Border color
g.drawRect(0, 0, 100, 100); // Border around image
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); // Border color
g.drawRect(0, 0, 100, 100); // Border around image
g.drawImage(scissorImg, 0, 0, 100, 100, this);
}
};
scissorCanvas.setBounds(450, 250, 100, 100);
scissorCanvas.addMouseListener(this);
add(scissorCanvas);
// Result label with vibrant colors
resultLabel = new Label("");
resultLabel.setBounds(200, 450, 200, 50);
resultLabel.setBackground(new Color(255, 215, 0)); // Gold background
resultLabel.setForeground(Color.RED); // Red text
resultLabel.setFont(new Font("Arial", Font.BOLD, 16));
resultLabel.setAlignment(Label.CENTER);
add(resultLabel);
setVisible(true);
// Random computer choice for each game
setComputerChoice();
}
// Randomly set the computer's choice
private void setComputerChoice() {
int b = random.nextInt(3);
if (b == 0)
computerChoice = 'r';
else if (b == 1)
computerChoice = 'p';
else
computerChoice = 's';
}
// Handle user clicks on the images
public void mouseClicked(MouseEvent e) {
if (e.getSource() == rockCanvas) {
userChoice = 'r';
} else if (e.getSource() == paperCanvas) {
userChoice = 'p';
} else if (e.getSource() == scissorCanvas) {
userChoice = 's';
}
// Determine the winner and display result
String result = determineWinner(userChoice, computerChoice);
resultLabel.setText(result);
// Reset computer choice for next round
setComputerChoice();
}
public String determineWinner(char userChoice, char computerChoice) {
if (userChoice == computerChoice) {
return "It's a tie!";
}
switch (userChoice) {
case 'r':
return (computerChoice == 's') ? "You win!" : "You lose!";
case 'p':
return (computerChoice == 'r') ? "You win!" : "You lose!";
case 's':
return (computerChoice == 'p') ? "You win!" : "You lose!";
default:
return "Invalid choice!";
}
}
// Required methods for MouseListener (empty implementations)
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();
}
}