Good day!
hello! lately i've been working on a project of mine and im almost done. but right now i have a problem. in my hangman program i cant seem to make thing that hangs the hangman appear, it will only appera after ive clicked the check button. can anyone help me please? the code is below.
P.S.
I've already tried the:
public void paintComponent (Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(130,450,240,450);
g.drawLine(185,40,185,450);
g.drawLine(110,40,185,40);
g.drawLine(110,40,110,100);
}
using the code above the thing (that hangs the man) still wont appear when i execute the program.
any help is greatly appreciated :)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Hangman extends JFrame implements ActionListener {
// DECLARATIONS
JLabel inputL,
lettersL,
wordL;
JTextField inputTF,
lettersTF,
wordTF;
JButton checkB,
exitB;
final String WORD[] = { "SPLINTER" , "MAGICAL" , "FUNDAMENTAL" , "ONYX" , "UNIVERSAL"
, "MYSTERIOUS" , "QUAIL" , "DISCOVER" , "UNIQUE" , "OLYMPICS" };
static int index;
int chances = 6;
char[] blanks,guess;
String usedLetters = "";
public Hangman()
{
JPanel thePanel = new JPanel();
thePanel.setBackground(Color.black);
inputL = new JLabel("Enter a letter");
lettersL = new JLabel("Used Letters");
wordL = new JLabel("The Word");
inputTF = new JTextField();
lettersTF = new JTextField(26);
wordTF = new JTextField(16);
inputTF.setFont(new Font("Arial",Font.BOLD,20));
inputTF.setHorizontalAlignment(0);
lettersTF.setFont(new Font("Arial",Font.BOLD,20));
lettersTF.setEditable(false);
lettersTF.setHorizontalAlignment(JTextField.CENTER);
wordTF.setFont(new Font("Arial",Font.BOLD,20));
wordTF.setEditable(false);
wordTF.setHorizontalAlignment(JTextField.CENTER);
String text = "";
for(int ctr=0 ; ctr < WORD[index].length() ; ctr++)
text = text + "_ ";
wordTF.setText(text);
blanks = text.toCharArray();
checkB = new JButton("Check");
checkB.addActionListener(this);
exitB = new JButton("EXIT");
exitB.addActionListener(this);
Container pane = getContentPane();
pane.setLayout(new GridLayout(1,2,10,0));
Container pane1 = new Container();
pane1.setLayout(new GridLayout(8,1,8,8));
pane1.add(wordL);
pane1.add(wordTF);
pane1.add(lettersL);
pane1.add(lettersTF);
pane1.add(inputL);
pane1.add(inputTF);
pane1.add(checkB,BorderLayout.EAST);
pane1.add(exitB,BorderLayout.SOUTH);
pane.add(thePanel);
pane.add(pane1);
setResizable(false);
setTitle("Hangman BETA VERSION");
setLocation(120,120);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,500);
}
public void actionPerformed (ActionEvent e)
{
Graphics g = getGraphics();
g.setColor(Color.red);
g.drawLine(130,450,240,450);
g.drawLine(185,40,185,450);
g.drawLine(110,40,185,40);
g.drawLine(110,40,110,100);
if(e.getSource()==exitB)
System.exit(0);
else if(e.getSource()==checkB)
{
char letter;
String input;
Boolean correct = false;
input = inputTF.getText();
letter = Character.toUpperCase( input.charAt(0) );
if(input.equals(null))
JOptionPane.showMessageDialog(null,"You Have to Enter Something!","ERROR",JOptionPane.WARNING_MESSAGE);
else if( input.length() != 1)
JOptionPane.showMessageDialog(null,"ENTER A SINGLE CHARACTER!","ERROR",JOptionPane.WARNING_MESSAGE);
else if(Character.isDigit(letter))
JOptionPane.showMessageDialog(null,"ENTER A CHARACTER NOT A NUMBER","ERROR",JOptionPane.WARNING_MESSAGE);
else
{
guess = WORD[index].toCharArray();
for(int ctr =0 ; ctr < WORD[index].length() ; ctr++)
{
if( letter == guess[ctr] )
{
blanks[ctr*2]=guess[ctr];
correct = true;
}
}
if (!correct)
{
usedLetters = usedLetters + letter + " ";
switch (chances)
{
case 1: g.drawArc(85,150,50,30,0,180); chances--; break;
case 2: g.drawLine(90,130,105,130); g.drawLine(115,130,130,130); chances--; break;
case 3: g.drawLine(70,230,150,230); chances--; break;
case 4: g.drawLine(110,350,70,400); g.drawLine(110,350,150,400); chances--; break;
case 5: g.drawLine(110,180,110,350); chances--; break;
case 6: g.drawOval(70,100,80,80); chances--; break;
}
if(chances==0)
{
JOptionPane.showMessageDialog(null,"GAMEOVER!\n The Word is " + WORD[index],"GAMEOVER!",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
wordTF.setText(new String(blanks));
}
lettersTF.setText(new String(usedLetters));
inputTF.setText("");
}
}
public static void main (String [] args)
{
index = (int) ( ( Math.random() ) * 10 );
Hangman theFrame = new Hangman();
}
}