- 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;
- 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];
- boolean isFirst=true;
- 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);
- answercard[randomnum[i+5]] = new ImageIcon(img);
- }
- }
-
- private void AddListener() {
- for(int i=0;i<pokers.length;i++) {
- pokers[i].addMouseListener(adapter);
- }
- }
- ImageIcon temp;
- int locate=0;
- MouseAdapter adapter=new MouseAdapter(){
- @Override
- public void mouseClicked(MouseEvent e)
- {
- for(int i=0; i<12; i++)
- {
- if(e.getSource()==pokers[i] && isFirst == true) {
- temp = answercard[i];
- pokers[i].setIcon(answercard[i]);
- isFirst = false;
- locate = i;
- break;
- }
- else if(e.getSource()==pokers[i] && isFirst == false) {
- pokers[i].setIcon(answercard[i]);
- if(temp == pokers[i].getIcon())
- {
- 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);
- pokers[locate].setIcon(newIcon);
- }
- isFirst = true;
- locate = i;
- break;
- }
-
- }
-
-
- }
-
- };
- }
複製代碼 |