hi i am trying to make a hangman game and what ineed to do is when there is wrong answer to put pic of part of the hangman but dont no how to do it, here is my code

thanks

int charPos = +1;
    
String letter = ALetter.getText();
charPos = FindWord.indexOf(letter);
MyMessage.setText("Position is " + charPos);

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

    }                                         

    public static void main(String args[]) {
        
                    public void run() {
                new HangMan().setVisible(true);
            }
        });
    }

hey i actually had to do a hangman game recently! here is a little snippet of code that was in my overwritten paint method, note that i had a multiplayer option so i made them choose a character from the characters of FF1 on nes so thats why i use 2 arrays, 1 for the char, 1 for the number of errors.

also note that while my friend tried to use 1 image per body part and sync them, i jsut went with 6 images for each number of errors possible.

if(nbErreurs[i] > 0){
    //selection du bon personage a dessiner et de son nombre de membre a dessiner
    Image img = null;
    try {
        img = ImageIO.read(this.getClass().getResource("Images\\pendu"+character[i]+"-"+nbErreurs[i]+".png"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    //dessiner pendu
    gPanel.drawImage(img, 50, 50, null);
}

worth noting if you use a background that jpg will not maintain the alpha chanel so keep your images in png!

as far as your hangman logic im not sure what you have done so far from that code you posted but im not sure you are taking the easiest route about character positions.

good luck.

hi thanks for reply here is the full code of it...

public class HangMan extends javax.swing.JFrame {

    String FindWord = "hello";
    
    public HangMan() {
        initComponents();
    }

    @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();
        InCorrect = new javax.swing.JLabel();
        MyMessage = new javax.swing.JLabel();
        TryLetter = new javax.swing.JButton();
        EnterLetter = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("HangMan");

        Char0.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        Char0.setText("_");

        Char1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        Char1.setText("_");

        Char2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        Char2.setText("_");

        Char3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        Char3.setText("_");

        Char4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        Char4.setText("_");

        Char5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        Char5.setText("_");

        ALetter.setText("e");

        InCorrect.setText("0");

        MyMessage.setText("Look Here");

        TryLetter.setText("Try Letter");
        TryLetter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TryLetterActionPerformed(evt);
            }
        });

        EnterLetter.setText("Enter Letter Here");

                
      
int charPos = +1;
    
String letter = ALetter.getText();
charPos = FindWord.indexOf(letter);
MyMessage.setText("Position is " + charPos);

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


    }                                         

    public static void main(String args[]) {
        
                  public void run() {
                new HangMan().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    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 EnterLetter;
    private javax.swing.JLabel InCorrect;
    private javax.swing.JLabel MyMessage;
    private javax.swing.JButton TryLetter;
    // End of variables declaration
}

i have also got the pics of the hangman to do but dont no how???

thanks paul

hmm i see a few things that make my eyes bleed in this code, some not important, some a bit more.

i personally keep my global variables on top, not bottom.

import javax.swing.*; instead of having all the "private javax.swing.JLabel Char0;"

class names start with Capitalized letters, functions and variables dont (Naming conventions)

instead of 5 objects (JLabels in this case) with name1,name2... use arrays (labels[index])

but dont even use a label array , you dont need 1 label per letter, not only does it hardcode your program to a limit of 5 letter words but it also complicates layout and coding.

use a single label with a function like setHiddenWord(); where you have an array of letters used so far , and build a string going over each letter of the word to find and if the letter is in the array of used letters add that to the string otherwise add an underscore ("_") and voila!

you never add your components to the contentPane. your gui isn't showing anything is it?

do some work towards those GUI points and come back with some code improvement, then we can talk about the hangman stuff.

this code is how it set out in my program from GUI i didnt type it i drag and dropped my swing controls and then done the code to the button and this GUI code is working fine and showing..

ah, net beans huh? alright , well that covers the contentPane adding part problem but i still recommand the naming convention and single label for displaying the hidden word.

yeh netbeans lol i was using eclipse but it hasnt got the GUI interface like netbeans other wise i would of used that...

i use eclipse BECAUSE it doesn't have the GUI helper, id much rather create my own code, especially while i was in school.

writing my own code means i know EXACTLY WHAT i wrote and WHERE. makes debugging a lot easier when you see something and you can say "oh that is because i set a GridLayout instead of a FlowLayout" ... or anything like that.

it lets me understand the way the GUI is going to react to diferent situations because i chose a specific approach in my design rather than let net beans choosing for me.

there are many other reasons but they are covered in diferent threads :)

yeh i was thinking of going back to eclispe as i did like it, im studying my first year in software develpoment so im just learning at the moment, would u say eclispe is better than netbeans especially for GUI applications and is it easyer???

it wouldn't be fair to say one is better than the other imo, we always had the choice at school, i prefered eclipse. im sure some would disagree.

can anybody help please need it done by monday

thanks

did my snippet not work?

wot snippet lol

yeh but not sure where to put it on my code

there are 2 ways to add this , you can put it in a custom method and call that method when the number of errors change, or put it in the overwritten paint() method and call repaint() when the number of errors change.

in the first scenario you will need to declare and define g , by grabing your panel's GraphicContext.

in the second g will already be passed as an argument.

you have a gui , somewhere in that gui should be a place where you plan on drawing the image, that place is probably held by a JPanel or something similar like JCanvas.

write a method in your class that goes like this :

private void drawMyStuff(){
    //get the current graphic context of you drawing component (jpanel or canvas or soemthing)
    Graphics gPanel = yourComponentsNameHere.getGraphics();

    //use gPanel to draw/paint stuff on your component
    gPanel.setColor(Color.BLACK);
    gPanel.drawRect(0, 0, wid, hei);

    //or to paint IMAGES WARNING CODE SPOILER HERE :)
    Image img = ImageIO.read(this.getClass().getResource("Images\\char"+i+".png"));
    gPanel.drawImage(img, (wid-80)/2, (hei-80)/2, null);
}

ive already got a private void

private void TryLetterActionPerformed(java.awt.event.ActionEvent evt)

Put a blank label placeholder in your UI where you want the picture displayed. When the guess is wrong, use the JLabel.setIcon() method to put the correct picture in the label.

Helpful tutorial: How to use icons

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.