Written for ECE:3330; Introductory Software Design course taught at the University of Iowa
Create a GUI hangman game using Java. Use Java Graphics or Graphics 2D class for implementing the GUI elements. This Hangman game must have a place to enter the word to be guessed (hidden from the actual player), the number of guesses remaining, the incorrect letters guessed, a place to enter letters the user would like to guess, some sort of readout for word guess progress, a progressively drawn stick figure, and indication of when the game has ended in either a losing or winning state.
The Hangman
class contains the main method which creates an a HangmanGame
object that requires a word parameter which sets the hangman game's word that the user must guess. However, creating a HangmanGame object does not actually start the hangman game until a call to its start
method is made, in which case a HangmanFrame
window is opened by calling its setVisible
method. HangmanFrame
is a JFrame which contains the entire hangman game graphics and interface, this hangman game window is divided into a two column grid, in which the left side contains StickfigureComponent
, a JComponent which handles drawing the hangman picture. The right side contains GameFieldsPanel
which is a JPanel containing several JLabel's, and a JTextField for inputting words, this action event is handled by GuessButtonListener
, which is stored in the HangmanGame
object and requires an instance of this HangmanGame
object to be passed in its constructor since it makes method calls to that object. This means both HangmanFrame
and GameFieldsPanel
require an instance of GuessButtonListener
to be passed in their constructor so the guess button pressed event can be listened to. When this action occurs, makeGuess
is called by GuessButtonListener
which is a method inside HangmanGame
that handles checking if a letter is valid/not valid and based on this result, whether the game has been won/lost or an incorrect guess needs to be updated. In the case of an incorrect guess, several fields are updated, one of which is the StickfigureComponent
which draws the next stick figures body part through the drawNextPart
method which is called by HangmanFrame's drawNextPart
method called by makeGuess
. Remaining guesses are computed from StickfigureComponent's getRemainingGuesses
method.
The main Hangman class handles starting a HangmanGame object which allows you to start playing. Run the Hangman class to start and a GUI should pop up prompting you to input a word the guesser will guess (DO NOT SHOW TO THE PERSON PLAYING!!!). Once the word to guess has been inputted, the game of Hangman will start. Input letters in the field on the right side of screen and press enter or the guess button to submit the letter. Incorrect guesses will result in an additional part of the stick man being drawn, the remaining guesses available to you are displayed at the top of the screen, alongside your previously incorrectly guessed letters located at the bottom. The top right most text displays the word guess progress, as correctly guessed letters are submitted the word begins to fill in with letters allowing the user to make better informed guesses on what missing letters could be. A user has won when they successfully guess the word/all letters in the word before running out of guesses (aka the hangman stick figure is fully drawn). A game win/lose screen will appear to indicate the end of the game, afterwards the application will close.