package aa.bb;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class RunGame extends JFrame {
public MyJpanel myPanel;
public Mythread[] myThreads;
public JPanel panel1, panel2;
public JRadioButton[] radioButtons;
public JRadioButton radioButton;
public JTextArea textArea;
public JScrollPane scro;
public JButton button;
public String[] strs1, strs2, strs3, strs4, strs5;
public String str;
public ButtonGroup buttongroup;
public int i, sum, ranking;
public Boolean flag;
public Image image;
public Image[] imgs1,imgs2;
public JLabel label;
public JComboBox comboBox;
public int width,height;
public RunGame() {
super("多线程赛跑");
panel1 = new JPanel();
panel2 = new JPanel();
strs1 = new String[] { "images/1.gif", "images/2.gif", "images/4.gif",
"images/3.gif" };
strs2 = new String[] { "1", "2", "3", "4" };
strs3 = new String[] { "张三", "李四", "王五", "赵六" };
strs4 = new String[] { "100", "200", "300", "500", "1000" };
strs5 = new String[] { "images/11.gif", "images/21.gif",
"images/41.gif", "images/31.gif", "images/13.gif",
"images/23.gif", "images/43.gif", "images/33.gif",
"images/12.gif", "images/22.gif", "images/42.gif",
"images/32.gif" };
buttongroup = new ButtonGroup();
radioButtons = new JRadioButton[4];
button = new JButton("Start");
textArea = new JTextArea(5, 10);
scro = new JScrollPane(textArea);
flag = true;
label = new JLabel();
sum = 10000;
ranking = 0;
width=80;height=90;
comboBox = new JComboBox();
myThreads = new Mythread[4];
myPanel = new MyJpanel();
imgs1 = new Image[4];
imgs2 = new Image[4];
comboBox.setBorder(BorderFactory.createTitledBorder("Select"));
label.setText("Money:" + sum);
panel1.setBackground(Color.PINK);
for (i = 0; i < 4; i++) {
radioButton = new JRadioButton();
radioButton.setText(strs3[i]);
radioButtons[i] = radioButton;
panel2.add(radioButtons[i]);
buttongroup.add(radioButtons[i]);
}
for (i = 0; i < 5; i++) {
comboBox.addItem(strs4[i]);
}
for (i = 0; i < 4; i++) {
myThreads[i] = new Mythread(Toolkit.getDefaultToolkit()
.createImage(strs5[i]), strs5[i + 4], strs3[i]);
}
for (i = 0; i < 4; i++) {
image = Toolkit.getDefaultToolkit().createImage(strs1[i]);
imgs1[i] = image;
image = Toolkit.getDefaultToolkit().createImage(strs5[i+8]);
imgs2[i] = image;
}
panel2.setOpaque(false);
panel2.setBorder(BorderFactory.createTitledBorder("选择区"));
panel1.add(panel2, BorderLayout.WEST);
panel1.add(button);
button.addActionListener(new MyListener());
panel1.add(scro);
panel1.add(label);
panel1.add(comboBox);
myPanel.setBorder(BorderFactory.createTitledBorder("赛马区"));
myPanel.setBackground(Color.WHITE);
this.add(myPanel);
this.add(panel1, "South");
this.setLocation(100, 100);
this.setSize(700, 500);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new RunGame();
}
class MyListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
switch (e.getActionCommand()) {
// 先判断button
case "Start":
str = (String) comboBox.getSelectedItem(); // 获得赌注
int number1 = 0;
width=0;
height=0;
try {
int x = Integer.valueOf(str);
for (i = 0; i < 4; i++) { // 如果没有选择人物,请选择
if (radioButtons[i].isSelected()) {
number1 += 1;
}
}
if (number1 == 0) {
JOptionPane.showMessageDialog(null, "请选择人物 ");
} else {
for (Mythread thread : myThreads) { // 线程开始
thread.start();
}
textArea.setText("比赛开始\n");
flag = true;
button.setEnabled(false);
button.setText("Restart");
button.setEnabled(false);
break;
}
} catch (Exception e2) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "输入格式不正确");
}
case "Restart":
str = (String) comboBox.getSelectedItem();
ranking = 0;
int number2 = 0;
width=0;height=0;
flag = true;
try {
int x = Integer.valueOf(str);
for (i = 0; i < 4; i++) {
if (radioButtons[i].isSelected()) {
number2 += 1;
}
}
if (number2 == 0) {
JOptionPane.showMessageDialog(null, "请选择人物 ");
} else if (sum == 0) {
JOptionPane.showMessageDialog(null, "您的余额为0,系统赠送给您500");
sum = 500;
} else if (x > sum) {
JOptionPane.showMessageDialog(null, "金额不足");
} else {
for (i = 0; i < 4; i++) { // 需要重新定义线程
myThreads[i] = new Mythread(Toolkit
.getDefaultToolkit().createImage(strs5[i]),
strs5[i + 4], strs3[i]);
}
for (Mythread thread : myThreads) {
thread.start();
}
textArea.setText("比赛开始\n");
button.setEnabled(false);
flag = true;
}
} catch (Exception e2) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "输入格式不正确");
}
}
}
}
class MyJpanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
for (i = 0; i < myThreads.length; i++) {
// 画背景
g.drawImage(imgs1[i], 5, 10 + i * 90, 680, 90, this);
// 画跑步图片
g.drawImage(myThreads[i].image1, myThreads[i].starX,
20 + i * 90,80-width,90-height, this);
g.drawImage(imgs2[i], 5, 10+i*90, width, height, this);
}
g.setColor(Color.YELLOW);
g.drawLine(650, 10, 650, 370);
}
}
class Mythread extends Thread {
Image image1;
Random ran = new Random();
int starX;
String strs;
// 线程构造器,每一个线程有一张图片,有一个字符串,有一个名字
public Mythread(Image image1, String strs, String str) {
this.image1 = image1;
this.strs = strs;
this.setName(str);
}
@Override
public void run() {
// TODO Auto-generated method stub
while (starX < 650 && flag) {
starX += ran.nextInt(15);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
ranking++;
button.setEnabled(true);
textArea.append("第" + ranking + "名是:"
+ Thread.currentThread().getName());
textArea.append("\n");
Match(Thread.currentThread().getName(), ranking);
if (ranking == 1) {
Show(this.strs);
}
try {
Thread.sleep(2000);
width=80;height=90;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 判断猜的是否正确
public void Match(String str, int ranking) {
if (flag) {
if (ranking == 1) {
// 把下面show里面的程序放在这里完全可以
for (i = 0; i < 4; i++) {
if (radioButtons[i].isSelected()
&& radioButtons[i].getText().equals(strs)) {
textArea.append("恭喜您猜对了!!!\n");
str = (String) comboBox.getSelectedItem();
sum = sum + Integer.valueOf(str);
label.setText("Money" + sum);