Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. // Daniel Zarco (dz7as) and Sean Wolfe (scw2tt)
  2. // Lab 7 10/16/2017
  3.  
  4.  
  5. import java.awt.Font;
  6. import java.awt.Toolkit;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.util.ArrayList;
  10.  
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15. import javax.swing.JTextArea;
  16.  
  17. public class GUILLOfFortune extends JFrame {
  18. JLabel instructions;
  19. JButton submit;
  20. JTextArea enterArea;
  21.  
  22. JLabel livesRemaining;
  23. JLabel lettersEntered;
  24. JLabel wordOutput;
  25.  
  26. boolean[] lettersCorrect = {false, false, false, false, false};
  27. int lives;
  28. String password;
  29. ArrayList<String> letters;
  30.  
  31. public static void main(String[] args) {
  32. new GUILLOfFortune();
  33. }
  34.  
  35. public GUILLOfFortune()
  36. {
  37. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  38. int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
  39. int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
  40. this.setSize(width, height);
  41.  
  42. password = "super";
  43. lives = 5;
  44. letters = new ArrayList<String>();
  45.  
  46. instructions = new JLabel("<html>Enter a letter to see if it's in the word!</html>" );
  47. submit = new JButton("ENTER");
  48. enterArea = new JTextArea();
  49. livesRemaining = new JLabel("Lives: " + lives);
  50. lettersEntered = new JLabel("<html>Letters Entered: " + letters + "</html>");
  51. wordOutput = new JLabel("_ _ _ _ _");
  52. wordOutput.setFont(new Font("Times", Font.BOLD, 20));
  53.  
  54. instructions.setSize(23*width/60, height/6);
  55. submit.setSize(width/3, 50);
  56. enterArea.setSize(width/3, 50);
  57. livesRemaining.setSize(width/4, height/12);
  58. lettersEntered.setSize(width/3, height/3);
  59. wordOutput.setSize(width/2, height/6);
  60.  
  61. instructions.setLocation(width/15, 1);
  62. submit.setLocation(width/2, height/2);
  63. enterArea.setLocation(width/6, height/2);
  64. livesRemaining.setLocation(11*width/15, 1);
  65. lettersEntered.setLocation(17*width/30, height/20);
  66. wordOutput.setLocation(width/3, height/3);
  67.  
  68. submit.addActionListener(new passwordButtonListener());
  69.  
  70. this.add(instructions);
  71. this.add(submit);
  72. this.add(enterArea);
  73. this.add(livesRemaining);
  74. this.add(lettersEntered);
  75. this.add(wordOutput);
  76.  
  77. this.add(new JLabel());
  78. this.setLocationRelativeTo(null);
  79.  
  80. setVisible(true);
  81. }
  82.  
  83. private class passwordButtonListener implements ActionListener
  84. {
  85.  
  86. @Override
  87. public void actionPerformed(ActionEvent arg0) {
  88. // take the input from one character
  89. String letter = enterArea.getText().substring(0, 1);
  90.  
  91. // change the background color
  92.  
  93.  
  94. //System.out.println(letter);
  95.  
  96. // verify that the character has not been submitted, use a set
  97. if (!letters.contains(letter)){
  98. letters.add(letter);
  99. // display the letter
  100. lettersEntered.setText("<html>Letters Entered: " + letters + "</html>");
  101.  
  102. // see if it is in the answer
  103. if (password.contains(letter)){
  104.  
  105. int index = password.indexOf(letter);
  106. // update wordOutput
  107. String newWord = wordOutput.getText();
  108. String newWord1 = newWord.substring(0 , 2* index) + letter + newWord.substring(2* index + 1);
  109.  
  110. wordOutput.setText(newWord1);
  111. System.out.println(newWord1);
  112. // take away a life if it is not correct
  113. } else {
  114. lives --;
  115. livesRemaining.setText("lives " + lives);
  116. }
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. }
  124.  
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement