- 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;
- 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];
- /**
- * 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);
-
-
- 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();
-
- }
- 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 temp1;
- int location;
- boolean first = true;
- MouseAdapter adapter=new MouseAdapter(){
- @Override
- public void mouseClicked(MouseEvent e)
- {
-
- for(int i=0; i<12; i++)
- {
-
- if(e.getSource()==larray[i] && first == true) {
-
- larray[i].setIcon(answer[i]);
- temp1 = answer[i];
- location = i;
- first = false;
- System.out.println("first");
- break;
- }
- else if (e.getSource()==larray[i] && first == false){
- System.out.println("second");
- if(temp1 != larray[i].getIcon()){
-
- larray[i].setIcon(poker[i]);larray[location].setIcon(poker[location]);
-
- }
-
- first = true;
- break;
-
- }
-
- }
- }
- };
-
- }
複製代碼 |