请教大神 如何让兔子和乌龟一起动
现在只有乌龟自己跑 兔子不跑代码运行没有错误
package 实验6;
import javax.swing.*;
import *;
import java.awt.*;
import *;
class Animals extends JPanel implements Runnable{
Image backpic, rabbit, tortoise;
int x1=0,y1=20,x2=0,y2=150;
int x=0;
int rabbitlength = 0;
int tortoiselength=0;
int rabbittime=0;
int tortoisetime = 0;
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0,0,500,300);
rabbit = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/rabbit.jpg"));
tortoise = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/tortoise.jpg"));
g.drawImage(rabbit, x1, y1, 30, 100, this);
g.drawImage(tortoise,x2,y2 ,30,100, this);
}
public void run() {
boolean flag = true;
while(flag) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
String name = Thread.currentThread().getName();
if(name.equals("兔子")) {
rabbitlength= rabbitlength+10;
rabbittime = rabbittime+5;
x1+=10;
if(rabbitlength%80==0) {
try {
Thread.sleep(2000);
}catch(Exception e) {
e.printStackTrace();
}
System.out.println("兔子休息");
rabbittime = rabbittime+20;
}
System.out.println(name+"已经跑了"+rabbitlength+"米");
if(rabbitlength==300) {
System.out.println("兔子到达了终点,用时为:"+rabbittime+"秒");
flag = false;
}
repaint();
}
if(name.equals("乌龟")) {
tortoiselength=tortoiselength+5;
tortoisetime = tortoisetime+3;
x2+=5;
System.out.println(name+"已经跑了"+tortoiselength+"米");
if(tortoiselength==300) {
System.out.println("乌龟到达了终点,用时为:"+tortoisetime+"秒");
flag = false ;
}
repaint();
}
}
}
}
public class RabbitTortoiseRace {
public static void main(String args[]) {
Animals rabbit = new Animals();
Animals tortoise= new Animals();
new Thread(rabbit,"兔子").start();
new Thread(tortoise,"乌龟").start();
JFrame f = new JFrame("龟兔赛跑游戏");
f.add(rabbit);
f.add(tortoise);
f.setSize(400, 300);
f.setVisible(true);
}
}