標題:
翻牌遊戲 (四)
[打印本頁]
作者:
周政輝
時間:
2018-5-5 11:05
標題:
翻牌遊戲 (四)
本帖最後由 周政輝 於 2018-5-5 11:18 編輯
延續 翻牌遊戲 (三) 的練習,使完成全部配對時顯示 "Good Job!" 圖樣,參考執行畫面如下:
[attach]4005[/attach]
[attach]4006[/attach]
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];
JLabel GoodJob;
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);
GoodJob = new JLabel("");
GoodJob.setBounds(0, 0, 636, 451);
contentPane.add(GoodJob);
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;
int pair = 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;
pair++;
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("一樣");
// 清除第一次牌的紀錄
pair++;
tempcount = 0;
}
isFirst = true;
}
}
if(pair == 6){
GoodJob.setIcon(new ImageIcon(Main.class.getResource("/tw/kuas/edu/tw/pic/goodjob.png")));
}
}
};
}
複製代碼
作者:
張健勳
時間:
2018-5-5 11:36
package bbs.istak.org.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 Game extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game frame = new Game();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
JLabel[] pokers = new JLabel[12];
ImageIcon[] randomc = new ImageIcon[12];
ImageIcon[] answerc = new ImageIcon[12];
int randomnum[] = new int[12];
public Game() {
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("");
Poker_1.setBounds(10, 10, 128, 128);
contentPane.add(Poker_1);
pokers[0] = Poker_1;
JLabel Poker_2 = new JLabel("");
Poker_2.setBounds(159, 10, 128, 128);
contentPane.add(Poker_2);
pokers[1] = Poker_2;
JLabel Poker_3 = new JLabel("");
Poker_3.setBounds(304, 10, 128, 128);
contentPane.add(Poker_3);
pokers[2] = Poker_3;
JLabel Poker_4 = new JLabel("");
Poker_4.setBounds(453, 10, 128, 128);
contentPane.add(Poker_4);
pokers[3] = Poker_4;
JLabel Poker_5 = new JLabel("");
Poker_5.setBounds(10, 155, 128, 128);
contentPane.add(Poker_5);
pokers[4] = Poker_5;
JLabel Poker_6 = new JLabel("");
Poker_6.setBounds(159, 155, 128, 128);
contentPane.add(Poker_6);
pokers[5] = Poker_6;
JLabel Poker_7 = new JLabel("");
Poker_7.setBounds(304, 155, 128, 128);
contentPane.add(Poker_7);
pokers[6] = Poker_7;
JLabel Poker_8 = new JLabel("");
Poker_8.setBounds(453, 155, 128, 128);
contentPane.add(Poker_8);
pokers[7] = Poker_8;
JLabel Poker_9 = new JLabel("");
Poker_9.setBounds(10, 293, 128, 128);
contentPane.add(Poker_9);
pokers[8] = Poker_9;
JLabel Poker_10 = new JLabel("");
Poker_10.setBounds(159, 293, 128, 128);
contentPane.add(Poker_10);
pokers[9] = Poker_10;
JLabel Poker_11 = new JLabel("");
Poker_11.setBounds(304, 293, 128, 128);
contentPane.add(Poker_11);
pokers[10] = Poker_11;
JLabel Poker_12 = new JLabel("");
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("");
Reset.setIcon(new ImageIcon(Game.class.getResource("pic/R.png")));
Reset.setBounds(603, 418, 33, 33);
contentPane.add(Reset);
for (int i = 0; i < 12; i++) {
System.out.println(answerc[i]);
}
}
private ImageIcon InitImg() {
ImageIcon icon = new ImageIcon(Game.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(Game.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(Game.class.getResource(String
.format("pic/%d.png", i)));
Image img = icon.getImage().getScaledInstance(128, 128,
java.awt.Image.SCALE_FAST);
answerc[randomnum[i - 1]] = new ImageIcon(img); // 0-5
answerc[randomnum[i + 5]] = new ImageIcon(img); // 6-11
}
}
private void AddListener() {
for (int i = 0; i < pokers.length; i++) {
pokers[i].addMouseListener(adapter);
}
}
ImageIcon tmp = null;
int locate = 0;
boolean iF = true;
int tc = 0;
MouseAdapter adapter = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
for (int i = 0; i < 12; i++) {
if (iF && e.getSource() == pokers[i]) {
pokers[i].setIcon(answerc[i]);
if (tc == 0) {
tmp = answerc[i];
locate = i;
System.out.println("第一次翻牌");
tc++;
} else {
System.out.println("第一次翻牌");
if (tmp.getImage() != answerc[i].getImage()) {
System.out.println("不同");
ImageIcon defaultIcon = new ImageIcon(
Game.class.getResource("pic/0.png"));
pokers[locate].setIcon(defaultIcon);
tmp = answerc[i];
locate = i;
} else {
System.out.println("一樣");
tc = 0;
iF = true;
break;
}
}
iF = false;
break;
}
if ((iF == false) && e.getSource() == pokers[i]) {
pokers[i].setIcon(answerc[i]);
System.out.println("第二次翻牌");
if (tmp.getImage() != un[i].getImage()) {
System.out.println("不同");
ImageIcon defaultIcon = new ImageIcon(
Game.class.getResource("pic/0.png"));
pokers[locate].setIcon(defaultIcon);
tmp = answerc[i];
locate = i;
} else {
System.out.println("一樣");
tc = 0;
}
iF = true;
}
}
}
};
}
複製代碼
作者:
蔡庭豪
時間:
2018-5-5 12:04
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;
}
複製代碼
作者:
林侑成
時間:
2018-5-10 19:04
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];
JLabel GoodJob;
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);
GoodJob = new JLabel("");
GoodJob.setBounds(0, 0, 636, 451);
contentPane.add(GoodJob);
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]);
}
}
複製代碼
作者:
林侑成
時間:
2018-5-10 19:05
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);
}
}
ImageIcon temp = null;
int locate = 0;
boolean isFirst = true;
int tempcount = 0;
int pair = 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;
pair++;
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("一樣");
pair++;
tempcount = 0;
}
isFirst = true;
}
}
if(pair == 6){
GoodJob.setIcon(new ImageIcon(Main.class.getResource("/tw/kuas/edu/tw/pic/goodjob.png")));
}
}
};
}
複製代碼
作者:
黃茂勛
時間:
2018-5-23 15:30
package bbs.istak.org.tw;
import java.awt.Desktop.Action;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
private JFrame fm;
private JButton btn[]=new JButton[12];
private JButton btn_r;
private ImageIcon ii[]=new ImageIcon[12];
private ImageIcon ic,ii_r,ii_b,ii_gj;
private int rc[]=new int[12];
private ImageIcon test; //測試對象
private int tmp; //上一張牌
private JLabel lb;
private int done=0;
Main()
{
rc();
ic=new ImageIcon(Main.class.getResource("star.png"));
ii_b=new ImageIcon(Main.class.getResource("0.png"));
ii_r=new ImageIcon(Main.class.getResource("return.png"));
ii_gj=new ImageIcon(Main.class.getResource("goodjob.png"));
for(int i=0; i<6; i++)
{
ii[i]=new ImageIcon(Main.class.getResource((i+1)+".png"));
ii[i+6]=new ImageIcon(Main.class.getResource((i+1)+".png"));
}
for(int i=0; i<12; i++)
btn[i]=new JButton(ii_b);
for(int i=0; i<4; i++)
btn[i].setBounds(i*130+12, 10, 128, 128);
for(int i=0; i<4; i++)
btn[i+4].setBounds(i*130+12, 140, 128, 128);
for(int i=0; i<4; i++)
btn[i+8].setBounds(i*130+12, 270, 128, 128);
for(int i=0; i<12; i++)
btn[i].addMouseListener(ma);
btn_r=new JButton(ii_r);
btn_r.setBounds(497, 407, 33, 33);
btn_r.addActionListener(al);
lb=new JLabel(ii_gj);
lb.setVisible(false);
lb.setBounds(0, 0, 636, 451);
lb.addMouseListener(ma);
fm=new JFrame("翻牌遊戲");
fm.setBounds(100, 100, 548, 480);
fm.setIconImage(ic.getImage());
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
for(int i=0; i<12; i++)
fm.add(btn[i]);
fm.add(btn_r);
}
void rc()
{
test=null; //清空測試對象
for(int i=0; i<12; i++)
{
rc[i]=(int)(Math.random()*12); //0~11
for(int j=0; j<i; j++)
{
if(rc[i]==rc[j])
{
i--;
break;
}
}
}
}
MouseAdapter ma=new MouseAdapter() {
public void mousePressed(MouseEvent e)
{
for(int i=0; i<12; i++)
{
if(e.getSource()==btn[i] && btn[i].isEnabled())
{
for(int j=0; j<12; j++)
{
if(btn[j].isEnabled())
btn[j].setIcon(ii_b); //將其他翻回
}
btn[i].setIcon(ii[rc[i]]); //將點擊的 翻到正面
if(test==ii[rc[i]]) //1.點擊第一次略過(test=null) //4.判斷下一次點擊的 是否為受試對象
{
btn[i].setEnabled(false);
btn[tmp].setEnabled(false);
btn[tmp].setIcon(ii[rc[tmp]]);
done++;
}
if(rc[i]<6)
test=ii[rc[i]+6]; //2.取得受試對象
else
test=ii[rc[i]-6];
tmp=i; //3.取得前一次點擊者
}
}
}
public void mouseReleased(MouseEvent e)
{
if(done==6)
lb.setVisible(true);
}
};
ActionListener al=new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn_r)
{
rc();
for(int i=0; i<12; i++)
{
btn[i].setIcon(ii_b);
btn[i].setEnabled(true);
}
}
}
};
public static void main(String[] args) {
new Main();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2