Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.Border;
  5. import java.io.*;
  6.  
  7. public class rps {
  8. //ROCK PAPER SCISSORS
  9. static JLabel middle = new JLabel();
  10. static JLabel them = new JLabel();
  11. static JLabel yourWins = new JLabel();
  12. static JLabel theirWins = new JLabel();
  13. static JPanel yourPanel = new JPanel();
  14. static JPanel middlePanel = new JPanel();
  15. static JLabel blank1 = new JLabel();
  16. static JLabel blank2 = new JLabel();
  17. static JButton rock = new JButton("Rock");
  18. static JButton paper = new JButton("Paper");
  19. static JButton scissors = new JButton("Scissors");
  20. static int yw = 0;
  21. static int tw = 0;
  22. static ButtonHandler listener = new ButtonHandler();
  23.  
  24. public static void main(String[] args) {
  25.  
  26. //Create the frame
  27. JFrame frame = new JFrame("Rock Paper Scissors");
  28. frame.setSize(500, 500); //Setting the size of the frame
  29.  
  30. middle.setFont(new Font("Serif", Font.PLAIN, 30));
  31. middle.setHorizontalAlignment(SwingConstants.CENTER);
  32. them.setFont(new Font("Serif", Font.PLAIN, 15));
  33. them.setHorizontalAlignment(SwingConstants.CENTER);
  34. yourWins.setHorizontalAlignment(SwingConstants.CENTER);
  35. theirWins.setHorizontalAlignment(SwingConstants.CENTER);
  36.  
  37. //Creating panels
  38. JPanel bigPanel = new JPanel();
  39.  
  40. Border border = BorderFactory.createLineBorder(Color.BLACK, 1);
  41. Border wlb = BorderFactory.createLineBorder(Color.RED, 1);
  42. them.setBorder(border);
  43. yourPanel.setBorder(border);
  44. bigPanel.setBorder(border);
  45. yourWins.setBorder(wlb);
  46. theirWins.setBorder(wlb);
  47. middlePanel.setBorder(border);
  48.  
  49. //Creating grid layouts
  50. GridLayout yourGrid = new GridLayout(1,3,10,10);
  51. GridLayout theirGrid = new GridLayout(1,1); //One row, one column
  52. GridLayout middleGrid = new GridLayout(5,1);
  53. GridLayout bigGrid = new GridLayout(3,1);//Two rows, one column
  54.  
  55. //Setting the layouts of each panel to the grid layouts created above
  56. yourPanel.setLayout(yourGrid); //Adding layout to buttons panel
  57. them.setLayout(theirGrid); //Adding layout to label panel
  58. middlePanel.setLayout(middleGrid);
  59. bigPanel.setLayout(bigGrid);
  60.  
  61. //Adding r/p/s to your grid.
  62. yourPanel.add(rock);
  63. yourPanel.add(paper);
  64. yourPanel.add(scissors);
  65.  
  66. //Adding w/l rations to middlegrid.
  67. middlePanel.add(theirWins);
  68. middlePanel.add(blank1);
  69. middlePanel.add(middle);
  70. middlePanel.add(blank2);
  71. middlePanel.add(yourWins);
  72.  
  73. //Attaching the listener to all the buttons
  74. rock.addActionListener(listener);
  75. paper.addActionListener(listener);
  76. scissors.addActionListener(listener);
  77.  
  78. bigPanel.add(them);
  79. bigPanel.add(middlePanel);
  80. bigPanel.add(yourPanel);
  81.  
  82. //Shows the score at 0-0.
  83. yourWins.setText("Your wins: " + yw);
  84. theirWins.setText("Their wins: " + tw);
  85.  
  86. frame.getContentPane().add(bigPanel); //panel to frame
  87. frame.setVisible(true); // Shows frame on screen
  88. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  89. }
  90.  
  91. //Class represents what do when a button is pressed
  92. private static class ButtonHandler implements ActionListener {
  93. public void actionPerformed (ActionEvent e) {
  94. Timer timer = new Timer(1000, this);
  95.  
  96. String tc = random();
  97. them.setText("They chose: " + tc + "!");
  98. if (e.getSource() == rock) {
  99. whoWins("rock", tc);
  100. } else if (e.getSource() == paper) {
  101. whoWins("paper", tc);
  102. } else if (e.getSource() == scissors) {
  103. whoWins("scissors", tc);
  104. }
  105. yourWins.setText("Your wins: " + yw);
  106. theirWins.setText("Their wins: " + tw);
  107.  
  108. timer.setRepeats(false);
  109. timer.start();
  110. }
  111. }
  112.  
  113. public static String random() {
  114. int random = (int) (Math.random() * 3);
  115. if (random == 0) {
  116. return "Rock";
  117. } else if (random == 1) {
  118. return "Paper";
  119. } else if (random == 2) {
  120. return "Scissors";
  121. }
  122. return "";
  123. }
  124.  
  125. public static void whoWins(String yc, String tc) {
  126. if (yc.equals("rock")) {
  127. if (tc.equals("Rock")) {
  128. middle.setText("It's a tie!");
  129. } else if (tc.equals("Paper")) {
  130. middle.setText("You lose!");
  131. tw++;
  132. } else if (tc.equals("Scissors")) {
  133. middle.setText("You win!");
  134. yw++;
  135. }
  136. } else if (yc.equals("paper")) {
  137. if (tc.equals("Rock")) {
  138. middle.setText("You win!");
  139. yw++;
  140. } else if (tc.equals("Paper")) {
  141. middle.setText("It's a tie!");
  142. } else if (tc.equals("Scissors")) {
  143. middle.setText("You lose!");
  144. tw++;
  145. }
  146. } else if (yc.equals("scissors")) {
  147. if (tc.equals("Rock")) {
  148. middle.setText("You lose!");
  149. tw++;
  150. } else if (tc.equals("Paper")) {
  151. middle.setText("You win!");
  152. yw++;
  153. } else if (tc.equals("Scissors")) {
  154. middle.setText("It's a tie!");
  155. }
  156. }
  157. }
  158. }
  159.  
  160. them.setText("They chose: " + tc + "!");
  161.  
  162. private static class ButtonHandler implements ActionListener {
  163. public void actionPerformed (ActionEvent e) {
  164. // I'd be disabling the buttons here to prevent
  165. // the user from trying to trigger another
  166. // update...
  167.  
  168. // This is an instance field which is used by your
  169. // listener
  170.  
  171. Timer timer = new Timer(1000, listenert);
  172. timer.setRepeats(false);
  173. timer.start();
  174. }
  175. }
  176. private static class timer implements ActionListener {
  177. public void actionPerformed (ActionEvent e) {
  178. String tc = random(); //A method that chooses a random word.
  179. them.setText("They chose: " + tc + "!");
  180. if (e.getSource() == rock) {
  181. whoWins("rock", tc); //whoWins is a method that will display a message.
  182. } else if (e.getSource() == paper) {
  183. whoWins("paper", tc);
  184. } else if (e.getSource() == scissors) {
  185. whoWins("scissors", tc);
  186. }
  187. yourWins.setText("Your wins: " + yw);
  188. theirWins.setText("Their wins: " + tw);
  189.  
  190. // Start another Timer here that waits 1 second
  191. // and re-enables the other buttons...
  192. }
  193. }
  194.  
  195. private static class ButtonHandler implements ActionListener {
  196. public void actionPerformed (ActionEvent e) {
  197. final JButton button = (JButton)e.getSource();
  198. Timer timer = new Timer(1000, new ActionListener() {
  199. public void actionPerformed(ActionEvent e) {
  200. String tc = random();
  201. them.setText("They chose: " + tc + "!");
  202. if (button == rock) {
  203. whoWins("rock", tc);
  204. } else if (button == paper) {
  205. whoWins("paper", tc);
  206. } else if (button == scissors) {
  207. whoWins("scissors", tc);
  208. }
  209. yourWins.setText("Your wins: " + yw);
  210. theirWins.setText("Their wins: " + tw);
  211. }
  212. });
  213. timer.setRepeats(false);
  214. timer.start();
  215.  
  216. }
  217. }
  218.  
  219. public void actionPerformed (ActionEvent e) {
  220. // I'd be disabling the buttons here to prevent
  221. // the user from trying to trigger another
  222. // update...
  223.  
  224. // This is an instance field which is used by your
  225. // listener
  226. choice = e.getSource();
  227.  
  228. Timer timer = new Timer(1000, listener);
  229. timer.setRepeats(false);
  230. timer.start();
  231. }
  232.  
  233. public void actionPerformed (ActionEvent e) {
  234. String tc = random(); //A method that chooses a random word.
  235. them.setText("They chose: " + tc + "!");
  236. if (choice == rock) {
  237. whoWins("rock", tc); //whoWins is a method that will display a message.
  238. } else if (choice == paper) {
  239. whoWins("paper", tc);
  240. } else if (choice == scissors) {
  241. whoWins("scissors", tc);
  242. }
  243. yourWins.setText("Your wins: " + yw);
  244. theirWins.setText("Their wins: " + tw);
  245.  
  246. // Start another Timer here that waits 1 second
  247. // and re-enables the other buttons...
  248. }
  249.  
  250. private JLabel label;
  251. private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  252.  
  253. public TestPane() {
  254. setLayout(new GridBagLayout());
  255. label = new JLabel();
  256. add(label);
  257. tick();
  258.  
  259. Timer timer = new Timer(500, new ActionListener() {
  260. @Override
  261. public void actionPerformed(ActionEvent e) {
  262. tick();
  263. }
  264. });
  265. timer.start();
  266. }
  267.  
  268. protected void tick() {
  269.  
  270. label.setText(sdf.format(new Date()));
  271.  
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement