import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class hangmanGUI extends JFrame implements ActionListener
{
private hangman hangman = new hangman();
private String selectedWord = new String();
private String disguisedWord = new String();
private Vector<String> words = new Vector<String>();
private Button wordCheck;
private Button letterCheck;
private JTextArea letterIn;
private JTextArea wordIn;
private int guesses;
public void gameLoop()throws IOException
{
JFrame hangFrame = new JFrame();
JPanel main = new JPanel();
main.setLayout(new BorderLayout());
words = hangman.load();//retrives a vector of words from the text file
guesses = 10;//sets the number of guesses a person can make
selectedWord = hangman.selectWord(words);//randomly (pseudorandom) chosses a single word from a vector
disguisedWord = hangman.disguseWord(selectedWord);//takes selected words and converts into a string of '-' for displaying to the player
JPanel displayBar = new JPanel();
displayBar.setLayout(new BorderLayout());
JLabel guessCount = new JLabel("You have " + guesses + " guesses left ");
JLabel wordClue = new JLabel("Your Guessing the Word " + disguisedWord);
guessCount.setFont(new Font("Arial", Font.PLAIN, 12));
wordClue.setFont(new Font("Arial", Font.PLAIN, 12));
displayBar.add(guessCount, BorderLayout.EAST);
displayBar.add(wordClue, BorderLayout.WEST);
main.add(displayBar, BorderLayout.NORTH);
JPanel guessBar = new JPanel();
guessBar.setLayout(new BorderLayout());
Button letterCheck = new Button("Guess a letter");
letterCheck.addActionListener(this);
JTextArea wordIn = new JTextArea(1, 20);
JTextArea letterIn = new JTextArea(1, 20);
guessBar.add(letterIn, BorderLayout.NORTH);
guessBar.add(letterCheck, BorderLayout.SOUTH);
main.add(guessBar, BorderLayout.SOUTH);
if(guesses == 11)
{
String path = "hangEleven.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else if(guesses == 10)
{
String path = "hangZero.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else if(guesses == 9)
{
String path = "hangOne.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else if(guesses == 8)
{
String path = "hangTwo.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else if(guesses == 7)
{
String path = "hangThree.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else if(guesses == 6)
{
String path = "hangFour.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else if(guesses == 5)
{
String path = "hangFive.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else if(guesses == 4)
{
String path = "hangSix.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else if(guesses == 3)
{
String path = "hangSeven.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else if(guesses == 2)
{
String path = "hangEight.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else if(guesses == 1)
{
String path = "hangNine.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else if(guesses == 1)
{
String path = "hangTen.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
else
{
String path = "hangTen.jpg";
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel hangingMan = new JLabel(new ImageIcon(image));
main.add(hangingMan, BorderLayout.CENTER);
}
hangFrame.setSize(400,300);
hangFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hangFrame.add(main);
hangFrame.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
String inputChar = letterIn.getText();
Character letterGuess = inputChar.charAt(0);
Vector<Integer> indexChar = new Vector<Integer>();
indexChar = hangman.checkLetter(letterGuess, selectedWord, selectedWord.length());
String newDisguise = hangman.revealWord(indexChar, letterGuess, disguisedWord);
if (indexChar.isEmpty())
{
guesses = guesses - 1;
}
else
{
disguisedWord = newDisguise;
}
}
}