Question

Using GUI to ask user for an alphabet input and to graphically display the hangman. During...

Using GUI to ask user for an alphabet input and to graphically display the hangman.

During each round, the player will enter an input, and the program will display the hangman accordingly to the input.
Main method with a loop that will provide a hint and prompt the user for an input after each round.
String getWord() method returns a random word from our list to use for the game.

Void hangman(String guess) method that will receive the user input as its parameter, see if the input matches one of the alphabets in the word and then print out feedbacks.
Void hangmanImage() that will use GUI elements to display the state of the hangman.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Component;

import java.awt.Container;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.awt.RenderingHints;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.BorderFactory;

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.Timer;

public class Hangman extends JComponent

{

Draw draw = null;

JButton skipButton = null;

JButton newGameButton = null;

JLabel wordArea = null;

JLabel messageArea = null;

java.util.List alphaButtonList = new ArrayList();

Iterator alphaIterator = null;

boolean reset = true;

boolean disable = false;

boolean dontWrap = false;

boolean wrap = true;

boolean headDrawn = false;

boolean bodyDrawn = false;

boolean leftArmDrawn = false;

boolean rightArmDrawn= false;

boolean leftLegDrawn = false;

boolean rightLegDrawn= false;

// Target words

String[] targetWords = {"abstract", "cemetery", "nurse", "pharmacy", "climbing"};

String losingPrefix = "Game over! Total score is ";

String win = "Total score is ";

String currentGuess;

String targetWord;

JLabel space ;

  

int numberWrong = 0;

int numberOfBodyParts = 6;

static int score = 0;

int next = 0;

  

  

// method: setUpNewGame()

// purpose: Sets up a new game

public void setUpNewGame() {

numberWrong = 0;

score =100;

messageArea.setText("Hangman");

//Enable alphabet buttons

Iterator alphaIterator = alphaButtonList.iterator();

while( alphaIterator.hasNext() ) {

( (JButton)alphaIterator.next() ).setEnabled( reset );

}

//Disable new game button

newGameButton.setEnabled( disable );

//Color the word area

wordArea.setBackground(Color.black);

//Present the new word

double numb = Math.random();

next = (int)( numb * targetWords.length );

targetWord = targetWords[next];

//Fill the word-to-guess with ???

currentGuess = "?";

for( int i=0; i<targetWord.length()-1; i++) {

currentGuess = currentGuess.concat("?");

}

wordArea.setText( currentGuess );

//Nothing is drawn yet

headDrawn = false;

bodyDrawn = false;

leftArmDrawn = false;

rightArmDrawn= false;

leftLegDrawn = false;

rightLegDrawn= false;

draw.repaint();

}//setUpNewGame

  

// method: processAnswer()

// pupose: it determines if the input is correct or not

public void processAnswer(String answer) throws IOException {   

char newCharacter = answer.charAt(0);

// Look thru the target word.

// If the character matches the target, concat the new character.

// If the character doesn't match the target, concat the character

// from the current guess.

String nextGuess = "";

boolean foundAMatch = false;

for( int i=0; i<targetWord.length(); i++ ) {

char characterToMatch = targetWord.charAt(i);

if( characterToMatch == newCharacter ) {

nextGuess = nextGuess.concat( String.valueOf(newCharacter) );

foundAMatch = true;

}

else {

nextGuess = nextGuess.concat(String.valueOf( currentGuess.charAt(i) ));

}

}//for each character

currentGuess = nextGuess;

wordArea.setText( currentGuess );

if( !foundAMatch ) {

numberWrong++;

score = score-10;

switch (numberWrong){

case 1: { headDrawn = true; break; }

case 2: { bodyDrawn = true; break; }

case 3: { leftArmDrawn = true; break; }

case 4: { rightArmDrawn = true; break; }

case 5: { leftLegDrawn = true; break; }

case 6: { rightLegDrawn = true; break; }

default: System.out.println("You should be dead!");

}

// Repaint the gallows area JPanel

draw.repaint();

}

// We have a winner

if( currentGuess.equals( targetWord ) ) {

//Disable the buttons

Iterator alphaIterator = alphaButtonList.iterator();

while( alphaIterator.hasNext() ) {

( (JButton)alphaIterator.next() ).setEnabled( disable );

}

messageArea.setText( losingPrefix + score);

skipButton.setEnabled( reset );

// newGameButton.setEnabled( reset );

Colors c = new Colors();

c.run();

Main.dispose();

  

}

// Wrong Answer

// Set out a new body part to be drawn by repaint()

// else {

  

// Is the game over?

if( numberWrong >= numberOfBodyParts )

{

//Disable the buttons

Iterator alphaIterator = alphaButtonList.iterator();

while( alphaIterator.hasNext() ) {

( (JButton)alphaIterator.next() ).setEnabled( disable );

}

messageArea.setText( win+ score);

// newGameButton.setEnabled( reset );

Colors c = new Colors();

c.run();

Main.dispose();

skipButton.setEnabled( reset );

}

// }//if else

}//processAnswer

//method: createNorthPane()

//Purpose: Create the North pane of the the game

//where the word prompts will be displayed.

public Component createNorthPane() {

JPanel pane = new JPanel();

pane.setLayout( new BoxLayout( pane, BoxLayout.X_AXIS ) );

pane.setBorder( BorderFactory.createEmptyBorder(0, 10, 10, 10) );

pane.add(Box.createHorizontalGlue() );

wordArea = new JLabel("Press New Game");

wordArea.setFont( new Font("Helvetica", Font.PLAIN, 24) );

wordArea.setBackground(Color.lightGray);

wordArea.setForeground(Color.black);

pane.add(wordArea);

pane.add(Box.createHorizontalGlue() );

return pane;

}

//method: createWestPane()

//Purpose: Create the West pane of the the game

//where the keyboard will be displayed.

public Component createWestPane() {

ActionListener alphabetButtonAction = new ActionListener() {

public void actionPerformed( ActionEvent e ) {

JButton buttonPushed = (JButton)e.getSource();

buttonPushed.setEnabled( disable );

try {

processAnswer( buttonPushed.getText() );

} catch (IOException ex) {

Logger.getLogger(Hangman.class.getName()).log(Level.SEVERE, null, ex);

}

}

};

JPanel pane = new JPanel();

pane.setBorder(BorderFactory.createLoweredBevelBorder());

GridBagLayout gridbag = new GridBagLayout();

GridBagConstraints c = new GridBagConstraints();

pane.setLayout( gridbag );

c.fill = GridBagConstraints.BOTH;

JButton button = new JButton( "a" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 0;

c.gridy = 0;

c.gridheight = 1;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "b" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 1;

c.gridy = 0;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "c" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 2;

c.gridy = 0;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "d" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 0;

c.gridy = 1;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "e" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 1;

c.gridy = 1;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "f" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 2;

c.gridy = 1;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "g" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 0;

c.gridy = 2;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "h" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 1;

c.gridy = 2;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "i" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 2;

c.gridy = 2;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "j" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 0;

c.gridy = 3;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "k" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 1;

c.gridy = 3;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "l" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 2;

c.gridy = 3;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "m" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 0;

c.gridy = 4;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "n" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 1;

c.gridy = 4;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "o" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 2;

c.gridy = 4;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "p" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 0;

c.gridy = 5;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "q" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 1;

c.gridy = 5;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "r" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 2;

c.gridy = 5;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "s" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 0;

c.gridy = 6;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "t" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 1;

c.gridy = 6;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "u" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 2;

c.gridy = 6;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "v" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 0;

c.gridy = 7;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "w" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 1;

c.gridy = 7;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "x" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 2;

c.gridy = 7;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "y" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 0;

c.gridy = 8;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

button = new JButton( "z" );

c.weightx = 0.5;

c.weighty = 0.5;

c.gridx = 1;

c.gridy = 8;

gridbag.setConstraints( button, c );

button.addActionListener( alphabetButtonAction );

pane.add( button );

alphaButtonList.add( button );

alphaIterator = alphaButtonList.iterator();

while( alphaIterator.hasNext() ) {

( (JButton)alphaIterator.next() ).setEnabled( disable );

}

return pane;

}

//method: createSouthPane()

//Purpose: Create the South pane of the the game

//where the status prompts will be displayed.

public Component createSouthPane() {

JPanel pane = new JPanel();

pane.setLayout( new BoxLayout( pane, BoxLayout.X_AXIS ) );

pane.setBorder( BorderFactory.createEmptyBorder(10, 10, 10, 10) );

pane.add(Box.createHorizontalGlue() );

messageArea = new JLabel("Hangman");

messageArea.setFont( new Font("Helvetica", Font.PLAIN, 28) );

messageArea.setBackground( Color.lightGray );

messageArea.setForeground( Color.red );

pane.add(messageArea);

pane.add(Box.createHorizontalGlue() );

return pane;

}

//method: createCenterPane()

//Purpose: Create the center pane of the the game

//where the picture prompts will be displayed.

public Component createCenterPane() {

// Pass the reference to this instance of the game so that

// the repaint() method can find out what to draw

draw = new Draw(this);

return draw;

}

//method: createEastPane()

//Purpose: Create the East pane of the the game

//where the buttons prompts will be displayed.

public Component createEastPane()

{

ActionListener controlButtonListener = new ActionListener() {

public void actionPerformed( ActionEvent e )

{

JButton buttonPushed = (JButton)e.getSource();

if( buttonPushed.getText().equals("New Game") ) {

setUpNewGame();

}

else

{

Colors c = new Colors();

c.run();

Main.dispose();

}

}//actionPerformed

};//controlButtonListener

JPanel pane = new JPanel();

pane.setBorder(BorderFactory.createLoweredBevelBorder());

pane.setLayout( new BoxLayout( pane, BoxLayout.Y_AXIS ) );

Clock clock = new Clock();

pane.add( clock.time );

pane.setVisible( true );

clock.start();

newGameButton = new JButton( "New Game" );

newGameButton.setFont( new Font("Helvetica", Font.PLAIN, 18) );

newGameButton.setBounds(75,100,300,200);

newGameButton.setToolTipText("Press to Start Game");

newGameButton.addActionListener( controlButtonListener );

pane.add( newGameButton );

skipButton = new JButton( " skip " );

skipButton.setFont( new Font("Helvetica", Font.PLAIN, 18) );

skipButton.setBounds(75,160,300,200);

skipButton.setToolTipText("Skip and go to next Game");

skipButton.addActionListener( controlButtonListener );

pane.add( skipButton);

return pane;

}

//method: createComponenets()

//Purpose: Create the GUI for the game.

public Component createComponents() {

JPanel pane = new JPanel();

pane.setBorder(BorderFactory.createLoweredBevelBorder());

pane.setLayout(new BorderLayout() );

pane.add( createNorthPane(), BorderLayout.NORTH );

pane.add( createWestPane(), BorderLayout.WEST );

pane.add( createSouthPane(), BorderLayout.SOUTH );

pane.add( createCenterPane(), BorderLayout.CENTER );

pane.add( createEastPane(), BorderLayout.EAST );

return pane;

}

}

Add a comment
Know the answer?
Add Answer to:
Using GUI to ask user for an alphabet input and to graphically display the hangman. During...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • Create the game hangman using c code: Program description In the game of hangman, one player...

