返回列表 發帖

翻牌遊戲(三)

判斷當前的牌是否與上一張一樣,如果不一樣,則需要將排給還原
  1. package tw.kuas.edu.tw;

  2. import java.awt.BorderLayout;
  3. import java.awt.EventQueue;
  4. import java.awt.Image;

  5. import javax.swing.ImageIcon;
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;

  9. import java.awt.FlowLayout;

  10. import javax.swing.JLabel;

  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.MouseAdapter;
  14. import java.awt.event.MouseEvent;
  15. import java.util.Timer;
  16. import java.util.concurrent.TimeUnit;

  17. public class Main extends JFrame {

  18.         private JPanel contentPane;

  19.         /**
  20.          * Launch the application.
  21.          */
  22.         public static void main(String[] args) {
  23.                 EventQueue.invokeLater(new Runnable() {
  24.                         public void run() {
  25.                                 try {
  26.                                         Main frame = new Main();
  27.                                         frame.setVisible(true);
  28.                                 } catch (Exception e) {
  29.                                         e.printStackTrace();
  30.                                 }
  31.                         }
  32.                 });
  33.         }

  34.         /**
  35.          * Create the frame.
  36.          */
  37.         JLabel[] pokers = new JLabel[12];
  38.         ImageIcon[] randomcard = new ImageIcon[12];
  39.         ImageIcon[] answercard = new ImageIcon[12];
  40.         int randomnum[]=new int[12];
  41.         public Main() {
  42.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.                 setBounds(100, 100, 662, 499);

  44.                 contentPane = new JPanel();
  45.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  46.                 setContentPane(contentPane);
  47.                 contentPane.setLayout(null);

  48.                 JLabel Poker_1 = new JLabel("New label");
  49.                
  50.                 Poker_1.setBounds(10, 10, 128, 128);
  51.                 contentPane.add(Poker_1);
  52.                 pokers[0] = Poker_1;
  53.                

  54.                 JLabel Poker_2 = new JLabel("New label");
  55.                 Poker_2.setBounds(159, 10, 128, 128);
  56.                 contentPane.add(Poker_2);
  57.                 pokers[1] = Poker_2;
  58.                

  59.                 JLabel Poker_3 = new JLabel("New label");
  60.                 Poker_3.setBounds(304, 10, 128, 128);
  61.                 contentPane.add(Poker_3);
  62.                 pokers[2] = Poker_3;
  63.                

  64.                 JLabel Poker_4 = new JLabel("New label");
  65.                 Poker_4.setBounds(453, 10, 128, 128);
  66.                 contentPane.add(Poker_4);
  67.                 pokers[3] = Poker_4;
  68.                
  69.                
  70.                 JLabel Poker_5 = new JLabel("New label");
  71.                 Poker_5.setBounds(10, 155, 128, 128);
  72.                 contentPane.add(Poker_5);
  73.                 pokers[4] = Poker_5;
  74.                

  75.                 JLabel Poker_6 = new JLabel("New label");
  76.                 Poker_6.setBounds(159, 155, 128, 128);
  77.                 contentPane.add(Poker_6);
  78.                 pokers[5] = Poker_6;
  79.                

  80.                 JLabel Poker_7 = new JLabel("New label");
  81.                 Poker_7.setBounds(304, 155, 128, 128);
  82.                 contentPane.add(Poker_7);
  83.                 pokers[6] = Poker_7;
  84.                

  85.                 JLabel Poker_8 = new JLabel("New label");
  86.                 Poker_8.setBounds(453, 155, 128, 128);
  87.                 contentPane.add(Poker_8);
  88.                 pokers[7] = Poker_8;
  89.         

  90.                 JLabel Poker_9 = new JLabel("New label");
  91.                 Poker_9.setBounds(10, 293, 128, 128);
  92.                 contentPane.add(Poker_9);
  93.                 pokers[8] = Poker_9;
  94.                

  95.                 JLabel Poker_10 = new JLabel("New label");
  96.                 Poker_10.setBounds(159, 293, 128, 128);
  97.                 contentPane.add(Poker_10);
  98.                 pokers[9] = Poker_10;
  99.         

  100.                 JLabel Poker_11 = new JLabel("New label");
  101.                 Poker_11.setBounds(304, 293, 128, 128);
  102.                 contentPane.add(Poker_11);
  103.                 pokers[10] = Poker_11;
  104.                

  105.                 JLabel Poker_12 = new JLabel("New label");
  106.                 Poker_12.setBounds(453, 293, 128, 128);
  107.                 contentPane.add(Poker_12);
  108.                 pokers[11] = Poker_12;
  109.                


  110.                 InitializeImg(pokers);
  111.                 rc();
  112.                 InitAnsImg(randomnum);
  113.                 AddListener();
  114.                 JLabel Reset = new JLabel("New label");
  115.                 Reset.setIcon(new ImageIcon(Main.class.getResource("/tw/kuas/edu/tw/pic/return.png")));
  116.                 Reset.setBounds(603, 418, 33, 33);
  117.                 contentPane.add(Reset);
  118.                 for(int i=0;i<12;i++) {
  119.                         System.out.println(answercard[i]);
  120.                 }

  121.         }

  122.         private ImageIcon InitImg() {
  123.                 ImageIcon icon = new ImageIcon(Main.class.getResource("pic/0.png"));
  124.                 Image img = icon.getImage().getScaledInstance(128, 128,  java.awt.Image.SCALE_FAST );
  125.                 ImageIcon newIcon = new ImageIcon(img);
  126.                 return newIcon;
  127.         }

  128.         private void InitializeImg(JLabel[] pokers) {
  129.                 for(int i=0;i<pokers.length;i++) {
  130.                         ImageIcon icon = new ImageIcon(Main.class.getResource("pic/0.png"));
  131.                         Image img = icon.getImage().getScaledInstance(128, 128,  java.awt.Image.SCALE_FAST );
  132.                         ImageIcon newIcon = new ImageIcon(img);
  133.                         pokers[i].setIcon(newIcon);
  134.                 }
  135.         }

  136.         private void rc() {
  137.                 for(int i=0; i<12; i++) {
  138.                         randomnum[i]=(int)(Math.random()*12);    //0~11
  139.                         for(int j=0; j<i; j++) {
  140.                                 if(randomnum[i]==randomnum[j]) {
  141.                                         i--;
  142.                                         break;
  143.                                 }
  144.                         }
  145.                 }
  146.         }

  147.         private void InitAnsImg(int[] randomnum) {
  148.                
  149.                 for(int i=1;i<=6;i++) {
  150.                         ImageIcon icon = new ImageIcon(Main.class.getResource(String.format("pic/%d.png",i)));
  151.                         Image img = icon.getImage().getScaledInstance(128, 128,  java.awt.Image.SCALE_FAST );
  152.                         answercard[randomnum[i-1]] = new ImageIcon(img); //0-5
  153.                         answercard[randomnum[i+5]] = new ImageIcon(img); //6-11
  154.                 }
  155.         }
  156.         
  157.         private void AddListener() {
  158.                 for(int i=0;i<pokers.length;i++) {
  159.                         pokers[i].addMouseListener(adapter);
  160.                 }
  161.         }
  162.         // 給予一個共用的MouseAdapter事件
  163.          ImageIcon temp = null;
  164.          int locate = 0;
  165.          boolean isFirst = true;
  166.          MouseAdapter adapter=new MouseAdapter(){
  167.                  @Override
  168.          public void mouseClicked(MouseEvent e)
  169.          {
  170.                  for(int i=0; i<12; i++)
  171.               {
  172.                          
  173.                          // 判斷當下使用者點選的牌是哪一張
  174.                       if(e.getSource()==pokers[i] && isFirst) {
  175.                              // 翻牌
  176.                               pokers[i].setIcon(answercard[i]);
  177.                              // 紀錄當前的圖片以及位置
  178.                              temp = answercard[i];
  179.                              locate = i;
  180.                              isFirst = false;
  181.                              System.out.println("第一次翻牌");
  182.                              break;
  183.                       }
  184.                      
  185.                       // 第二次翻牌
  186.                       if(isFirst == false  && e.getSource()==pokers[i]) {
  187.                             // 翻牌
  188.                               pokers[i].setIcon(answercard[i]);
  189.                                
  190.                                                 System.out.println("第二次翻牌");
  191.                                                
  192.                                                  // 判斷第二次翻的牌跟第一次有沒有一樣,如果不一樣則需要把第一張牌翻回去
  193.                                if(temp != answercard[i]) {
  194.                                      
  195.                                                          ImageIcon defaultIcon = new ImageIcon(Main.class.getResource("pic/0.png"));
  196.                                        pokers[locate].setIcon(defaultIcon);
  197.                                         try {
  198.                                                       Thread.currentThread();
  199.                                                                 Thread.sleep(2000);
  200.                                                     //pokers[i].setIcon(defaultIcon);
  201.                                                   } catch (InterruptedException ie) {
  202.                                                      
  203.                                                }
  204.                                              

  205.                                                                
  206.                                        }
  207.                            
  208.                                        isFirst = true;
  209.                                                
  210.                              
  211.                       }
  212.                      
  213.                      
  214.                      
  215.                            
  216.               }
  217.               
  218.                  
  219.          }
  220. };


  221. }
