Java BlueJ hangman game

a little help please!

Page 1 of 1

1 Replies - 22742 Views - Last Post: 25 April 2008 - 12:53 PM Rate Topic: -----

#1 Boffy_Dude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-April 08

Java BlueJ hangman game

Post icon  Posted 25 April 2008 - 08:29 AM

I must make a blueJ hangman game, this is as far as i have got:


/**
* Write a description of class HangmanG here.
* 
* @author () 
* @version (v 1.0)
*/


import javax.swing.*;
import java.awt.*;
import java.lang.*;
import BreezySwing.*;

public class HangmanG extends GBFrame {
	

	// 'spacing' labels - just help in the layout of the interface
	JLabel spacingLabel		 = addLabel("  ", 1, 1, 1, 1);
	// ---------------------------------------------------------

	// The word label, field and button
	JLabel wordLabel			= addLabel("Word to Guess", 2, 1, 1, 1);
	JTextField wordField		= addTextField("",		  3, 1, 1, 1);
	JButton wordButton		  = addButton("Enter Word",   4, 1, 1, 1);	
	// ---------------------------------------------------------
	
	// 'spacing' labels - just help in the layout of the interface
	JLabel spacingLabe2		 = addLabel("  ", 5, 1, 1, 1);
	// ---------------------------------------------------------
	
	// wordGuesedField will display the word guessed so far
	JLabel guessedWordLabel		 = addLabel("Word Guessed so far:",  6, 1, 1, 1);
	JTextField guessedWordField	 = addTextField("",				 7, 1, 1, 1);
	// ---------------------------------------------------------

	// 'spacing' labels - just help in the layout of the interface	
		JLabel spacingLabel3		= addLabel("  ", 8, 1, 1, 1);
	// ---------------------------------------------------------

	// The letter label, field and button
	JLabel letterLabel	  = addLabel("Guess Letter",  9, 1, 1, 1);
	JTextField letterField  = addTextField("",		  10, 1, 1, 1);
	JButton letterButton	= addButton("Enter Guess",  11, 1, 1, 1);
	// ---------------------------------------------------------

	// the drawingPanel for drawing the stick figure
	GBPanel drawingPanel = addPanel(new GBPanel(), 1, 2, 1, 10);
	// ---------------------------------------------------------


	char[] wordToGuess = null;		  // the word the user needs to guess

	char[] wordGuessedSoFar = null;		  // the word the user has guessed so far

	char guessedLetter;				 // the letter the user types in 
	int wrong;
	int dash;
	int yes;
	public HangmanG() {

		drawingPanel.setBackground(Color.gray);


	} // end constructor


	public void buttonclicked(JButton buttonObj){

		if (buttonObj == wordButton) {
			processWordButton();
		}
		else if (buttonObj == letterButton) {
			processLetterButton();
			updateGuessedWordField();
		}

	} // end buttonclicked()


	/**
	*   Assigns the text entered in wordField to the wordToGuess char array,
	*   with all characters being converted to lowercase
	*/
	public void processWordButton() {

		String wordIn;					  // local variable to hold the text in the wordField for conversion

		wordIn = wordField.getText();	   // get the String from the wordField
		
		if (wordIn == null || wordIn.length() <= 0 ) {   // check for null or  zero length word
			
			messageBox(" You forgot to enter in a word!" );
			return;
			
		}

		wordIn = wordIn.toLowerCase();	  // convert all in the String to lowercase

		wordToGuess = wordIn.toCharArray(); // now change the String to an array of char
		int wordLength = wordIn.length();
		wordGuessedSoFar = new char[wordLength];
		int i;
		for (i = 0; i < wordLength; i++) {
			wordGuessedSoFar[i] = '*';
			
		}
		updateGuessedWordField();
		wordField.setText("");			  // blank the wordField

		wordButton.setEnabled(false);	   // now disable the wordButton so the user cannot enter another word
	   

		
	} // end processWordButton()


	/**
	*   Takes the first character entered in letterField,
	*   and assigns it to guessedLetter.
	*   Finally, guessedLetter is converted to lowercase.
	*/
	public void processLetterButton() {

		String letterInString;					  // local variable to hold the text in the letterField for conversion

		char[] letterIn; 

		letterInString = letterField.getText();	 // get the String from the letterField
		
		 if (letterInString == null || letterInString.length() <= 0 ) {
			
			messageBox(" You forgot to enter in a letter!" );
			return;
			
		}

		letterIn = letterInString.toCharArray();	// now change the String to an array of char
		
		guessedLetter = letterIn[0];				// use only the first element of the array
		guessedLetter = Character.toLowerCase(guessedLetter);   // convert the guessedLetter to lowercase
		int wordLength = wordToGuess.length;
		boolean correctGuess = false;
		int i;
		for (i = 0; i < wordLength; i++) {
			if (wordToGuess[i] == guessedLetter) {
				wordGuessedSoFar[i] = guessedLetter;
				correctGuess = true;
				yes = yes + 1;
			} 
			
		}
			if (correctGuess == false) {
				wrong = wrong + 1;
				super.repaint();
			}
			if (yes == wordToGuess.length) {
			messageBox("**PLAYER WINS!**");

			return;
		}
			if (wrong == (wordToGuess.length + 5)) {
				messageBox("**YOU LOOSE, COMPUTER WINS!**");
				return;

		}
		updateGuessedWordField();

		
		letterField.setText("");					// blank the letterField

	} // end processLetterButton


	/**
	*   If the guessedWord char array is not null,
	*   then display the guessedWord in the guesedWordField
	*/
	public void updateGuessedWordField() {

		String guessedWordString;

		if (wordGuessedSoFar  != null) {
		  
			guessedWordField.setText(new String(wordGuessedSoFar));
			
		}

	} // end updateGuessedWordField()


	public void paint(Graphics g) {

		super.paint(g); 
		Graphics panelG = drawingPanel.getGraphics();
		if (wrong > 0) {
		panelG.drawLine(50,250,100,250);
		panelG.drawLine(75,250,75,50);
		panelG.drawLine(75,50,200,50);
		panelG.drawLine(75,70,100,50);
		panelG.drawLine(200,50,200,80);
		panelG.drawOval(180,80,40,40);
		panelG.drawLine(200,120,200,180);
		panelG.drawLine(200,180,175,210);
		panelG.drawLine(200,180,225,210);
		panelG.drawLine(200,140,175,160);
		panelG.drawLine(200,140,225,160);
	   
	}


}public static void main(String[] args)  {

		HangmanG tpo = new HangmanG();
		
		tpo.setSize(520, 350);  // set the size of frame: width = 500, height = 400
		tpo.setVisible(true);   // now make the frame visible


	} // end main()


} // end class





I am unsure as how to lnk each wrong guess with a new line aded on the picture of the hangman.

Any help would be much appretiated

Cheers

Is This A Good Question/Topic? 0
  • +

Replies To: Java BlueJ hangman game

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java BlueJ hangman game

Posted 25 April 2008 - 12:53 PM

BTW BlueJ is your IDE it has nothing to do with the kind of Java code you are writing or even it's color.
If your code works with BlueJ it will work at the console with Notepad or on Eclipse or on JBuilder.
Java is Java.

A lot of errors.... lets correct the most obvious one

// sorry we don't have access to this package so cannot help you with that
import BreezySwing.*;

// don't know what is a GBFrame probably coming from BreezySwing
public class HangmanG extends GBFrame {
	
// multiple errors here
// the syntax to create a JLabel is:  JLabel label = new JLabel("")
// addLabel("", 1,1,1,1) is not a constructor for JLabel

	// 'spacing' labels - just help in the layout of the interface
	JLabel spacingLabel		 = addLabel("  ", 1, 1, 1, 1);
	JLabel wordLabel			= addLabel("Word to Guess", 2, 1, 1, 1);
// same thing for JTextFiels adn JButton
	JTextField wordField		= addTextField("",		  3, 1, 1, 1);
	JButton wordButton		  = addButton("Enter Word",   4, 1, 1, 1);	
// unless GBFrame has the methods addLbel, addTextField and addButton
// that create that JComponent, position them and return them
// if it is the case, we cannot help

// GBPanel must also be part of BreezySwing 
	// the drawingPanel for drawing the stick figure
	GBPanel drawingPanel = addPanel(new GBPanel(), 1, 2, 1, 10);
	public void processWordButton() {

// messageBox is not declares and/or defined			
	messageBox(" You forgot to enter in a word!" );
// messageBox is also used a few times below




That should keep you busy for a while
But without BreezySwing there is not much more we can do

This post has been edited by pbl: 25 April 2008 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1