import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JApplet;

/**
*    
* @author 万春丽,赵学庆 java2000.net
*
*/

public class T extends JApplet implements Runnable {
    Image backpic, rabbit, tortoise;
     int x1 = 0, y1 = 0;
     int x2 = 0, y2 = 100;
     int rab_road = 0, tor_road = 0;
     int rab_time = 0, tor_time = 0;
    String str1 = "rabbit", str2 = "tortoise";

     public void init() {
        setSize(700, 200);
        backpic = getImage(getCodeBase(), "back.gif");
        rabbit = getImage(getCodeBase(), "rabbit.jpg");
        tortoise = getImage(getCodeBase(), "tortoise.jpg");
    }

     public void paint(Graphics g) {

        g.drawImage(backpic, 0, 0, 700, 200, this);
        g.drawImage(rabbit, x1, y1, 60, 60, this);
        g.drawString(str1, x1, y1 + 80);
        g.drawImage(tortoise, x2, y2, 60, 60, this);
        g.drawString(str2, x2, y2 + 80);
    }

     public void start() {
        Thread rab = new Thread( this, "rabbit");
        Thread tor = new Thread( this, "tortoise");
        rab.start();
        tor.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;
                }
            }
            repaint();
        }
    }
}