import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;public class Race extends JApplet implements Runnable
{
int x1,x2,y1,y2;
Thread a=null;
public void init()
{
Container cp=getContentPane();
x1=10;
x2=10;
y1=50;
y2=100;
}
public void start()
{
a=new Thread(this);
a.start();
}
public void run()
{
int d1=10,d2=5;
while(true)
{
x1+=d1;
x2+=d2;
if(x1>300)
{ JOptionPane.showMessageDialog(null,"兔子获胜!");
System.exit(0);
a.stop();
} repaint();
try{a.sleep(100);}
catch(InterruptedException e){}
}
}
public void paint(Graphics g)
{ g.setColor(Color.gray);
g.fillRect(0,0,400,200);
g.setColor(Color.red);
g.drawLine(310,0,310,150);
g.setColor(Color.red);
g.fillOval(x1,y1,10,10);
g.setColor(Color.green);
g.fillOval(x2,y2,10,10);
g.drawString("红球是兔子,绿球是乌龟!",50,180);
}
}