The Hanged Man game Java Applet - source code
![]()
![]()
![]()
![]()
![]()
By Sergey Skudaev
Here you will find "The Hanged Man" game java applet and source code. You can play and improve your spelling or you can learn Java computer language. The Java source code for this tutorial can be downloaded at the bottom of this page. You have to be familiar with Java applets to understand the source code.
First player of the game enters a word in the text field. The text field displays asterisk characters for each letter, so that the second player of the game cannot read the word...
For computer game applet I used different layouts to place panels with title, text field array, input text and buttons. My Applet has BorderLayout layout. The BorderLayout has the North area, the Center area, the South area, the West and the East areas.
The title panel is located in the North area. The text fields array panel for letters and the text field panel for the whole word are located on the main panel. The main panel has grid layout. It has two rows and one column. Text fields array for letters of the word is located in the first row. The input text field is located in the second row. The main panel is located in the Center area.
The button panel is located in the South area and holds two button.
Here you will learn how to use BorderLayout, FlowLayout and GridLayout.
Java Source Code for Computer Game applet
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Guessword extends applet implements ActionListener
{
char charArr[];
String input;
int len;
boolean equalFlag = false;
private TextField tinput;
private TextField[] txtCh = new TextField[15];
Button btnInput, btnCheck;
Panel pbtn;
Panel ptxt;
Panel pinput;
Panel pmain;
Panel ptitle;
Label lbltitle;
int count;
public void init()
{
//set layout for applet
setLayout(new BorderLayout());
//create character array to hold single letters of the input word
charArr = new char[15];
for(int i=0;i<15;i++)
charArr[i] = ' '; //initialize array with blank
//create label for title with central alignment and font
lbltitle = new Label("Guess word game", Label.CENTER);
lbltitle.setFont(new Font("TimesRoman", Font.BOLD, 30));
//create title panel and add title label to panel
ptitle = new Panel();
ptitle.setLayout(new FlowLayout(FlowLayout.CENTER));
ptitle.add(lbltitle);
//create panel for array of text field and set layout
ptxt = new Panel();
ptxt.setLayout(new FlowLayout());
//Create array of text fields
for(int i = 0; i <15; i++)
{
txtCh[i] = new TextField();
}
//add text array of fields to panel
ptxt.add(txtCh[0]);
ptxt.add(txtCh[1]);
ptxt.add(txtCh[2]);
ptxt.add(txtCh[3]);
ptxt.add(txtCh[4]);
ptxt.add(txtCh[5]);
ptxt.add(txtCh[6]);
ptxt.add(txtCh[7]);
ptxt.add(txtCh[8]);
ptxt.add(txtCh[9]);
ptxt.add(txtCh[10]);
ptxt.add(txtCh[11]);
ptxt.add(txtCh[12]);
ptxt.add(txtCh[13]);
ptxt.add(txtCh[14]);
//create panel for input text and set layout
pinput = new Panel();
pinput.setLayout(new FlowLayout());
//create input text field and set echo character
tinput = new TextField(15);
tinput.setEchoChar('*');
//add input text field to panel
pinput.add(tinput);
//create main panel with grid layout
//with two rows and one column
pmain=new Panel();
pmain.setLayout(new GridLayout(2,1));
//add text array panel and input text panel to central panel
pmain.add(ptxt);
pmain.add(pinput);
//create button panel and button
pbtn = new Panel();
pbtn.setLayout(new FlowLayout());
btnInput = new Button("Input");
// add action listener to input button
//to get button click event
btnInput.addActionListener(this);
//create check button and add both button to panel
btnCheck = new Button("Check");
btnCheck.addActionListener(this);
pbtn.add(btnInput);
pbtn.add(btnCheck);
//add all panels to applet
add(ptitle, BorderLayout.NORTH);
add(pmain, BorderLayout.CENTER);
add(pbtn, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
//if user clicks input button
//fill array with blank char to reset it
if(e.getSource() == btnInput)
{
for(int i=0;i<15;i++)
{
charArr[i] = ' ';
txtCh[i].setText("");
}
count = 0;
input = tinput.getText();
//get length of the word
len = input.length();
//fill array with a word
for(int i = 1; i< len-1; i++)
charArr[i] = input.charAt(i);
//show first letter
txtCh[0].setText(String.valueOf(input.charAt(0)));
//show last letter
txtCh[len-1].setText(String.valueOf(input.charAt(len-1)));
//clear the input text
tinput.setText("");
} //if user clicks Check button
else if (e.getSource() == btnCheck)
{
count += 1;
//look through the text field array
//if it contains a letter that exist in char array
//display the letter in the right text field
for(int i = 1; i<len-1; i++) //first for
if(!((txtCh[i].getText().equals(String.valueOf(charArr[i])))))
{
for(int n = 1; n<len-1; n++)
{
for(int j = 1; j < len-1; j++)
{
if( (txtCh[n].getText().equals(String.valueOf(charArr[j]))))
{
txtCh[j].setText(String.valueOf(charArr[j]));
}
}
}
}
//if guessed letter is not in char array remove it
if (!((txtCh[i].getText().equals(String.valueOf(charArr[i])))))
txtCh[i].setText("");
}//end first for
showStatus(String.valueOf(count));
} //else if source=btnCheck
} //end function
} //end class
Download source file:
Guessword.java
Please rate the tutorial
Learn SQL Programming By Examples [Kindle Edition]2.99
Learn PHP Programming by Examples [Kindle Edition] $2.99
Learn Visual Basic 6.0 [Kindle Edition] $1.99
How to Build Your Own Web Site from Scratch [Kindle Edition] $1.49
New-trip.com website source code
| Comments | |
|---|---|
- Home
- computer_game
- Batch Files
- Java Properties
- Form Validation
- Display Image PHP
- Upload File PHP
- phpMyAdmin
- Environment Variables
- Delete Trojan horse
- Java Code Examples
- Learn Java
- Java, MS Access, MySQL
- Java Random Generator
- Thread Applet
- Sort Algorithms
- Computer Game
- Poet Applet
- More Code Examples
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn SQL
- Learn PHP
- Learn C++
- Learn Visual Basic
- Web Site Tips
- User_Auth. Demo
- Using Twitter
- Advertise here
- Site map
- Registration
Web programming Tips