记忆游戏一些问题

时间:2014-04-14 18:34:09

标签: java arrays jbutton

我正在尝试用图像创建记忆游戏。用户必须匹配图片。

我现在有两个问题:

  1. 我不明白如何在开头隐藏图片(而不是让它们在现在可见)
  2. 我也想弄清楚如何比较图片。
  3. 这是代码,

    谢谢。

    package lib;
    
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class MemoryGame extends JFrame implements ActionListener {
    
        private Timer tmrTime;
        private int seconds = 0;
    
        private JPanel panel;
        private JLabel lblTimer;
    
        private JButton btnStart;
        private int p, q, c, count = 0;
        private int[][] check = new int[4][4];
    
        private static ImageIcon one = new ImageIcon("a.png"), two = new ImageIcon(
                "b.png"), three = new ImageIcon("c.png"), four = new ImageIcon(
                "d.png"), five = new ImageIcon("e.png"), six = new ImageIcon(
                "f.png"), seven = new ImageIcon("g.png"), eight = new ImageIcon(
                "h.png");
    
        private static ImageIcon[] a = { one, two, three, four, five, six, seven,
                eight };
        private static JButton[][] b = new JButton[4][4];
    
        public static void main(String[] args) {
    
            new MemoryGame();
        }
    
        public MemoryGame() {
    
            for (int i = 0; i < a.length; i++) {
                c = 0;
                do {
                    p = (int) (Math.random() * 4);
                    q = (int) (Math.random() * 4);
                    if (check[p][q] == 0) {
                        c++;
                        check[p][q]++;
                        b[p][q] = new JButton(a[i]);
                        b[p][q].setPreferredSize(new Dimension(150, 100));
    
    
                        b[p][q].setBorder(BorderFactory.createLineBorder(Color.black));
                        b[p][q].setMargin(new Insets(0, 0, 0, 0));
                        //b[p][q].setBorder(BorderFactory.createEmptyBorder());
                        b[p][q].setContentAreaFilled(false);
                        b[p][q].addActionListener(this);
    
    
                    }
                } while (c != 2);
            }
    
            panel = new JPanel();
            panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
    
            JLabel lblTitle = new JLabel("MEMORY GAME");
            lblTitle.setPreferredSize(new Dimension(700, 30));
            lblTitle.setFont(new Font("Britannic Bold", Font.BOLD, 28));
            lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
            panel.add(lblTitle);
    
    
    
            JLabel lblTime = new JLabel("ELAPSED TIME:");
            lblTime.setFont(new Font("Britannic Bold", Font.BOLD, 18));
    
            lblTimer = new JLabel();
            lblTimer.setPreferredSize(new Dimension(100, 20));
            lblTimer.setFont(new Font("Britannic Bold", Font.BOLD, 18));
            lblTimer.setHorizontalAlignment(SwingConstants.CENTER);
    
            btnStart = new JButton("START");
            btnStart.setPreferredSize(new Dimension(660, 30));
            btnStart.setFocusable(false);
            btnStart.setFont(new Font("Tahoma", Font.BOLD, 14));
            btnStart.addActionListener(this);
    
    
    
            for (int i = 0; i < b.length; i++) {
                for (int j = 0; j < b[i].length; j++) {
                    panel.add(b[i][j]);
                }
            }
    
            panel.add(btnStart);
            panel.add(lblTime);
            panel.add(lblTimer);
    
            setContentPane(panel);
            setSize(800, 720);
            setTitle("Memory Game");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e) {
    
            seconds++;
            int minutes = seconds / 60;
            int sec = seconds % 60;
    
    
            if (sec < 10) {
                lblTimer.setText("" + minutes + ":0" + sec);
            } else if (seconds > 9) {
                lblTimer.setText("" + minutes + ":" + sec);
            }
    
            if (e.getSource() == btnStart && count == 0) {
                btnStart.setText("EXIT");
                for (int i = 0; i < b.length; i++) {
                    for (int j = 0; j < b[i].length; j++) {
    
                        //b[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
                        b[i][j].setOpaque(false);
                        b[i][j].setContentAreaFilled(false);
                        b[i][j].setBorderPainted(false);
    
    
                    }
                }
    
                count++;
    
                tmrTime = new Timer(1000, this);
                tmrTime.start();
    
    
            } else if (e.getSource() == btnStart && count > 0) {
                int option = JOptionPane.showConfirmDialog(null,
                        "Are you sure you want to exit ?", "Memory Game",
                        JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            } else   { 
    
    
                if (e.getSource() == b[p][q]) { 
                    b[p][q].getSource();
                } 
            }
    
    
    
        }
    }
    

1 个答案:

答案 0 :(得分:0)

我认为你应该在照片和一些独特的身份证之间进行映射。即,每个pic将由uid表示,当您想要显示pic使用地图[uid]并比较图片时,只需比较uid。