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
版权申诉
74 浏览量
2023-07-19
09:39:50
上传
评论
收藏 8.05MB ZIP 举报
白话机器学习
- 粉丝: 9242
- 资源: 7681
最新资源
- JSP基于WEB网上论坛设计与实现(源代码+lw+开题报告+答辩PPT+外文翻译).zip
- JSP基于WEB的图书馆借阅系统的设计与实现(源代码+lw).zip
- 数电实验三:74LS151逻辑功能测试、74LS153逻辑功能测试、74LS153全加器、三输入多数表决电路
- 基于python知识图谱抖音舆情数据可视化分析系统python + flask web + mysql + 知识图谱
- jsp基于Web的可维护的数据库浏览器(源代码+lw+答辩PPT).zip
- jsp基于WEB的考务管理系统的设计与实现(源代码+lw).zip
- jsp基于WEB操作系统课程教学网站的设计与实现(源代码+lw).zip
- 数据库课程实践在IDEA上使用java语言采用JDBC技术连接Mysql数据库,实现购物系统.zip
- JSP基于SSH2的网络在线问答系统.zip
- JSP基于Iptables图形管理工具的设计与实现(源代码+lw).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