Main Java class
Images used
1 package rpslsgui; 2 3 import javax.swing.JFrame; 4 5 6 public class RPSLSgui { 7 8 public static void main(String[] args) { 9 10 JFrame frame = new JFrame("Rock, Paper, Scissors, Lizard or Spock ( tonynsx@gmail.com )"); 11 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 frame.getContentPane().add(new RPSLSpanel()); 13 frame.setResizable(false); 14 frame.pack(); 15 frame.setVisible(true); 16 } 17 } 18
RPSLS panel gui class
1 package rpslsgui; 2 3 import java.awt.Color; 4 import java.awt.Dimension; 5 import java.awt.Font; 6 import java.awt.event.MouseEvent; 7 import java.awt.event.MouseListener; 8 import javax.swing.ImageIcon; 9 import javax.swing.JLabel; 10 import javax.swing.JPanel; 11 import java.util.Random; 12 13 public class RPSLSpanel extends JPanel{ 14 15 Random rand = new Random(); 16 17 int comp, winScore, loseScore, tieScore; 18 19 ImageIcon[] imageArray = {new ImageIcon("rock.jpg"), new ImageIcon("paper.jpg"), new ImageIcon("scissors.jpg"), 20 new ImageIcon("lizard.jpg"), new ImageIcon("spock.jpg")}; 21 JLabel title, instruction, option1, option2, option3, option4, option5, player, computer, playerTitle, computerTitle, wins, loses, ties, wL, lL, tL; 22 JPanel imagePanel, playerPanel, computerPanel, scorePanel; 23 24 public RPSLSpanel() 25 { 26 imagePanel = new JPanel(); 27 playerPanel = new JPanel(); 28 computerPanel = new JPanel(); 29 scorePanel = new JPanel(); 30 31 title = new JLabel("Please click on the image to make a choice, then computer will make a random choice."); 32 instruction = new JLabel(new ImageIcon("howto.jpg")); 33 34 option1 = new JLabel(new ImageIcon("rock.jpg")); 35 option1.addMouseListener(new mouseListener()); 36 option2 = new JLabel(new ImageIcon("paper.jpg")); 37 option2.addMouseListener(new mouseListener()); 38 option3 = new JLabel(new ImageIcon("scissors.jpg")); 39 option3.addMouseListener(new mouseListener()); 40 option4 = new JLabel(new ImageIcon("lizard.jpg")); 41 option4.addMouseListener(new mouseListener()); 42 option5 = new JLabel(new ImageIcon("spock.jpg")); 43 option5.addMouseListener(new mouseListener()); 44 45 player = new JLabel(new ImageIcon("blank.jpg")); 46 computer = new JLabel(new ImageIcon("blank.jpg")); 47 48 playerTitle = new JLabel("PLAYER"); 49 computerTitle = new JLabel("COMPUTER"); 50 51 wins = new JLabel("Wins:"); 52 wins.setForeground(Color.red); 53 wins.setFont(new Font("Helvetica", Font.BOLD, 36)); 54 loses = new JLabel("Loses:"); 55 loses.setForeground(Color.red); 56 loses.setFont(new Font("Helvetica", Font.BOLD, 36)); 57 ties = new JLabel("Ties:"); 58 ties.setForeground(Color.red); 59 ties.setFont(new Font("Helvetica", Font.BOLD, 36)); 60 wL = new JLabel(""+winScore); 61 wL.setFont(new Font("Helvetica", Font.BOLD, 36)); 62 lL = new JLabel(""+loseScore); 63 lL.setFont(new Font("Helvetica", Font.BOLD, 36)); 64 tL = new JLabel(""+tieScore); 65 tL.setFont(new Font("Helvetica", Font.BOLD, 36)); 66 67 add(instruction); 68 add(title); 69 70 imagePanel.add(option1); 71 imagePanel.add(option2); 72 imagePanel.add(option3); 73 imagePanel.add(option4); 74 imagePanel.add(option5); 75 add(imagePanel); 76 imagePanel.setBackground(Color.WHITE); 77 78 playerPanel.add(playerTitle); 79 playerPanel.add(player); 80 computerPanel.add(computerTitle); 81 computerPanel.add(computer); 82 add(playerPanel); 83 add(computerPanel); 84 playerPanel.setBackground(Color.WHITE); 85 computerPanel.setBackground(Color.WHITE); 86 87 scorePanel.add(wins); 88 scorePanel.add(wL); 89 scorePanel.add(loses); 90 scorePanel.add(lL); 91 scorePanel.add(ties); 92 scorePanel.add(tL); 93 add(scorePanel); 94 scorePanel.setBackground(Color.WHITE); 95 96 winScore = loseScore = tieScore = 0; 97 98 setPreferredSize(new Dimension(550, 575)); 99 setBackground(Color.WHITE); 100 } 101 public class mouseListener implements MouseListener 102 { 103 104 @Override 105 public void mouseClicked(MouseEvent e) 106 { 107 if(e.getSource() == option1) 108 { 109 player.setIcon(imageArray[0]); 110 comp = rand.nextInt(imageArray.length)+0; 111 computer.setIcon(imageArray[comp]); 112 } 113 if(e.getSource() == option2) 114 { 115 player.setIcon(imageArray[1]); 116 comp = rand.nextInt(imageArray.length)+0; 117 computer.setIcon(imageArray[comp]); 118 } 119 if(e.getSource() == option3) 120 { 121 player.setIcon(imageArray[2]); 122 comp = rand.nextInt(imageArray.length)+0; 123 computer.setIcon(imageArray[comp]); 124 } 125 if(e.getSource() == option4) 126 { 127 player.setIcon(imageArray[3]); 128 comp = rand.nextInt(imageArray.length)+0; 129 computer.setIcon(imageArray[comp]); 130 } 131 if(e.getSource() == option5) 132 { 133 player.setIcon(imageArray[4]); 134 comp = rand.nextInt(imageArray.length)+0; 135 computer.setIcon(imageArray[comp]); 136 } 137 138 // calculating wins 139 if(player.getIcon() == imageArray[4] && computer.getIcon() == imageArray[2] || 140 player.getIcon() == imageArray[2] && computer.getIcon() == imageArray[1] || 141 player.getIcon() == imageArray[1] && computer.getIcon() == imageArray[0] || 142 player.getIcon() == imageArray[0] && computer.getIcon() == imageArray[3] || 143 player.getIcon() == imageArray[3] && computer.getIcon() == imageArray[4] || 144 player.getIcon() == imageArray[1] && computer.getIcon() == imageArray[3] || 145 player.getIcon() == imageArray[1] && computer.getIcon() == imageArray[4] || 146 player.getIcon() == imageArray[0] && computer.getIcon() == imageArray[2] || 147 player.getIcon() == imageArray[4] && computer.getIcon() == imageArray[0] || 148 player.getIcon() == imageArray[3] && computer.getIcon() == imageArray[1]) 149 { 150 winScore++; 151 wL.setText(""+winScore); 152 } 153 154 // calculating loses 155 if(player.getIcon() == imageArray[0] && computer.getIcon() == imageArray[1] || 156 player.getIcon() == imageArray[0] && computer.getIcon() == imageArray[4] || 157 player.getIcon() == imageArray[1] && computer.getIcon() == imageArray[2] || 158 player.getIcon() == imageArray[2] && computer.getIcon() == imageArray[0] || 159 player.getIcon() == imageArray[2] && computer.getIcon() == imageArray[3] || 160 player.getIcon() == imageArray[2] && computer.getIcon() == imageArray[4] || 161 player.getIcon() == imageArray[3] && computer.getIcon() == imageArray[0] || 162 player.getIcon() == imageArray[3] && computer.getIcon() == imageArray[2] || 163 player.getIcon() == imageArray[4] && computer.getIcon() == imageArray[1] || 164 player.getIcon() == imageArray[4] && computer.getIcon() == imageArray[3]) 165 { 166 loseScore++; 167 lL.setText(""+loseScore); 168 } 169 170 // if they're both same 171 if(player.getIcon() == computer.getIcon()) 172 { 173 tieScore++; 174 tL.setText(""+tieScore); 175 } 176 } 177 178 @Override 179 public void mousePressed(MouseEvent e) {} 180 181 @Override 182 public void mouseReleased(MouseEvent e) {} 183 184 @Override 185 public void mouseEntered(MouseEvent e) {} 186 187 @Override 188 public void mouseExited(MouseEvent e) {} 189 } 190 } 191 192 193
No comments:
Post a Comment