package com;
import javax.swing.*;
public class StartButton extends JButton implements Runnable {
Dice[] dices;
JLabel singleText;
JTextArea totalText;
static int index;
Customer customer;
JLabel resultText;
int wonCount;
public StartButton(ImageIcon imageIcon, Dice[] dices, JLabel singleText, JTextArea totalText, JLabel resultText) {
super(imageIcon);
this.singleText = singleText;
this.totalText = totalText;
this.dices = dices;
this.resultText = resultText;
}
@Override
public synchronized void run() {
String pName = this.getName();
String image1 = "img/" + this.getName() + "1.png";
String image2 = "img/" + this.getName() + "2.png";
ImageIcon imageIcon1 = new ImageIcon(image1);
ImageIcon imageIcon2 = new ImageIcon(image2);
this.setIcon(imageIcon2);
startDicesThread();
printOne(pName);
totalPrint();
this.setIcon(imageIcon1);
}
private void startDicesThread() {
Thread[] threads = new Thread[3];
for (int i = 0; i < 3; i++) {
threads[i] = new Thread(dices[i]);
threads[i].start();
}
try {
for (int i = 0; i < 3; i++) {
threads[i].join();
}
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
private void totalPrint() {
totalText.append(customer.toString());
totalText.append("\r\n");
resultText.setBounds(10,index * 20 +30, 100, 100);
resultText.setText("" +
"<html>" +
"<font size=6> " +
"<body>" +
"玩了" + index + "次" + " 赢了:" + wonCount +"次" + "成功率是:" + String.format("%2.2f", (wonCount * 1.0)/index * 100) + "%" +
" </body>" +
"</font>" +
"</html>");
}
private void printOne(String pName) {
String guess;
if (pName.equals("b")) guess = "big";
else guess = "small";
int dice1 = dices[0].getNum();
int dice2 = dices[1].getNum();
int dice3 = dices[2].getNum();
int sum = dice1 + dice2 + dice3;
String dicesResult;
if (sum > 9) {
dicesResult = "big";
} else {
dicesResult = "small";
}
String result = (guess == dicesResult ? "won" : "lose");
if (result == "won") wonCount ++;
customer = new Customer(index++, guess, dice1 + " " + dice2 + " " +dice3, sum, dicesResult, result);
singleText.setText("" +
"<html>" +
"<font size=6> " +
"<body>" +
"Three Dice:" + dice1 + " " + dice2 + " " + dice3 + "<br/>" +
"The sum of three dice:" + String.format("% 2d", sum) + " " + dicesResult + "<br/>" +
"--------------------" + "<br/>" +
"you guess:" + guess + "<br/>" + "The dice result:" + dicesResult + "<br/>" +
"The bureau results:" + result + "<br/>" +
"</body>" +
"</font>" +
"</html>");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java作业。简单的摇骰子游戏、使用线程和swing、awt.zip (66个子文件)
dicegame-master
game.iml 423B
.gitattributes 66B
src
img
s2.png 77KB
b1.png 83KB
b2.png 75KB
t2.png 85KB
bj2.jpg 251KB
t4.png 88KB
t3.png 91KB
t5.png 82KB
t1.png 93KB
bj.jpg 847KB
t6.png 88KB
s1.png 80KB
bj1.jpg 151KB
META-INF
MANIFEST.MF 49B
com
Dice.java 869B
StartPanel.java 3KB
GameFrame.java 3KB
TotalPrintPanel.java 1KB
Customer.java 765B
StartButton.java 3KB
SinglePrintPanel.java 1KB
DicesPanel.java 937B
out
artifacts
game_jar
game.jar 2.01MB
production
game
img
s2.png 77KB
b1.png 83KB
b2.png 75KB
t2.png 85KB
bj2.jpg 251KB
t4.png 88KB
t3.png 91KB
t5.png 82KB
t1.png 93KB
bj.jpg 847KB
t6.png 88KB
s1.png 80KB
bj1.jpg 151KB
META-INF
MANIFEST.MF 49B
game.kotlin_module 16B
com
StartPanel.class 3KB
DicesPanel.class 1KB
SinglePrintPanel.class 1KB
GameFrame.class 4KB
StartButton.class 4KB
Customer.class 1KB
Dice.class 1KB
TotalPrintPanel.class 2KB
.idea
uiDesigner.xml 9KB
artifacts
game_jar.xml 270B
misc.xml 361B
modules.xml 248B
.gitignore 176B
img
s2.png 77KB
b1.png 83KB
b2.png 75KB
t2.png 85KB
bj2.jpg 251KB
t4.png 88KB
t3.png 91KB
t5.png 82KB
t1.png 93KB
bj.jpg 847KB
t6.png 88KB
s1.png 80KB
bj1.jpg 151KB
共 66 条
- 1
资源评论
马coder
- 粉丝: 1208
- 资源: 6602
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功