java - 内存游戏一些问题

标签 java arrays jbutton

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

我现在有两个问题:

  1. 我不明白如何在开始时隐藏图片(而不是现在让它们可见)
  2. 我也在尝试弄清楚如何比较这些图片。

这是代码,

谢谢。

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



    }
}

最佳答案

我认为你应该在照片和一些唯一的 ID 之间进行映射。即每张图片都由一个 uid 表示,当您想要显示图片时,请使用 map[uid] 并比较图片,只需比较 uid 即可。

关于java - 内存游戏一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23067608/

相关文章:

java - 一个普通的 servlet 可以被配置为一个 seam 组件吗?

javascript - JavaScript 中的分区细化算法

java - 内部带有 JTextArea 组件的自定义 JButton

java - JButton.setText 水平切割

java - Jodatime重叠方法不检查等于

java - 使用java代码进行Saml断言解密

java - 在 Android 中向日历添加一天在 31 日失败

arrays - 如何在PL/SQL函数中使用传递数组

javascript - 如何使用 es6 展平嵌套对象数组

java - 除非先单击 JButton,否则 keyPressed 不起作用