Hi, I've decided to make a hangman game. I made this in Python last year, however I'm a little bit stick in Java, since I'm quite new to as well as Swing.

Anyway, the problem I'm having is that my while loop keeps terminating if I enter 1 incorrect letter, but it is fine if I keep entering the letters which are in the chosen word.

I think i've made drawing the line a bit too complicated but it's seem to be working fine.

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

/** class to display a JFrame object using border layout */

public class HangmanJFrame extends JFrame implements ActionListener {

	private JPanel centerPanel;
	private JPanel southPanel;
	private JTextField textField;
	private LinePanel line;
	private String [] wordList = {"computer","java"}; // word list
	private ArrayList<String> usedLetter = new ArrayList(); // list of used letter by user
	private ArrayList<String> correctGuesses;  // list of users correct guesses
	private int numLives = 6; // number of lives
	public String theWord; // the wrong which is chosen
	private String userInput = "";
	
	// no-argument constructor
	public HangmanJFrame() { 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		theWord = pickWord();
		correctGuesses = new ArrayList(theWord.length());

		setSize(600,500);
		setLocation(100, 100);
		setTitle("Hangman Game");
		setLayout(new BorderLayout());

		centerPanel = new JPanel();
		
		line = new LinePanel(0,theWord);
		centerPanel.setSize(500,500);
		centerPanel.setBackground(Color.PINK);
		centerPanel.add(line);
		add(centerPanel, BorderLayout.CENTER);
		
		textField = new JTextField(20);
		textField.addActionListener(this);

		southPanel = new JPanel();
		southPanel.setBackground(Color.CYAN);
		southPanel.add(textField);
		add(southPanel, BorderLayout.SOUTH);

	}

	// Picks a word, latter it will be picked randomly.
	private String pickWord(){
		return wordList[0];
	}

