package System.web3.IO.UI;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SaiPao extends JFrame {
private JButton start,stop;
private JLabel imgT,imgW, time;
private int X;
Rectangle tz,wg;
public SaiPao() throws HeadlessException {
//添加启动按钮
setLayout(null);
this.setTitle("龟兔赛跑");
start=new JButton("开始");
stop=new JButton("结束");
add(start);
add(stop);
start.setBounds(250,350,100,50);
stop.setBounds(350,350,100,50);
start.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
new Thread(new Time()).start();
}
});
stop.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
);
//秒数跳动
time=new JLabel("<html><body><font style=font-size:150; color=red>"+3+"</font></body></html>");
add(time);
time.setBounds(300, 130, 150, 150);
//添加图片
imgT=new JLabel(new ImageIcon("imgs/tuzi.png"));
imgW = new JLabel(new ImageIcon("imgs/wugui.png"));
add(imgT);
add(imgW);
imgT.setBounds(0,100,100, 200);
imgW.setBounds(0,200,100,200);
JLabel lab=new JLabel("<html><body><font size=20 color=red>龟兔赛跑</font></body></html>");
add(lab);
lab.setBounds(300,0,400,100);
JLabel lab2=new JLabel("<html><body><font style=font-size:50; color=red>起点</font></body></html>");
add(lab2);
lab2.setBounds(80, 170, 100, 150);
JLabel lab3=new JLabel("<html><body><font style=font-size:50; color=red>终点</font></body></html>");
add(lab3);
lab3.setBounds(700, 170, 100, 150);
this.setSize(810,450);
this.setVisible(true);
this.setLocation(300, 100);
this.setResizable(false);
Rectangle rec;
rec=this.getBounds();
X=rec.width;
tz=imgT.getBounds();
wg=imgW.getBounds();
}
//比赛开始
class Time implements Runnable
{
private int timeout=3;
public void run() {
imgT.setBounds(0,100,100, 200);
imgW.setBounds(0,200,100,200);
while(timeout!=-1)
{
try {
Thread.sleep(1000);
--timeout;
time.setText("<html><body><font style=font-size:150; color=red>"+timeout+"</font></body></html>");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
time.setText("");
new Thread( new Tuzi()).start();
new Thread(new WuGui()).start();
}
}
//兔子线程
class Tuzi implements Runnable
{
private int sudu;//移动速度
private boolean stop=true;
public void run() {
while(stop)
{
sudu=(int)(Math.random()*8);
tz=imgT.getBounds();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
if((tz.x+100)>X)
{
wg.x=X;
time.setText("<html><body><font style=font-size:20; color=red>乌龟胜利了!兔子加油!</font></body></html>");
stop=false;
}else
{
imgT.setLocation(tz.x+sudu, 100);
}
}
}
}
//乌龟线程
class WuGui implements Runnable
{
private int sudu;//移动速度
private boolean stop=true;
public void run() {
while(stop)
{
sudu=(int)(Math.random()*8);
wg=imgW.getBounds();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if((wg.x+100)>X)
{
tz.x=X;
time.setText("<html><body><font style=font-size:20; color=red>兔子胜利了!乌龟加油!</font></body></html>");
stop=false;
}else
{
imgW.setLocation(wg.x+sudu, 200);
}
}
}
}
}