-2

I want to make a very simple hangman program with a GUI that uses 6 images to show the progression of the game. I want to hardcode the word "Hello" and for each mistake the user makes the image changes. (Total of 6 progressive images).

I've managed to make the GUI using the Swing library on Netbeans, but I'm stumped as to how I can proceed with the actual code. Any pointers?

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.util.Random;
import java.io.*;


 /*
  * To change this license header, choose License Headers in Project               
Properties.

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

 /**
  *
  * @author ruzni
  */
 public class GuessTheWord extends javax.swing.JPanel {

     /**
      * Creates new form GuessTheWord
      */
     public GuessTheWord() {
        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.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated    Code">                          
      private void initComponents() {

    char0 = new javax.swing.JLabel();
    char1 = new javax.swing.JLabel();
    char2 = new javax.swing.JLabel();
    char3 = new javax.swing.JLabel();
    char4 = new javax.swing.JLabel();
    char5 = new javax.swing.JLabel();
    aLetter = new javax.swing.JTextField();
    TryLetter = new javax.swing.JButton();
    Img = new javax.swing.JButton();

    char0.setText("X");

    char1.setText("X");

    char2.setText("X");

    char3.setText("X");

    char4.setText("X");

    char5.setText("X");

    aLetter.setText("e");
    aLetter.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            aLetterActionPerformed(evt);
        }
    });

    TryLetter.setText("TryLetter");
    TryLetter.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            TryLetterMouseClicked(evt);
        }
    });

    Img.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img000.png"))); // NOI18N
    Img.setText("jButton3");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(20, 20, 20)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(TryLetter)
                .addComponent(aLetter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(char0)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(char1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(char2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(char3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(char4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(char5)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
            .addComponent(Img, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(21, 21, 21))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addComponent(aLetter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(26, 26, 26)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(char0)
                        .addComponent(char1)
                        .addComponent(char2)
                        .addComponent(char3)
                        .addComponent(char4)
                        .addComponent(char5))
                    .addGap(71, 71, 71)
                    .addComponent(TryLetter))
                .addGroup(layout.createSequentialGroup()
                    .addGap(29, 29, 29)
                    .addComponent(Img, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(36, Short.MAX_VALUE))
    );
}// </editor-fold>                        

private void aLetterActionPerformed(java.awt.event.ActionEvent evt) {                                        
//GEN-FIRST:event_guessActionPerformed
}                                       

private void TryLetterMouseClicked(java.awt.event.MouseEvent evt) {                                      

}                                     



// Variables declaration - do not modify                     
private javax.swing.JButton Img;
private javax.swing.JButton TryLetter;
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;
// End of variables declaration                   

}

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Here's a pointer: Do exactly the opposite. Forget the UI until you have an object model that behaves exactly as you want it when you drive it on the command line using text. Once you have that you can safely proceed with Swing. – duffymo Dec 04 '16 at 14:39

1 Answers1

0

First of all you need to start with the background code, not the visible part.

For example you could create an class called Hangman, which offers you a method to change the progress (add a mistake) and one that gives you the image for the current state to draw.

Pseudo Code like:

public class Hangman
{
    private int mistakesDone;

    private HashMap<Integer, Image> progressImages;

    public Hangman()
    {
        mistakesDone = 0;
        progressImages = new HashMap<Integer, Image>();

        for(int i = 0; i < 7;i++)
            progressImages.put(i, new ImageIcon(new URL("hangman_" + i)).getImage());
    }

    public Image getCurrentImage()
    {
        return proressImages.get(mistakesDone);
    }

    public void mistakeDone()
    {
        mistakesDone++;
    }

    public int getMistakesDone()
    {
        return mistakesDone;
    }

    public void resetMistakes()
    {
        mistakesDone = 0;
    }
}

Now that you got your Hangman class, you need to create an Object of it in your Main-Game-Class or however you call it. When you want to draw, you can ask the Object to get the current image. Remeber to update the mistakesDone!

I would reccomend you to do the same with the basic code of the guessing part as well! (Outsourcsing is a very very nice thing in Object-Orientated-Languages)

Good luck, if somethings not clear, comment the post.

Luftbaum
  • 303
  • 1
  • 13