	// This method check wither the input is valid
	// i.e. its in the alphabet.
	private boolean checkInput(String s){
		String [] alphabet = {"a","b","c","d","e","f",
				"g","h","i","j","k","l","m","n","o","p",
				"q","r","s","t","u","v","w","x","y","z"};

		for (int i = 0; i < alphabet.length; i++){
			if (s.equals(alphabet[i]) && s.length() <= 1){
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 */
	public void Play(){

		while (numLives > 0){
			
			boolean isValid = checkInput(userInput);
			//System.out.println(usedLetter.contains(usedLetter));
			
			if (isValid == true && theWord.contains(userInput) &&
					usedLetter.contains(usedLetter) == false){
				usedLetter.add(userInput);	// Add the letter to used list
				
				// add the guessed letter to the window
			}
			
			else if (userInput.length() > 0){
				numLives = numLives - 1;
			}
			
			System.out.println(numLives);
			
		}
		
		
		
//		int x = 1;
//		centerPanel.remove(line);
//		LinePanel line2 = new LinePanel(x,theWord);
//
//		//centerPanel.setSize(500,500);
//		centerPanel.setBackground(Color.GREEN);
//		centerPanel.add(line2);
//		add(centerPanel, BorderLayout.CENTER);
		
}

	public void actionPerformed(ActionEvent evt) {
		String temp = textField.getText();
		if (temp.length() > 0){
			userInput = temp;
		}
		textField.selectAll();
	}


}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JPanel;

public class LinePanel extends JPanel {

	int x;
	String temp = "";
	String theWord = "";
	
	public LinePanel(int x, String t) {
		super();
		setPreferredSize(new Dimension(300,350));
		setBackground(Color.BLUE);
		this.x = x;
		this.temp = t;
	}


	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		//Line2D line = new Line2D.Double(5, 5, 350, 5);
		if (x == 0 || x != 0){
			Line2D line = new Line2D.Double(0, 250, 80, 250); // Creates base
			Line2D line2 = new Line2D.Double(40, 50, 40, 250); // Creates vertical line
			Line2D line3 = new Line2D.Double(40, 50, 150, 50); // Creates horizontal line
			Line2D line4 = new Line2D.Double(150, 50, 150, 80); // Creates small line to hang the man
			g2.setStroke(new BasicStroke(5.0f));	// Line thickness
			g2.setColor(Color.BLACK);	// Line colour
			
			//draw shape of line
			g2.draw(line);
			g2.draw(line2);
			g2.draw(line3);
			g2.draw(line4);
			
			theWord = temp;
			
			int x1 = 0; int y = 320;
			
			for (int i = 0; i < temp.length();i++){
				g2.drawLine(x1, y, x1 + 15, y);
				x1 += 40;
			}
		}
			
		// head
		if (x == 6){
			g2.setStroke(new BasicStroke(5.0f));
			g2.drawOval(135, 85, 35, 35);
		}
		
		// body
		if (x == 5){
			g2.drawLine(150, 120, 150, 190);
		}
		
		// left arm
		if (x == 4){
			g2.drawLine(150, 140, 125, 155); 
		}
		
		// right arm
		if (x == 3){
			g2.drawLine(150, 140, 175, 155); 
		}
		
		// left leg and foot
		if (x == 2){
			g2.drawLine(150, 190, 125, 200); // leg
			g2.drawLine(125, 200, 120, 190); // foot
		}
		
		// right leg and foot
		if (x == 1){
			g2.drawLine(150, 190, 175, 200); // leg
			g2.drawLine(175, 200, 180, 190); // foot
		}
		
		if (x == 10){
			
			int x1 = 3; int y = 317;
			for (int i = 0; i < theWord.length() ;i++){
				g2.setColor(Color.ORANGE);
				g2.drawString("hi", x1, y);
				x1 += 40;
			}
			
			
		}
	}
}
public class PlayHangman {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		HangmanJFrame window = new HangmanJFrame();
		
		window.setVisible(true);
		
		window.Play();
		//ProcessHangman theMan = new ProcessHangman();
		
	}

}

Instead of using a while loop, change it to an if statement. Then call your Play() method in the actionPerformed method. Therefore, you take input, check it, enter the play method, then if nothing gets done, it returns to the actionPerformed and does it again.

What this is doing is, accepting input and keeps looping in the Play method, therefore, if you ever enter a wrong letter, it keeps looping and removing lives, then exits the loop when live = 0.

Edited 5 Years Ago by Akill10: n/a

Thanks I fixed that part as you mentioned. I just have one more question. What would be the best way to update the graphic each time a life is lost.

Currently I'm trying to remove the centre panel, and then create a new panel with the required lines. It works, but it's not showing up too nicely, I have too resize the window each time for it to show up.

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

/** class to display a JFrame object using border layout */

public class HangmanJFrame extends JFrame implements ActionListener {

	private JPanel centerPanel;
	private JPanel southPanel;
	private JTextField textField;
	private LinePanel line;
	private String [] wordList = {"computer","java"}; // word list
	/**
	 * 
	 */
	public ArrayList<String> usedLetter = new ArrayList(); // list of used letter by user
	


	private ArrayList<String> correctGuesses;  // list of users correct guesses
	private int numLives = 6; // number of lives
	public String theWord; // the wrong which is chosen
	private String userInput = "";
	
	// no-argument constructor
	public HangmanJFrame() { 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		theWord = pickWord();
		correctGuesses = new ArrayList(theWord.length());

		setSize(600,500);
		setLocation(100, 100);
		setTitle("Hangman Game");
		setLayout(new BorderLayout());

		centerPanel = new JPanel();
		
		line = new LinePanel(15,theWord);
		centerPanel.setSize(500,500);
		centerPanel.setBackground(Color.PINK);
		centerPanel.add(line);
		add(centerPanel, BorderLayout.CENTER);
		
		textField = new JTextField(20);
		textField.addActionListener(this);

		southPanel = new JPanel();
		southPanel.setBackground(Color.CYAN);
		southPanel.add(textField);
		add(southPanel, BorderLayout.SOUTH);

	}

	// Picks a word, latter it will be picked randomly.
	private String pickWord(){
		return wordList[0];
	}

	// This method check wither the input is valid
	// i.e. its in the alphabet.
	private boolean checkInput(String s){
		String [] alphabet = {"a","b","c","d","e","f",
				"g","h","i","j","k","l","m","n","o","p",
				"q","r","s","t","u","v","w","x","y","z"};

		for (int i = 0; i < alphabet.length; i++){
			if (s.equals(alphabet[i]) && s.length() <= 1){
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 */
	public void Play(){

		if (numLives > 0){
			boolean isValid = checkInput(userInput);
			
			if (isValid == true && theWord.contains(userInput) &&
					usedLetter.contains(userInput) == false){
				
				
				usedLetter.add(userInput);	// Add the letter to used list
				
				// add the guessed letter to the window
			}
			
			else if (userInput.length() == 1){
				numLives = numLives - 1;
				centerPanel.removeAll();
				LinePanel line2 = new LinePanel(numLives,theWord);
				centerPanel.setSize(500,500);
				centerPanel.setBackground(Color.GREEN);
				centerPanel.add(line2);
				add(centerPanel, BorderLayout.CENTER);
			}
			
			System.out.println(numLives);
			
			String t = "";
			for (String c : usedLetter){
				t += " " +c+" ";
			System.out.println(t);
			}
		}
		
		
		
}

	public void actionPerformed(ActionEvent evt) {
		String temp = textField.getText();
		if (temp.length() == 1){
			userInput = temp;
		}
		textField.selectAll();
		Play();
	}


}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;

import javax.swing.JPanel;

public class LinePanel extends JPanel {

	int x;
	String temp = "";
	String theWord = "";
	
	public LinePanel(int x, String t) {
		super();
		setPreferredSize(new Dimension(300,350));
		setBackground(Color.BLUE);
		this.x = x;
		this.temp = t;
	}


	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		//Line2D line = new Line2D.Double(5, 5, 350, 5);
		if (x == 15 || x != 15){
			Line2D line = new Line2D.Double(0, 250, 80, 250); // Creates base
			Line2D line2 = new Line2D.Double(40, 50, 40, 250); // Creates vertical line
			Line2D line3 = new Line2D.Double(40, 50, 150, 50); // Creates horizontal line
			Line2D line4 = new Line2D.Double(150, 50, 150, 80); // Creates small line to hang the man
			g2.setStroke(new BasicStroke(5.0f));	// Line thickness
			g2.setColor(Color.BLACK);	// Line colour
			
			//draw shape of line
			g2.draw(line);
			g2.draw(line2);
			g2.draw(line3);
			g2.draw(line4);
			
			theWord = temp;
			
			int x1 = 0; int y = 320;
			
			for (int i = 0; i < temp.length();i++){
				g2.drawLine(x1, y, x1 + 15, y);
				x1 += 40;
			}
		}
			
		// head
		if (x == 5 || x < 5){
			g2.setStroke(new BasicStroke(5.0f));
			g2.drawOval(135, 85, 35, 35);
		}
		
		// body
		if (x == 4 || x < 4){
			g2.drawLine(150, 120, 150, 190);
		}
		
		// left arm
		if (x == 3 || x < 3){
			g2.drawLine(150, 140, 125, 155); 
		}
		
		// right arm
		if (x == 2 || x < 2){
			g2.drawLine(150, 140, 175, 155); 
		}
		
		// left leg and foot
		if (x == 1 || x < 1){
			g2.drawLine(150, 190, 125, 200); // leg
			g2.drawLine(125, 200, 120, 190); // foot
		}
		
		// right leg and foot
		if (x == 0){
			g2.drawLine(150, 190, 175, 200); // leg
			g2.drawLine(175, 200, 180, 190); // foot
		}
		
		if (x == 5){
			
			int x1 = 3; int y = 317;
			for (int i = 0; i < theWord.length() ;i++){
				g2.setColor(Color.ORANGE);
				g2.drawString(theWord.substring(i, i+1), x1, y);
				x1 += 40;
			}
			
			
		}
	}
}

I'm sorry, but I had to ask... what was the point of this line?

if (x == 15 || x != 15) {

...that line will always execute, because no matter the number, it will always either equal 15 or not 15...

I'm sorry, but I had to ask... what was the point of this line?

if (x == 15 || x != 15) {

...that line will always execute, because no matter the number, it will always either equal 15 or not 15...

Hi, yes. I always want that line to execute as this if statement creates the base to hang the man.

I suppose I don't really need that line, but the way I have set up the constructor I had to use that if statement.

Edited 5 Years Ago by axa121: n/a

Could someone please have a look at my code?

I had everything working before, but now whenever a life is lost the panel shifts to the right for some reason, and then its not drawing the body parts.

If I can make the panel not shift then I will be finished.

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

/** class to display a JFrame object using border layout */

public class HangmanJFrame extends JFrame implements ActionListener {

	private JPanel centerPanel;
	private JPanel southPanel;
	private JTextField textField;
	private LinePanel line;
	private String [] wordList = {"computer","java","activity","alaska","appearance","article",
			"automobile","basket","birthday","canada","central","character","chicken","chosen",
			"cutting","daily","darkness","diagram","disappear","driving","effort","establish","exact",
			"establishment","fifteen","football","foreign","frequently","frighten","function","gradually",
			"hurried","identity","importance","impossible","invented","italian","journey","lincoln",
			"london","massage","minerals","outer","paint","particles","personal","physical","progress",
			"quarter","recognise","replace","rhythm","situation","slightly","steady","stepped",
			"strike","successful","sudden","terrible","traffic","unusual","volume","yesterday" };
	
	public ArrayList<String> usedLetters = new ArrayList(); // list of used letter by user
	public ArrayList<String> correctLetters = new ArrayList();
	public String userInput = "";

	private int numLives = 6; // number of lives
	public String theWord; // the wrong which is chosen
	JButton exitButton;
	JButton playAgain;
	
	// no-argument constructor
	public HangmanJFrame() { 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		theWord = pickWord();
		correctLetters = new ArrayList(theWord.length());

		setSize(600,500);
		setLocation(100, 100);
		setTitle("Hangman Game by Bilal Hamid");
		setLayout(new BorderLayout());

		centerPanel = new JPanel();
		line = new LinePanel(15,theWord,usedLetters);
		centerPanel.setSize(500,500);
		centerPanel.setBackground(Color.WHITE);
		centerPanel.add(line);
		add(centerPanel, BorderLayout.CENTER);
		
		textField = new JTextField(20);
		textField.addActionListener(this);

		playAgain = new JButton("Play Again");
		exitButton = new JButton("Exit");
		exitButton.addActionListener(this);
		playAgain.addActionListener(this);
				
		southPanel = new JPanel();
		southPanel.setBackground(Color.BLACK);
		southPanel.setLayout(new  GridLayout(0,3));
		southPanel.add(playAgain);
		southPanel.add(textField);
		southPanel.add(exitButton);
		add(southPanel, BorderLayout.SOUTH);

	}

	// Picks a word, latter it will be picked randomly.
	private String pickWord(){
		return wordList[0];
	}

	// This method check wither the input is valid
	// i.e. its in the alphabet.
	private boolean checkInput(String s){
		String [] alphabet = {"a","b","c","d","e","f",
				"g","h","i","j","k","l","m","n","o","p",
				"q","r","s","t","u","v","w","x","y","z"};

		for (int i = 0; i < alphabet.length; i++){
			if (s.equals(alphabet[i]) && s.length() <= 1){
				return true;
			}
		}
		return false;
	}

	/**
	 * 
	 */
	public void Play(){

		
		if (numLives > 0){
			
			if (userInput.length() == 1 && usedLetters.contains(userInput) == false &&
					checkInput(userInput) == true){
				usedLetters.add(userInput);
								
				if (theWord.contains(userInput) == true){
					correctLetters.add(userInput);
					centerPanel.removeAll();
					
					line = new LinePanel(20,theWord,correctLetters);
					centerPanel.add(line);
					centerPanel.revalidate();
				}
				
				else{
					numLives = numLives - 1;

					centerPanel.removeAll();
					line = new LinePanel(numLives,theWord,correctLetters);
					
					centerPanel.add(line);
					centerPanel.revalidate();
				}
			}
			
			else if (userInput.length() > 1)
				System.out.println("Enter a valid letter");
			
			else if (userInput.length() == 1 && checkInput(userInput) == true && theWord.contains(userInput)){
				correctLetters.add(userInput);
			}

			centerPanel.removeAll();
			
			line = new LinePanel(20,theWord,usedLetters);
			centerPanel.add(line);
			centerPanel.revalidate();
		}		
}
	// return true if the word and the correctly used letters list match
	public boolean checkWord(String s, ArrayList<String> t){
		String temp = "";
		
		for (int i = 0; i < s.length(); i++){
			if ( t.contains(s.substring(i, i+1)) == true){
				temp += s.substring(i, i+1);
			}
		}
		
		if (s.equals(temp)){
			return true;
		}
		
		return false;
	}

	public void actionPerformed(ActionEvent evt) {
		String temp = textField.getText();
		if (temp.length() == 1){
			userInput = temp;
		}
		textField.selectAll();
		if (checkWord(theWord, correctLetters) != true){
			Play();
		}
		
		if (evt.getSource() == exitButton){
			System.exit(0);
		}
		
//		if (evt.getSource() == playAgain){
//			removeAll();
//			repaint();
//			
//		}
		
	}


}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;

import javax.swing.JPanel;

public class LinePanel extends JPanel {

	int x = 5;
	String theWord = "";
	ArrayList<String> letterList;

	public LinePanel(int num, String t, ArrayList<String> s) {
		super(); // 300,350
		setPreferredSize(new Dimension(400,400));
		setBackground(Color.RED);
		this.x = num;
		this.theWord = t;
		letterList = cloneList(s);
	}

	private ArrayList<String> cloneList(ArrayList<String> aList) {
		ArrayList<String> clonedList = new ArrayList<String>(aList.size());
		for (String letter : aList) 
			clonedList.add(letter);
		return clonedList;
	}



	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		if (x == 15 || x != 15){
			Line2D line = new Line2D.Double(0, 250, 80, 250); // Creates base
			Line2D line2 = new Line2D.Double(40, 50, 40, 250); // Creates vertical line
			Line2D line3 = new Line2D.Double(40, 50, 150, 50); // Creates horizontal line
			Line2D line4 = new Line2D.Double(150, 50, 150, 80); // Creates small line to hang the man
			g2.setStroke(new BasicStroke(5.0f));	// Line thickness
			g2.setColor(Color.BLACK);	// Line colour

			//draw shape of line
			g2.draw(line);
			g2.draw(line2);
			g2.draw(line3);
			g2.draw(line4);

			int x1 = 0; int y = 320;
			for (int i = 0; i < theWord.length();i++){
				g2.drawLine(x1, y, x1 + 20, y);
				x1 += 50;
			}
		}

		// head
		if (x == 5 || x < 5){
			System.out.println("uheuheuha");
			g2.setStroke(new BasicStroke(5.0f));
			g2.drawOval(135, 85, 35, 35);
		}

		// body
		if (x == 4 || x < 4){
			g2.drawLine(150, 120, 150, 190);
		}

		// left arm
		if (x == 3 || x < 3){
			g2.drawLine(150, 140, 125, 155); 
		}

		// right arm
		if (x == 2 || x < 2){
			g2.drawLine(150, 140, 175, 155); 
		}

		// left leg and foot
		if (x == 1 || x < 1){
			g2.drawLine(150, 190, 125, 200); // leg
			g2.drawLine(125, 200, 120, 190); // foot
		}

		// right leg and foot
		if (x == 0){
			g2.drawLine(150, 190, 175, 200); // leg
			g2.drawLine(175, 200, 180, 190); // foot
		}

		// Show whole word on screen
		if (x == 20){

			int x1 = 3; int y = 317;

			String temp = "";

			for (int i = 0; i < theWord.length(); i++){
				if ( letterList.contains(theWord.substring(i, i+1)) == true){
					temp += theWord.substring(i, i+1);
				}
				else{
					temp += " ";
				}
			}

			for (int i = 0; i < temp.length() ;i++){
				g2.setColor(Color.BLACK);
				Font font = new Font("Arial", Font.PLAIN, 25);
				g2.setFont(font);
				g2.drawString(temp.substring(i, i+1), x1, y);
				x1 += 50;
			}


			x1 = 3;  y = 370;
			for (int i = 0; i < letterList.size() ;i++){

				if (theWord.contains(letterList.get(i)) == false){
					g2.drawString(letterList.get(i), x1, y );
					x1 += 50;
				}

			}


		}
	}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;


public class PlayHangman {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		HangmanJFrame window = new HangmanJFrame();
		window.setVisible(true);

	}
}
This article has been dead for over six months. Start a new discussion instead.