import java.applet.*;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JApplet;
public class Test extends JApplet implements Runnable{
Image backpic, rabbit,tortoise,xiaogou,qingwa;
int x1 = 0, y1 = 50;
int x2 = 0, y2 = 150;
int x3=0,y3=250;
int x4=0 ,y4=350;
int rab_road = 0, tor_road = 0,xiao_road=0, qing_road=0;
int rab_time = 0, tor_time = 0,xiao_time=0
,qing_time=0;
String str1 = "rabbit", str2 = "tortoise" ,str3="xiaogou" ,str4="qingwa";
public void init() {
setSize(1000, 500);
backpic = getImage(getCodeBase(), "back.jpg");
rabbit = getImage(getCodeBase(), "rabbit.jpg");
tortoise = getImage(getCodeBase(), "tortoise.jpg");
xiaogou=getImage(getCodeBase(),"xiaogou.jpg");
qingwa=getImage(getCodeBase(),"qingwa.jpg");
}
public void paint(Graphics g) {
g.drawImage(backpic, 0, 0, 1000, 500, this);
g.drawImage(rabbit, x1, y1, 70, 70, this);
g.drawString(str1, x1, y1 + 80);
g.drawImage(tortoise, x2, y2, 70, 70, this);
g.drawString(str2, x2, y2 + 80);
g.drawImage(xiaogou, x3, y3, 70,70,this);
g.drawString(str3, x3,y3+80 );
g.drawImage(qingwa, x4, y4, 70,70,this);
g.drawString(str4, x4,y4+80 );
}
public void start() {
Thread rab = new Thread(this, "rabbit");
Thread tor = new Thread(this, "tortoise");
Thread xiaogou=new Thread(this,"xiaogou");
Thread qingwa=new Thread(this,"qingwa");
rab.start();
tor.start();
xiaogou.start();
qingwa.start();
}
public void run() {
boolean stop = false;
while (!stop) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
String threadName = Thread.currentThread().getName();
if (threadName.equals("rabbit")) {
str1 = "rabbit";
x1 = x1 + 30;
rab_time++;
rab_road += 3;
if (rab_road % 24 == 0) {
str1 = "兔子睡眠";
try {
Thread.sleep(2400);
} catch (InterruptedException ex) {
}
rab_time += 24;
}
if (rab_road == 60) {
stop = true;
str1 = "兔子总用时(秒):" + rab_time;
}
} else if (threadName.equals("tortoise")) {
x2 += 10;
tor_road += 1;
tor_time++;
if (tor_road == 60) {
stop = true;
str2 = "乌龟总用时(秒 ):" + tor_time;
}
} else if(threadName.equals("xiaogou"))
{
str3="xiaogou";
x3=x3+60;
xiao_time++;
xiao_road+=6;
if(xiao_road%30==0)
{
str3="小狗睡眠";
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
xiao_time+=24;
}
if (xiao_road == 60) {
stop = true;
str3 = "小狗总用时(秒):" + xiao_time;
}
}
else if(threadName.equals("qingwa"))
{
str4="qingwa";
x4=x4+30;
qing_time++;
qing_road+=3;
if(qing_road%21==0)
{
str4="青蛙睡眠";
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
qing_time+=28;
}
if (qing_road == 60) {
stop = true;
str4 = "青蛙总用时(秒):" + qing_time;
}
}
repaint();
}
}
}
- 1
- 2
前往页