- package bbs.istak.org.tw;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.awt.Image;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- public class PtSC extends JFrame {
- private JPanel contentPane;
- private static ImageIcon icon;
- private ImageIcon poker[] = new ImageIcon[12];
- private int num[] = new int[12];
- private ImageIcon answer[] = new ImageIcon[12];
- private JLabel larray[] = new JLabel[12];
- private JLabel gdLabel;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- PtSC frame = new PtSC();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public PtSC() {
- icon = new ImageIcon(PtSC.class.getResource("star.png"));
- Image imgicon = icon.getImage().getScaledInstance(128, 128,
- java.awt.Image.SCALE_FAST);
- this.setIconImage(imgicon);
- larray = new JLabel[12];
- poker = new ImageIcon[12];
- answer = new ImageIcon[12];
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 620, 462);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- JLabel label_0 = new JLabel(pokercard(0));
- label_0.setBounds(10, 10, 128, 128);
- contentPane.add(label_0);
- larray[0] = label_0;
- JLabel label_1 = new JLabel(pokercard(1));
- label_1.setBounds(148, 10, 128, 128);
- contentPane.add(label_1);
- larray[1] = label_1;
- JLabel label_2 = new JLabel(pokercard(2));
- label_2.setBounds(286, 10, 128, 128);
- contentPane.add(label_2);
- larray[2] = label_2;
- JLabel label_3 = new JLabel(pokercard(3));
- label_3.setBounds(424, 10, 128, 128);
- contentPane.add(label_3);
- larray[3] = label_3;
- JLabel label_4 = new JLabel(pokercard(4));
- label_4.setBounds(10, 149, 128, 128);
- contentPane.add(label_4);
- larray[4] = label_4;
- JLabel label_5 = new JLabel(pokercard(5));
- label_5.setBounds(148, 149, 128, 128);
- contentPane.add(label_5);
- larray[5] = label_5;
- JLabel label_6 = new JLabel(pokercard(6));
- label_6.setBounds(286, 149, 128, 128);
- contentPane.add(label_6);
- larray[6] = label_6;
- JLabel label_7 = new JLabel(pokercard(7));
- label_7.setBounds(424, 149, 128, 128);
- contentPane.add(label_7);
- larray[7] = label_7;
- JLabel label_8 = new JLabel(pokercard(8));
- label_8.setBounds(10, 286, 128, 128);
- contentPane.add(label_8);
- larray[8] = label_8;
- JLabel label_9 = new JLabel(pokercard(9));
- label_9.setBounds(148, 286, 128, 128);
- contentPane.add(label_9);
- larray[9] = label_9;
- JLabel label_10 = new JLabel(pokercard(10));
- label_10.setBounds(286, 286, 128, 128);
- contentPane.add(label_10);
- larray[10] = label_10;
- JLabel label_11 = new JLabel(pokercard(11));
- label_11.setBounds(424, 286, 128, 128);
- contentPane.add(label_11);
- larray[11] = label_11;
- icon = new ImageIcon(PtSC.class.getResource("return.png"));
- Image bimg = icon.getImage().getScaledInstance(33, 33,
- java.awt.Image.SCALE_FAST);
- ImageIcon back = new ImageIcon(bimg);
- JLabel labelreset = new JLabel(back);
- labelreset.setBounds(562, 10, 33, 33);
- contentPane.add(labelreset);
- textField = new JTextField();
- textField.setBounds(558, 393, 36, 21);
- contentPane.add(textField);
- textField.setColumns(10);
- textField.setText("1");
-
-
- num = new int[12];
- for (int i = 0; i < num.length; i++) {
- num[i] = (int) (Math.random() * 12);
- for (int j = 0; j < i; j++) {
- if (num[i] == num[j]) {
- i--;
- break;
- }
- }
- }
- for (int k = 0; k < num.length; k++) {
- System.out.println(num[k]);
- }
- backcard(num);
- AddListener();
- gdLabel = new JLabel();
- gdLabel.setBounds(189, 78, 256, 256);
- contentPane.add(gdLabel);
-
- }
- private ImageIcon pokercard(int n) {
- poker = new ImageIcon[12];
- icon = new ImageIcon(PtSC.class.getResource("0.png"));
- Image andimg = icon.getImage().getScaledInstance(128, 128,
- java.awt.Image.SCALE_FAST);
- ImageIcon andriod = new ImageIcon(andimg);
- for (int a = 0; a < poker.length; a++) {
- poker[a] = andriod;
- }
- return poker[n];
- }
- private void backcard(int[] num) {
- for (int x = 1; x <= 6; x++) {
- icon = new ImageIcon(PtSC.class.getResource(String.format("%d.png",
- x)));
- Image img = icon.getImage().getScaledInstance(128, 128,
- java.awt.Image.SCALE_FAST);
- ImageIcon answercard = new ImageIcon(img);
- answer[num[x - 1]] = answercard;
- answer[num[x + 5]] = answercard;
- }
- }
- private void AddListener() {
- for (int i = 0; i < larray.length; i++)
- larray[i].addMouseListener(adapter);
- }
- ImageIcon temp = null;
- int locate = 0;
- boolean isFirst = true;
- int tempcount = 0;
- int rcc = 0;
- MouseAdapter adapter = new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- for (int i = 0; i < 12; i++) {
- // 判斷當下使用者點選的牌是哪一張
- if (isFirst && e.getSource() == larray[i]) {
- // 翻牌
- larray[i].setIcon(answer[i]);
- if (tempcount == 0) {
- // 第一次紀錄當前的圖片以及位置
- temp = answer[i];
- locate = i;
- textField.setText("1");
- tempcount++;
- } else {
- textField.setText("1");
- // 判斷第二次翻的牌跟第一次有沒有一樣,如果不一樣則需要把第一張牌翻回去
- if (temp.getImage() != answer[i].getImage()) {
- System.out.println("不一樣");
- ImageIcon defaultIcon = new ImageIcon(
- PtSC.class.getResource("0.png"));
- larray[locate].setIcon(defaultIcon);
- temp = answer[i];
- locate = i;
- } else {
- System.out.println("一樣");
- tempcount = 0;
- isFirst = true;
- break;
- }
- }
- isFirst = false;
- break;
- }
- // 第二次翻牌
- if ((isFirst == false) && e.getSource() == larray[i]) {
- // 翻牌
- larray[i].setIcon(answer[i]);
- textField.setText("2");
- // 判斷第二次翻的牌跟第一次有沒有一樣,如果不一樣則需要把第一張牌翻回去
- if (temp.getImage() != answer[i].getImage()) {
- System.out.println("不一樣");
- ImageIcon defaultIcon = new ImageIcon(
- PtSC.class.getResource("0.png"));
- larray[locate].setIcon(defaultIcon);
- temp = answer[i];
- locate = i;
- } else {
- System.out.println("一樣");
- // 清除第一次牌的紀錄
- tempcount = 0;
- rcc++;
- }
- isFirst = true;
- }
-
- }
- if(rcc==5){
- icon = new ImageIcon(PtSC.class.getResource("goodjob.png"));
- Image gimg = icon.getImage().getScaledInstance(256, 256,java.awt.Image.SCALE_FAST);
- ImageIcon goodjob = new ImageIcon(gimg);
- gdLabel.setIcon(goodjob);
- }
- }
-
-
- };
- private JTextField textField;
- }
複製代碼 |