Results 1 to 8 of 8
  1. #1
    aviolently is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default Hangman GUI project

    Does any one made a hangman gui game there in java.... not extended to applet ok?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    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

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    And don't double post. The other thread you started with the same question has been locked.

    db

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  5. #5
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    nice grammar

  6. #6
    smvolt is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default Re: Hangman GUI project

    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);

    }

    }

  7. #7
    smvolt is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default Re: Hangman GUI project

    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();

    }

    }

    }
    Last edited by smvolt; 01-01-2012 at 02:26 AM.

  8. #8
    smvolt is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default Re: Hangman GUI project

    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

Similar Threads

  1. Hangman
    By Feriscool in forum New To Java
    Replies: 23
    Last Post: 05-17-2011, 06:54 PM
  2. hangman
    By coltragon in forum New To Java
    Replies: 2
    Last Post: 01-16-2010, 09:56 AM
  3. hangman
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-14-2009, 09:06 AM
  4. Need help with Hangman!!!
    By chinasome in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 04:42 AM
  5. Hangman Help!!!
    By chinasome in forum New To Java
    Replies: 5
    Last Post: 11-08-2008, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •