Gaming Need help with Java coursework II

R2DJ

Well-Known Member
OP
Member
Joined
Jan 30, 2008
Messages
1,898
Trophies
0
Location
London
XP
815
Country
So I'm trying to make a Hangman game with GUIForm. This is what I've come up so far:

Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* HangForm.java
*
* Created on 14-Feb-2011, 13:50:59
*/

package hangman;

/**
*
* @author w1223861
*/
public class HangForm extends javax.swing.JFrame {

ÂÂÂÂString FindWord = "hello";
ÂÂÂÂint wrong;

ÂÂÂÂ/** Creates new form HangForm */
ÂÂÂÂpublic HangForm() {
ÂÂÂÂÂÂÂÂinitComponents();
ÂÂÂÂ}

ÂÂÂÂ/** This method is called from within the constructor to
ÂÂÂÂ * initialize the form.
ÂÂÂÂ * WARNING: Do NOT modify this code. The content of this method is
ÂÂÂÂ * always regenerated by the Form Editor.
ÂÂÂÂ */
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 

ÂÂÂÂprivate void tryLetterActionPerformed(java.awt.event.ActionEvent evt) {ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ
ÂÂÂÂint charPos = 2;
ÂÂÂÂString letter = aLetter.getText();
ÂÂÂÂcharPos = FindWord.indexOf(letter);
ÂÂÂÂif (charPos == 0) char0.setText(letter);
ÂÂÂÂif (charPos == 1) char1.setText(letter);
ÂÂÂÂif (charPos == 2) char2.setText(letter);
ÂÂÂÂif (charPos == 3) char3.setText(letter);
ÂÂÂÂif (charPos == 4) char4.setText(letter);
ÂÂÂÂif (charPos == 5) char5.setText(letter);
ÂÂÂÂMyMessage.setText("position is " + charPos);
ÂÂÂÂcharPos = FindWord.indexOf(letter, charPos + 1);
ÂÂÂÂif (charPos == 0) char0.setText(letter);
ÂÂÂÂif (charPos == 1) char1.setText(letter);
ÂÂÂÂif (charPos == 2) char2.setText(letter);
ÂÂÂÂif (charPos == 3) char3.setText(letter);
ÂÂÂÂif (charPos == 4) char4.setText(letter);
ÂÂÂÂif (charPos == 5) char5.setText(letter);

ÂÂÂÂif (charPos==0)
ÂÂÂÂ{

ÂÂÂÂ}
ÂÂÂÂif (charPos==-1)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂwrong++;
ÂÂÂÂÂÂÂÂinCorrect.setText(Integer.toString(wrong));
ÂÂÂÂ}
ÂÂÂÂif (wrong==1)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂjButton1.setIcon(new javax.swing.ImageIcon("HANG9.jpg"));
ÂÂÂÂ}
ÂÂÂÂif (wrong==2)
ÂÂÂÂ{
ÂÂÂÂÂÂÂÂjButton1.setIcon(new javax.swing.ImageIcon("HANG8.JPG"));
ÂÂÂÂ}
if (wrong==3){
ÂÂÂÂjButton1.setIcon(new javax.swing.ImageIcon("HANG7.JPG"));
}
if (wrong==4){
ÂÂÂÂjButton1.setIcon(new javax.swing.ImageIcon("HANG6.JPG"));
}
if (wrong==5){
ÂÂÂÂjButton1.setIcon(new javax.swing.ImageIcon("HANG5.JPG"));
}
if (wrong==6){
ÂÂÂÂjButton1.setIcon(new javax.swing.ImageIcon("HANG4.JPG"));
}
if (wrong == 7) {
ÂÂÂÂjButton1.setIcon(new javax.swing.ImageIcon("HANG3.JPG"));
ÂÂÂÂÂÂÂÂ// TODO add your handling code here:
ÂÂÂÂ}ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 
}

ÂÂÂÂ/**
ÂÂÂÂ* @param args the command line arguments
ÂÂÂÂ*/
ÂÂÂÂpublic static void main(String args[]) {
ÂÂÂÂÂÂÂÂjava.awt.EventQueue.invokeLater(new Runnable() {
ÂÂÂÂÂÂÂÂÂÂÂÂpublic void run() {
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂnew HangForm().setVisible(true);
ÂÂÂÂÂÂÂÂÂÂÂÂ}
ÂÂÂÂÂÂÂÂ});
ÂÂÂÂ}

ÂÂÂÂ// Variables declaration - do not modifyÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 
ÂÂÂÂprivate javax.swing.JLabel MyMessage;
ÂÂÂÂprivate javax.swing.JTextField aLetter;
ÂÂÂÂprivate javax.swing.JLabel char0;
ÂÂÂÂprivate javax.swing.JLabel char1;
ÂÂÂÂprivate javax.swing.JLabel char2;
ÂÂÂÂprivate javax.swing.JLabel char3;
ÂÂÂÂprivate javax.swing.JLabel char4;
ÂÂÂÂprivate javax.swing.JLabel char5;
ÂÂÂÂprivate javax.swing.JLabel char6;
ÂÂÂÂprivate javax.swing.JLabel inCorrect;
ÂÂÂÂprivate javax.swing.JButton jButton1;
ÂÂÂÂprivate javax.swing.JButton tryLetter;
ÂÂÂÂ// End of variables declarationÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ 

}

Right now, I have messed up with the code, as it doesn't work properly. When the Hangman image (starts from a blank one), entering the correct answer will lead to an error instead. Also, I don't know how to make Java read from a text file. The text file will contain other words for use in the game so the game will not only have "hello" as the concealed word.

Thanks
 

Diablo1123

Newcomer
Member
Joined
Mar 9, 2008
Messages
1,432
Trophies
0
Age
30
Location
Underground
Website
Visit site
XP
240
Country
United States
Won't compile for me at the initComponents();, but I would use a BufferedReader for the text file to make a string array of all the words. Then randomly selecting a word from that array with a random number generator and turning that word into a char array. Which then is just some editing of your code to make it work.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    Psionic Roshambo @ Psionic Roshambo: Tonight's nightmare sponsored but G-Fuel! lolo