複製代碼

package tw.kuas.edu.tw;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.FlowLayout;

import javax.swing.JLabel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.concurrent.TimeUnit;

public class Main extends JFrame {

        private JPanel contentPane;

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                try {
                                        Main frame = new Main();
                                        frame.setVisible(true);
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                });
        }

        /**
         * Create the frame.
         */
        JLabel[] pokers = new JLabel[12];
        ImageIcon[] randomcard = new ImageIcon[12];
        ImageIcon[] answercard = new ImageIcon[12];
        int randomnum[] = new int[12];

        public Main() {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setBounds(100, 100, 662, 499);

                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPane);
                contentPane.setLayout(null);

                JLabel Poker_1 = new JLabel("New label");

                Poker_1.setBounds(10, 10, 128, 128);
                contentPane.add(Poker_1);
                pokers[0] = Poker_1;

                JLabel Poker_2 = new JLabel("New label");
                Poker_2.setBounds(159, 10, 128, 128);
                contentPane.add(Poker_2);
                pokers[1] = Poker_2;

                JLabel Poker_3 = new JLabel("New label");
                Poker_3.setBounds(304, 10, 128, 128);
                contentPane.add(Poker_3);
                pokers[2] = Poker_3;

                JLabel Poker_4 = new JLabel("New label");
                Poker_4.setBounds(453, 10, 128, 128);
                contentPane.add(Poker_4);
                pokers[3] = Poker_4;

                JLabel Poker_5 = new JLabel("New label");
                Poker_5.setBounds(10, 155, 128, 128);
                contentPane.add(Poker_5);
                pokers[4] = Poker_5;

                JLabel Poker_6 = new JLabel("New label");
                Poker_6.setBounds(159, 155, 128, 128);
                contentPane.add(Poker_6);
                pokers[5] = Poker_6;

                JLabel Poker_7 = new JLabel("New label");
                Poker_7.setBounds(304, 155, 128, 128);
                contentPane.add(Poker_7);
                pokers[6] = Poker_7;

                JLabel Poker_8 = new JLabel("New label");
                Poker_8.setBounds(453, 155, 128, 128);
                contentPane.add(Poker_8);
                pokers[7] = Poker_8;

                JLabel Poker_9 = new JLabel("New label");
                Poker_9.setBounds(10, 293, 128, 128);
                contentPane.add(Poker_9);
                pokers[8] = Poker_9;

                JLabel Poker_10 = new JLabel("New label");
                Poker_10.setBounds(159, 293, 128, 128);
                contentPane.add(Poker_10);
                pokers[9] = Poker_10;

                JLabel Poker_11 = new JLabel("New label");
                Poker_11.setBounds(304, 293, 128, 128);
                contentPane.add(Poker_11);
                pokers[10] = Poker_11;

                JLabel Poker_12 = new JLabel("New label");
                Poker_12.setBounds(453, 293, 128, 128);
                contentPane.add(Poker_12);
                pokers[11] = Poker_12;

                InitializeImg(pokers);
                rc();
                InitAnsImg(randomnum);
                AddListener();
                JLabel Reset = new JLabel("New label");
                Reset.setIcon(new ImageIcon(Main.class.getResource("/tw/kuas/edu/tw/pic/return.png")));
                Reset.setBounds(603, 418, 33, 33);
                contentPane.add(Reset);
                for (int i = 0; i < 12; i++) {
                        System.out.println(answercard[i]);
                }

        }

        private ImageIcon InitImg() {
                ImageIcon icon = new ImageIcon(Main.class.getResource("pic/0.png"));
                Image img = icon.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST);
                ImageIcon newIcon = new ImageIcon(img);
                return newIcon;
        }

        private void InitializeImg(JLabel[] pokers) {
                for (int i = 0; i < pokers.length; i++) {
                        ImageIcon icon = new ImageIcon(Main.class.getResource("pic/0.png"));
                        Image img = icon.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST);
                        ImageIcon newIcon = new ImageIcon(img);
                        pokers[i].setIcon(newIcon);
                }
        }

        private void rc() {
                for (int i = 0; i < 12; i++) {
                        randomnum[i] = (int) (Math.random() * 12); // 0~11
                        for (int j = 0; j < i; j++) {
                                if (randomnum[i] == randomnum[j]) {
                                        i--;
                                        break;
                                }
                        }
                }
        }

        private void InitAnsImg(int[] randomnum) {

                for (int i = 1; i <= 6; i++) {
                        ImageIcon icon = new ImageIcon(Main.class.getResource(String.format("pic/%d.png", i)));
                        Image img = icon.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST);
                        answercard[randomnum[i - 1]] = new ImageIcon(img); // 0-5
                        answercard[randomnum[i + 5]] = new ImageIcon(img); // 6-11
                }
        }

        private void AddListener() {
                for (int i = 0; i < pokers.length; i++) {
                        pokers[i].addMouseListener(adapter);
                }
        }

        // 給予一個共用的MouseAdapter事件
        ImageIcon temp = null;
        int locate = 0;
        boolean isFirst = true;
        int tempcount = 0;
        MouseAdapter adapter = new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                        for (int i = 0; i < 12; i++) {

                                // 判斷當下使用者點選的牌是哪一張
                                if (isFirst && e.getSource() == pokers[i] ) {
                                        // 翻牌
                                        pokers[i].setIcon(answercard[i]);
                                        if(tempcount == 0) {
                                                // 第一次紀錄當前的圖片以及位置
                                                temp = answercard[i];
                                                locate = i;
                                                System.out.println("第一次翻牌");
                                                tempcount++;
                                        }
                                        else {
                                                System.out.println("第一次翻牌");
                                                // 判斷第二次翻的牌跟第一次有沒有一樣,如果不一樣則需要把第一張牌翻回去
                                                if (temp.getImage() != answercard[i].getImage()) {
                                                        System.out.println("不一樣");
                                                        ImageIcon defaultIcon = new ImageIcon(Main.class.getResource("pic/0.png"));
                                                        pokers[locate].setIcon(defaultIcon);
                                                        temp = answercard[i];
                                                        locate = i;
                                                }
                                                else {
                                                        System.out.println("一樣");
                                                        tempcount = 0;
                                                        isFirst = true;
                                                        break;
                                                }

                                        }
                                        isFirst = false;

                                       
                                        break;
                                }

                                // 第二次翻牌
                                if ((isFirst == false) && e.getSource() == pokers[i]) {
                                        // 翻牌
                                        pokers[i].setIcon(answercard[i]);

                                        System.out.println("第二次翻牌");
                                        // 判斷第二次翻的牌跟第一次有沒有一樣,如果不一樣則需要把第一張牌翻回去
                                        if (temp.getImage() != answercard[i].getImage()) {
                                                System.out.println("不一樣");
                                                ImageIcon defaultIcon = new ImageIcon(Main.class.getResource("pic/0.png"));
                                                pokers[locate].setIcon(defaultIcon);
                                                temp = answercard[i];
                                                locate = i;
                                        }
                                        else {
                                                System.out.println("一樣");
                                                // 清除第一次牌的紀錄
                                                tempcount = 0;
                                        }

                                        isFirst = true;

                                }

                        }

                }
        };

}

TOP

返回列表