    Create the game hangman using c code: Program description In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the...

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

  • Hangman using while and fo loops, if's, and functions.

    I would like to preface this by saying this is NOT a current homework assignment; but it is an assignment from 3 years ago before I dropped out of school. I am self teaching and am revisiting an old assignment. I am NOT asking for the entire program, I'm simply looking for help building the skeleton for the initial start of the game.MORE INFO: Player 1 will enter word(of any length / i have been using "Testing") for Player 2...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • c++. I'm working on a hangman game. It has to utilize a class. It should show...

    c++. I'm working on a hangman game. It has to utilize a class. It should show the number of attempts that have been made, the length of the word represented by dots, and then the alphabet. There also needs to be input validation. If the word was horse, the very first attempt at the user guessing should display " 1 ..... choose: abcdefghijklmnopqrstuvwxyz ". As the user guesses letters, the number of attempts should go up, the letter should be...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • Purpose of it is to coordinate the interaction between the user and the lower-level functionality of...

    Purpose of it is to coordinate the interaction between the user and the lower-level functionality of the game. There are two overloaded constructors, one for constructing a brand new game and one for constructing a game from a file. w - Move Up    s - Move Down    a - Move Left    d - Move Right    q - Quit and Save Board If the user inputs any one of these characters, execute the corresponding move. This should result in a refreshed...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT