Does any one made a hangman gui game there in java.... not extended to applet ok?
Printable View
Does any one made a hangman gui game there in java.... not extended to applet ok?
If you need to ask that question, you need to go through this tutorial: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
Unless of course you were mistakenly hoping that someone would provide a free handout.
db
And don't double post. The other thread you started with the same question has been locked.
db
I vote that this thread be deleted as it (and his double post) are nothing more than a beg for code and thus not appropriate for the forum. Thoughts?
nice grammar
actually i decided to give this a try and have run into some problems. hope no one minds if i post what i have so far in this thread. i am new to game programming so forgive me for what i'm sure is sloppy programming.
import javax.swing.JFrame;
public class HangmanFrame
{
//----------------------------------------------
// Creates the main frame of the program.
//----------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Hangman");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
HangmanPanel panel = new HangmanPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HangmanPanel extends JPanel
{
private JLabel inputLabel; // Input field for letter guesses
private String word; // Variable to hold answer word
private String guessed = "Guessed: "; // Holds letters already guessed
private String l, text = ""; // l: Variable to hold letter guess; text: Tells user whether their guess was correct
private JTextField letter; // Text Field for Input Label
private int counter = 0; // Incremented when wrong guess is made; controls drawing of hangman
private String underscore = ""; // Shows answer as sequence of underscores, which are replaced with correct guesses
//------------------------------------------
// Sets up the hangman panel.
//------------------------------------------
public HangmanPanel()
{
setBackground (Color.white);
setPreferredSize (new Dimension(300, 300));
askQuestion();
inputLabel = new JLabel ("Enter a letter.");
add (inputLabel);
letter = new JTextField (1);
add (letter);
letter.addActionListener(new TempListener());
}
public void askQuestion()
{
word = JOptionPane.showInputDialog ("Enter the word.");
for (int i = 0; i < word.length(); i++)
{
underscore += "-";
}
}
//----------------------------------
// Paints a hanging man.
//----------------------------------
public void paintComponent (java.awt.Graphics page)
{
super.paintComponent (page);
final int xBound = 200, yBound = 20;
page.setColor (Color.black);
page.fillRect(xBound + 30, yBound + 80, 60, 10); // Stand
page.fillRect (xBound +55, yBound, 10, 80); // Gallows Pole
page.fillRect(xBound + 25, yBound, 40, 5);
page.fillRect(xBound + 25, yBound, 3, 20); // Rope
if (counter > 0)
page.fillOval(xBound + 18, yBound + 15, 16, 16); // Head
if (counter > 1)
page.fillRect(xBound + 24, yBound + 23, 5, 30); // Torso
if (counter > 2)
page.drawLine(xBound + 23, yBound + 40, xBound + 13, yBound + 30); // Right Arm
if (counter > 3)
page.drawLine(xBound + 29, yBound + 40, xBound + 39, yBound + 30); // Left Arm
if (counter > 4)
page.drawLine(xBound + 23, yBound + 53, xBound + 18, yBound + 63); // Right Leg
if (counter > 5)
{
page.drawLine(xBound + 29, yBound + 53, xBound + 34, yBound + 63); // Left Leg
text = "You are dead.";
page.drawString("Play Again?", 50, 130);
HangmanPanel newGame = new HangmanPanel();
}
page.drawString(guessed, 20, 130);
page.drawString(underscore, 20, 110);
page.drawString(text, 20, 250);
}
private class TempListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
l = letter.getText(); // Stores letter guess as a string
letter.setText(""); // Clears Text Field
char let = l.charAt(0); // Creates a char variable for letter guess
guessed = guessed + let + " ";
int index = word.indexOf(let);
if (index != -1)
{
text = "Correct";
underscore = underscore.substring(0,index) + word.substring(index, index+1) + underscore.substring(index+1); // Replaces underscore with found letter
String substring = word.substring(index+1);
int index2 = substring.indexOf(let);
while (substring.indexOf(let) != -1)
{
index2 = substring.indexOf(let);
index = index + index2 + 1;
underscore = underscore.substring(0,index) + word.substring(index, index+1) + underscore.substring(index+1);
substring = word.substring(index+1);
}
}
else
{
text = "Wrong";
counter++;
}
if (word.indexOf('-') == -1)
text = "You Win!";
repaint();
}
}
}
anyways i am happy with how the code works except that it is only good for one game; i haven't figured out how to display that the user has won or lost and then ask if they would like to play again. if anyone can help me out it would be much appreciated