Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement