Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Daniel Zarco (dz7as) and Sean Wolfe (scw2tt)
- // Lab 7 10/16/2017
- import java.awt.Font;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- public class GUILLOfFortune extends JFrame {
- JLabel instructions;
- JButton submit;
- JTextArea enterArea;
- JLabel livesRemaining;
- JLabel lettersEntered;
- JLabel wordOutput;
- boolean[] lettersCorrect = {false, false, false, false, false};
- int lives;
- String password;
- ArrayList<String> letters;
- public static void main(String[] args) {
- new GUILLOfFortune();
- }
- public GUILLOfFortune()
- {
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
- int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
- this.setSize(width, height);
- password = "super";
- lives = 5;
- letters = new ArrayList<String>();
- instructions = new JLabel("<html>Enter a letter to see if it's in the word!</html>" );
- submit = new JButton("ENTER");
- enterArea = new JTextArea();
- livesRemaining = new JLabel("Lives: " + lives);
- lettersEntered = new JLabel("<html>Letters Entered: " + letters + "</html>");
- wordOutput = new JLabel("_ _ _ _ _");
- wordOutput.setFont(new Font("Times", Font.BOLD, 20));
- instructions.setSize(23*width/60, height/6);
- submit.setSize(width/3, 50);
- enterArea.setSize(width/3, 50);
- livesRemaining.setSize(width/4, height/12);
- lettersEntered.setSize(width/3, height/3);
- wordOutput.setSize(width/2, height/6);
- instructions.setLocation(width/15, 1);
- submit.setLocation(width/2, height/2);
- enterArea.setLocation(width/6, height/2);
- livesRemaining.setLocation(11*width/15, 1);
- lettersEntered.setLocation(17*width/30, height/20);
- wordOutput.setLocation(width/3, height/3);
- submit.addActionListener(new passwordButtonListener());
- this.add(instructions);
- this.add(submit);
- this.add(enterArea);
- this.add(livesRemaining);
- this.add(lettersEntered);
- this.add(wordOutput);
- this.add(new JLabel());
- this.setLocationRelativeTo(null);
- setVisible(true);
- }
- private class passwordButtonListener implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // take the input from one character
- String letter = enterArea.getText().substring(0, 1);
- // change the background color
- //System.out.println(letter);
- // verify that the character has not been submitted, use a set
- if (!letters.contains(letter)){
- letters.add(letter);
- // display the letter
- lettersEntered.setText("<html>Letters Entered: " + letters + "</html>");
- // see if it is in the answer
- if (password.contains(letter)){
- int index = password.indexOf(letter);
- // update wordOutput
- String newWord = wordOutput.getText();
- String newWord1 = newWord.substring(0 , 2* index) + letter + newWord.substring(2* index + 1);
- wordOutput.setText(newWord1);
- System.out.println(newWord1);
- // take away a life if it is not correct
- } else {
- lives --;
- livesRemaining.setText("lives " + lives);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement