0

I am 'relatively' new to java creating a Hangman game in Java (Eclipse) using Swing. I have got all the components relating to the word done.

However I want that in the middle of the screen a Hangman image to be shown and when you get a guess wrong it updated with a different image (Of a more complete hangman).

However I am unsure of how to do this, whether I would need ImageIcon or just images and using paintComponent etc. Any Help appreciated, my code is below. I am unsure of how to do it in a efficient method that allows me to change it every time variable guess goes down. Please Suggest a method for doing this and the basics of how it might work.

public class Test  extends JTextField    {

    private JFrame frame;
    JTextField userInput;
    private JTextField textField;
    private String inputString;
    private String total="";
    private static  char[] pass = Hangmangame.word.getPassword();
    private static  String passString = new String(pass);
    private String []wordch = new String[passString.length()];
    private String []astri = new String[passString.length()];
    private int guess;
    private boolean letter = false;
    private JTextField txtGuesses;
    private JPanel PicturePanel;


    public static void start() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    Test window = new Test();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Test() {
        initialize();
    }

    public void initialize()  {

        for (int i = 0; i < passString.length(); i++) {
            astri[i]="*";
            wordch[i]=passString.substring(i, i+1);
            total =total+ astri[i];
            guess =10;
        }
        //------------------------------------------------
        frame = new JFrame();
        frame.setBounds(300, 100, 1000, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));
        //------------------------------------
        JLabel Hangman = new JLabel("Hangman");
        Hangman.setFont(new Font("Tahoma", Font.BOLD, 46));
        Hangman.setHorizontalAlignment(SwingConstants.CENTER);
        frame.getContentPane().add(Hangman, BorderLayout.NORTH);
        //----------------------------------
        textField = new JTextField(total);
        textField.setBorder(null);
        textField.setHorizontalAlignment(SwingConstants.CENTER);
        textField.setFont(new Font("Tahoma", Font.BOLD, 46));
        frame.getContentPane().add(textField, BorderLayout.SOUTH);
        textField.setColumns(10);


        //------------------------------------------
        userInput = new JTextField();
        userInput.setBorder(new LineBorder(new Color(0, 0, 0)));

        userInput.setFont(new Font("Tahoma", Font.PLAIN, 22));
        userInput.setHorizontalAlignment(SwingConstants.CENTER);
        frame.getContentPane().add(userInput, BorderLayout.EAST);
        userInput.setColumns(10);

        TextFieldListener tfListener = new TextFieldListener();
        userInput.addActionListener(tfListener);
        //----------------------------------------------

        txtGuesses = new JTextField();
        txtGuesses.setBorder(null);
        txtGuesses.setHorizontalAlignment(SwingConstants.LEFT);
        txtGuesses.setFont(new Font("Tahoma", Font.BOLD, 20));
        txtGuesses.setText("Guesses:"+guess);
        frame.getContentPane().add(txtGuesses, BorderLayout.WEST);
        txtGuesses.setColumns(10);
        //-------------------------------
        PicturePanel = new JPanel();
        PicturePanel.setBackground(new Color(255, 255, 255));
        frame.getContentPane().add(PicturePanel, BorderLayout.CENTER);
        //----------------------------------------------
    }


    public class TextFieldListener implements ActionListener
    {  public void actionPerformed(ActionEvent evt)
    {   inputString = userInput.getText();
    userInput.setText("");
    checkcorrect();
    }
    }

    private void checkcorrect() {
        //runs it as many times as there are letters
        for (int i = 0; i < passString.length(); i++) {
            if(wordch[i].equals(inputString)) {

                astri[i]=wordch[i];
                total="";
                // rewrites the *** with the letter added in same as code for setting up the astrixs.
                for (int x = 0; x < passString.length(); x++) {

                    total =total+ astri[x];
                }
                textField.setText(total);
                letter = true;

            }
        }

        if(letter==true) {
            letter=false;
        }else {
            guess=guess-1;
            txtGuesses.setText("Guesses:"+guess);

            if(guess==0) {
                if(JOptionPane.showConfirmDialog(frame,"You Lose","Hangman", 
                        JOptionPane.PLAIN_MESSAGE ) == JOptionPane.YES_OPTION   ) {
                    System.exit(0);
                }
            }
        }
        if(total.equals(passString)) {

            if(JOptionPane.showConfirmDialog(frame,"You Win","Hangman", 
                    JOptionPane.PLAIN_MESSAGE ) == JOptionPane.YES_NO_OPTION    ) {
                System.exit(0);
            }
        }


    }
    private void Image() {
        // My images
        ImageIcon icon1 = new ImageIcon("P:\\Profiles\\workspace\\Games\\Images\\files\\0");
        ImageIcon icon2 = new ImageIcon("P:\\Profiles\\workspace\\Games\\Images\\files\\1");
        ImageIcon icon3 = new ImageIcon("P:\\Profiles\\workspace\\Games\\Images\\files\\2");
        ImageIcon icon4 = new ImageIcon("P:\\Profiles\\workspace\\Games\\Images\\files\\3");
        ImageIcon icon5 = new ImageIcon("P:\\Profiles\\workspace\\Games\\Images\\files\\4");
        ImageIcon icon6 = new ImageIcon("P:\\Profiles\\workspace\\Games\\Images\\files\\5");
        ImageIcon icon7 = new ImageIcon("P:\\Profiles\\workspace\\Games\\Images\\files\\6");
        ImageIcon icon8 = new ImageIcon("P:\\Profiles\\workspace\\Games\\Images\\files\\7");
        ImageIcon icon9 = new ImageIcon("P:\\Profiles\\workspace\\Games\\Images\\files\\8");
        ImageIcon icon10 = new ImageIcon("P:\\Profiles\\workspace\\Games\\Images\\files\\9");
        ImageIcon icon11 = new ImageIcon("P:\\Profiles\\workspace\\Games\\Images\\files\\10");
    }
    }
  • Please be a bit more specific when asking a question: *What have you tried so far with a code example? ([I downvoted because there is no code](http://idownvotedbecau.se/nocode/))* / *What do you expect?* / *What error do you get?* **For Help take a look at "[How to ask](https://stackoverflow.com/help/how-to-ask)"** – Hille Jan 09 '18 at 10:22
  • I'd say use whatever you feel is best. You can do it either way, i.e. different ImageIcons or just a panel that renders an image (e.g. as its background image). Just try something - I guess that game is a learning excercise anyways. – Thomas Jan 09 '18 at 10:29
  • An `ImageIcon` displayed in a `JLabel` would be a simpler way to show the gallows and hanged man. You've loaded images in local variables in a method. Make those (locally declared) image icons attributes of the class (like the others declared at the top) & try progressing the problem. In future, for better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). (Try loading one or two images instead of 10, and remove the cruft that is not related to loading and displaying images). One way to get image(s) for an example is to .. – Andrew Thompson Jan 09 '18 at 10:30
  • .. hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jan 09 '18 at 10:30

0 Answers0