刽子手添加图片Swing

时间:2018-01-09 10:21:49

标签: java image swing

我对使用Swing在Java(Eclipse)中创建Hangman游戏的java“相对”不熟悉。我已经获得了与完成单词相关的所有组件。

但是我希望在屏幕中间显示一个Hangman图像,当你猜错时它会更新一个不同的图像(一个更完整的刽子手)。

但是我不确定如何执行此操作,是否需要ImageIcon或仅使用paintComponent等图片。如果有任何帮助,我的代码如下。我不确定如何在一个有效的方法中做到这一点,每次变量猜测下降时我都可以改变它。请建议一种方法,以及它的工作原理。

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

0 个答案:

没有答案