How To Make a Java Rock Paper Scissors Game Project Using NetBeans
In this Java Tutorial we will see How to Make a Simple Rock, Paper, Scissors Game Using Jframe JPanels and Jlabels In Java NetBeans .
Images Links:
https://pixabay.com/vectors/stone-rock-nature-pebble-zen-576268/
https://pixabay.com/vectors/certificate-paper-parchment-roll-154169/
https://pixabay.com/vectors/scissors-cut-hairdresser-1297454/
OR
https://pixabay.com/vectors/rock-paper-scissors-rock-hand-296854/
https://pixabay.com/vectors/rock-paper-scissors-scissors-hand-296853/
https://pixabay.com/vectors/rock-paper-scissors-paper-hand-296855/
Project Source Code:
// create borders
Border gray_border = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.DARK_GRAY);
Border orange_border = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.ORANGE);
// images path
String rock = "/images/rock.png";
String paper = "/images/paper.png";
String scissors = "/images/scissors.png";
// create an arraylist
ArrayList<String> list = new ArrayList<>();
Random random = new Random();
int your_wins = 0, computer_wins = 0;
String random_selection;
// set borders
jLabel_You.setBorder(gray_border);
jLabel_Computer.setBorder(gray_border);
jLabel_Rock_.setBorder(gray_border);
jLabel_Paper_.setBorder(gray_border);
jLabel_Scissors.setBorder(gray_border);
// display images in jlabels
displayImage(rock, jLabel_Rock_, 110,90);
displayImage(paper, jLabel_Paper_, 110,90);
displayImage(scissors, jLabel_Scissors, 110,90);
// add items to the list
list.add(rock);
list.add(paper);
list.add(scissors);
// create a function to display images in jlabel
public void displayImage(String imagePath, JLabel label, int width, int height)
{
// get the image
ImageIcon imgIco = new ImageIcon(getClass().getResource(imagePath));
// resize the image
Image img = imgIco.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
// set image into the jLabel
label.setIcon(new ImageIcon(img));
}
// create a function to get the winner
public void getWinner(String you, String computer)
{
if(you.equals(computer))
{
System.out.println("Draw");
}
else if(you.equals(rock))
{
if(computer.equals(scissors))
{
your_wins++;
jLabel_YourWins.setText(String.valueOf(your_wins));
}
else
{
computer_wins++;
jLabel_ComputerWins.setText(String.valueOf(computer_wins));
}
}
else if(you.equals(paper))
{
if(computer.equals(rock))
{
your_wins++;
jLabel_YourWins.setText(String.valueOf(your_wins));
}
else
{
computer_wins++;
jLabel_ComputerWins.setText(String.valueOf(computer_wins));
}
}
else if(you.equals(scissors))
{
if(computer.equals(paper))
{
your_wins++;
jLabel_YourWins.setText(String.valueOf(your_wins));
}
else
{
computer_wins++;
jLabel_ComputerWins.setText(String.valueOf(computer_wins));
}
}
// you can stop when you or the computer reach 3 wins
// if you want to just keep playing
// remove this if section
/*
if(your_wins == 3 || computer_wins == 3)
{
int yes_no = JOptionPane.showConfirmDialog(null, "Play a New Partie", "Play Again", JOptionPane.YES_NO_OPTION);
if(yes_no == 0)// yes == 0
{
jLabel_Computer.setIcon(null);
jLabel_You.setIcon(null);
your_wins = 0;
computer_wins = 0;
jLabel_YourWins.setText(String.valueOf(your_wins));
jLabel_ComputerWins.setText(String.valueOf(computer_wins));
}
}*/
}
private void jLabel_Rock_MouseClicked(java.awt.event.MouseEvent evt) {
// display rock image
displayImage(rock, jLabel_You,160,120);
// make a random selection
random_selection = list.get(random.nextInt(list.size()));
// display image
displayImage(random_selection, jLabel_Computer,160,120);
getWinner(rock, random_selection);
}
private void jLabel_Paper_MouseClicked(java.awt.event.MouseEvent evt) {
// display paper image
displayImage(paper, jLabel_You,160,120);
// make a random selection
random_selection = list.get(random.nextInt(list.size()));
// display image
displayImage(random_selection, jLabel_Computer,160,120);
getWinner(paper, random_selection);
}
private void jLabel_ScissorsMouseClicked(java.awt.event.MouseEvent evt) {
// display scissors image
displayImage(scissors, jLabel_You,160,120);
// make a random selection
random_selection = list.get(random.nextInt(list.size()));
// display image
displayImage(random_selection, jLabel_Computer,160,120);
getWinner(scissors, random_selection);
}
private void jLabel_Rock_MouseEntered(java.awt.event.MouseEvent evt) {
// change border
jLabel_Rock_.setBorder(orange_border);
}
private void jLabel_Rock_MouseExited(java.awt.event.MouseEvent evt) {
// change border
jLabel_Rock_.setBorder(gray_border);
}
private void jLabel_Paper_MouseEntered(java.awt.event.MouseEvent evt) {
// change border
jLabel_Paper_.setBorder(orange_border);
}
private void jLabel_Paper_MouseExited(java.awt.event.MouseEvent evt) {
// change border
jLabel_Paper_.setBorder(gray_border);
}
private void jLabel_ScissorsMouseEntered(java.awt.event.MouseEvent evt) {
// change border
jLabel_Scissors.setBorder(orange_border);
}
private void jLabel_ScissorsMouseExited(java.awt.event.MouseEvent evt) {
// change border
jLabel_Scissors.setBorder(gray_border);
}
////// OUTPUT :
More Java Projects:
Download Projects Source Code
1 comments:
commentssource code is not complete...hayystt sad
Reply