Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. package lib;
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.awt.*;
  6. import java.awt.event.*;
  7.  
  8. public class MemoryGame extends JFrame implements ActionListener {
  9.  
  10. private Timer tmrTime;
  11. private int seconds = 0;
  12.  
  13. private JPanel panel;
  14. private JLabel lblTimer;
  15.  
  16. private JButton btnStart;
  17. private int p, q, c, count = 0;
  18. private int[][] check = new int[4][4];
  19.  
  20. private static ImageIcon one = new ImageIcon("a.png"), two = new ImageIcon(
  21. "b.png"), three = new ImageIcon("c.png"), four = new ImageIcon(
  22. "d.png"), five = new ImageIcon("e.png"), six = new ImageIcon(
  23. "f.png"), seven = new ImageIcon("g.png"), eight = new ImageIcon(
  24. "h.png");
  25.  
  26. private static ImageIcon[] a = { one, two, three, four, five, six, seven,
  27. eight };
  28. private static JButton[][] b = new JButton[4][4];
  29.  
  30. public static void main(String[] args) {
  31.  
  32. new MemoryGame();
  33. }
  34.  
  35. public MemoryGame() {
  36.  
  37. for (int i = 0; i < a.length; i++) {
  38. c = 0;
  39. do {
  40. p = (int) (Math.random() * 4);
  41. q = (int) (Math.random() * 4);
  42. if (check[p][q] == 0) {
  43. c++;
  44. check[p][q]++;
  45. b[p][q] = new JButton(a[i]);
  46. b[p][q].setPreferredSize(new Dimension(150, 100));
  47.  
  48.  
  49. b[p][q].setBorder(BorderFactory.createLineBorder(Color.black));
  50. b[p][q].setMargin(new Insets(0, 0, 0, 0));
  51. //b[p][q].setBorder(BorderFactory.createEmptyBorder());
  52. b[p][q].setContentAreaFilled(false);
  53. b[p][q].addActionListener(this);
  54.  
  55.  
  56. }
  57. } while (c != 2);
  58. }
  59.  
  60. panel = new JPanel();
  61. panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
  62.  
  63. JLabel lblTitle = new JLabel("MEMORY GAME");
  64. lblTitle.setPreferredSize(new Dimension(700, 30));
  65. lblTitle.setFont(new Font("Britannic Bold", Font.BOLD, 28));
  66. lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
  67. panel.add(lblTitle);
  68.  
  69.  
  70.  
  71. JLabel lblTime = new JLabel("ELAPSED TIME:");
  72. lblTime.setFont(new Font("Britannic Bold", Font.BOLD, 18));
  73.  
  74. lblTimer = new JLabel();
  75. lblTimer.setPreferredSize(new Dimension(100, 20));
  76. lblTimer.setFont(new Font("Britannic Bold", Font.BOLD, 18));
  77. lblTimer.setHorizontalAlignment(SwingConstants.CENTER);
  78.  
  79. btnStart = new JButton("START");
  80. btnStart.setPreferredSize(new Dimension(660, 30));
  81. btnStart.setFocusable(false);
  82. btnStart.setFont(new Font("Tahoma", Font.BOLD, 14));
  83. btnStart.addActionListener(this);
  84.  
  85.  
  86.  
  87. for (int i = 0; i < b.length; i++) {
  88. for (int j = 0; j < b[i].length; j++) {
  89. panel.add(b[i][j]);
  90. }
  91. }
  92.  
  93. panel.add(btnStart);
  94. panel.add(lblTime);
  95. panel.add(lblTimer);
  96.  
  97. setContentPane(panel);
  98. setSize(800, 720);
  99. setTitle("Memory Game");
  100. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  101. setLocationRelativeTo(null);
  102. setVisible(true);
  103. }
  104.  
  105. public void actionPerformed(ActionEvent e) {
  106.  
  107. seconds++;
  108. int minutes = seconds / 60;
  109. int sec = seconds % 60;
  110.  
  111.  
  112. if (sec < 10) {
  113. lblTimer.setText("" + minutes + ":0" + sec);
  114. } else if (seconds > 9) {
  115. lblTimer.setText("" + minutes + ":" + sec);
  116. }
  117.  
  118. if (e.getSource() == btnStart && count == 0) {
  119. btnStart.setText("EXIT");
  120. for (int i = 0; i < b.length; i++) {
  121. for (int j = 0; j < b[i].length; j++) {
  122.  
  123. //b[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
  124. b[i][j].setOpaque(false);
  125. b[i][j].setContentAreaFilled(false);
  126. b[i][j].setBorderPainted(false);
  127.  
  128.  
  129. }
  130. }
  131.  
  132. count++;
  133.  
  134. tmrTime = new Timer(1000, this);
  135. tmrTime.start();
  136.  
  137.  
  138. } else if (e.getSource() == btnStart && count > 0) {
  139. int option = JOptionPane.showConfirmDialog(null,
  140. "Are you sure you want to exit ?", "Memory Game",
  141. JOptionPane.YES_NO_OPTION);
  142. if (option == JOptionPane.YES_OPTION) {
  143. System.exit(0);
  144. }
  145. } else {
  146.  
  147.  
  148. if (e.getSource() == b[p][q]) {
  149. b[p][q].getSource();
  150. }
  151. }
  152.  
  153.  
  154.  
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement