import java.applet.*;
import java.awt.*;
import java.util.Date;
public class s1 extends Applet implements Runnable{
float x1, v, x2, a=5, t=0, t1=0, t2=0, pm=10, vahe;
int apikkus=3;
long aeg1, aeg2;
boolean p1=false, p2=false, valmis=false;
Image pilt, sahver, auto1, auto2;
Graphics piltg, sahverg, iseg;
Thread tr;
Date dat1, dat2;
Label l1=new Label();
Button sn=new Button("Start");

public void init(){
 pilt=createImage(400, 100);
 sahver=createImage(400, 100);
 piltg=pilt.getGraphics();
 sahverg=sahver.getGraphics();
 iseg=this.getGraphics();
 auto1=getImage(getCodeBase(), "auto.gif");
 auto2=getImage(getCodeBase(), "auto03.gif");
 this.setLayout(new BorderLayout());
 add("North", l1);
 add("South", sn);
 v=10;
 vahe=v*v/a/2;
}

void edasi(){
 t+=0.01;
 if(p1)t1+=0.01;
 if(p2)t2+=0.01;
 arvuta();
 joonista();
}

void arvuta(){
// if(a*t1<=v) x1=v*t-a*t1*t1/2;
if(!p1)x1=v*t;
 if(a*t2<=v) x2=v*t-a*t2*t2/2-vahe-apikkus;
 else valmis=true;
}
public void start(){tr=new Thread(this); tr.start();}
public void run(){
sn.hide();
int koht;
p1=false; p2=false; t=t1=t2=0;
valmis=false;
int pk=100+(int)(200*Math.random());
koht=0;
while(koht++<500&&!valmis){
edasi();
try{tr.sleep(10);} catch(Exception e){}
if(pk==koht){p1=true; dat1=new Date(); aeg1=dat1.getTime();}
}
sn.show();
}
public boolean keyDown(Event e, int key){
if((char)key=='l'){
 if(p1==true){p2=true;dat2=new Date(); aeg2=dat2.getTime();
 l1.setText(Integer.toString((int)(aeg2-aeg1)));}
 else l1.setText("Vara veel!");
 }
return true;
}

public boolean action(Event e, Object arg){
  if(e.target==sn)start();
return super.action(e, arg);
 }
void joonista(){
 piltg.setColor(Color.white);
 piltg.fillRect(0, 0, 400, 400);
 piltg.drawImage(auto1, (int)(x1*pm), 40, (int)(apikkus*pm), 30, null);
 piltg.drawImage(auto2, (int)(x2*pm), 40, (int)(apikkus*pm), 30, null);
// iseg.drawImage(pilt, 0, 0, this);
repaint();
}
public void paint(Graphics g){g.drawImage(pilt, 0, 0, this);}
public void update(Graphics g){paint(g);}

}
