import java.awt.*; import java.applet.*; import java.awt.image.*; /*Author Manuel J Paez Departamento de Fisica Universidad de Antioquia Medellin-COLOMBIA*/ public class Dinam extends Applet { private Scrollbar Bvelocity,Clockrate; private Dinamcan canv; private Panel elpanel,pan; private Button stp,rest; private TextField t1,t2,t3; public void init() { canv=new Dinamcan(); canv.resize(520,380); pan=new Panel(); elpanel=new Panel(); stp=new Button("Stop"); rest=new Button("Start"); t1=new TextField("Vel B 0.8 c"); t2=new TextField("v=0.5 c"); t3=new TextField("rate=1.0"); t1.setEditable(false); t2.setEditable(false); t3.setEditable(false); Bvelocity=new Scrollbar(Scrollbar.HORIZONTAL,80,0,10,99); Clockrate=new Scrollbar(Scrollbar.VERTICAL,40,0,10,110); elpanel.setLayout(new BorderLayout()); pan.setLayout(new GridLayout(10,1)); pan.add(stp); pan.add(rest); pan.add(t1); pan.add(t2); pan.add(t3); elpanel.add("East", pan); elpanel.add("Center", canv); elpanel.add("South",Bvelocity); elpanel.add("West",Clockrate); setLayout(new BorderLayout()); add("Center",elpanel); canv.Flag=0; canv.init(); canv.start(); } public boolean handleEvent(Event ev){ //events sent by Dinamcan and scrollbar interpreted here if(ev.target instanceof Scrollbar){ if(ev.target==Bvelocity){ canv.xpos=(Bvelocity.getValue()); t2.setText("v="+Double.toString(canv.vrel)+"c"); t1.setText("Vel B " +Double.toString(canv.vwr)+"c"); } if(ev.target==Clockrate){ canv.tpos=(Clockrate.getValue()); t3.setText("rate="+Double.toString(canv.trel)); } canv.firstime=1; canv.jj=1; canv.scrollyes=1; canv.repaint(); return true; } return super.handleEvent(ev); } public boolean action(Event ev, Object whataction) { if(ev.target instanceof Button){ String buttonLabel=(String) whataction; if(buttonLabel=="Start"){ canv.firstime=0; canv.jj=1; //To start from initial conditions canv.setFlag(1); canv.negat=1; canv.y0=110.0; canv.yb=1.0; canv.time2=0.0; canv.time3=0.0; canv.time4=0.0; canv.negat1=1; canv.scrollyes=1; return true; } if(buttonLabel=="Stop") { canv.setFlag(0); canv.firstime=1; return true; } return false; }//if button else if(ev.target instanceof Scrollbar){ canv.postEvent(ev);//sent to Eqstring handlEvent return true; } return false; } }//class class Dinamcan extends Canvas implements Runnable{ public int xpos,tpos; Thread runstring; double mxx,bxx,myy,byy; Image offScreenImage; Graphics offScreenGraphics; double y0,yb,time3,time2,time4,vc2,sqv,vwr,vrel,trel,ttpos; int firstime,negat,negat1,bslx,bsly,Bslx,Bsly,alx,aly,Aly; int brsx,brsy,Brsx,Brsy,arx,ary,Arx,Ary; int jj; int Flag; int scrollyes; public void init(){ jj=1; y0=110.0; yb=1.0; time2=0.0; time3=0.0; time4=0.0; xpos=80; tpos=40; negat=1; negat1=1; firstime=1; vc2=0.0; sqv=0.0; Flag=1; scrollyes=0; bslx=0; bsly=0;//initialize Bslx=0; Bsly=0;//initialize alx=0; aly=0; Aly=0; brsx=0; brsy=0; Brsx=0; Brsy=0; arx=0; ary=0; Arx=0; Ary=0; offScreenImage=createImage(size().width,size().height); trel=1.0; } public void start() { if(runstring==null) { runstring=new Thread(this); runstring.start(); } } public void stop() { //stops motion if(runstring != null){ runstring.suspend(); runstring=null; } } public void run() { while(true){ if(firstime==0) repaint(); try{ Thread.sleep(15); } catch(InterruptedException e){ } } } public void setFlag(int fl) { Flag=fl; //Flag 0 do not begin string motion, 1 yes } public void update(Graphics g) { //To avoid flicker Graphics offScreenGraphics=offScreenImage.getGraphics(); offScreenGraphics.setColor(getBackground()); offScreenGraphics.fillRect(0,0,size().width,size().height); offScreenGraphics.setColor(g.getColor()); paint(offScreenGraphics); g.drawImage(offScreenImage,0,0,this); } void coormundo(double xiz,double ysu,double xde,double yinf) { double maxx,maxy,xxfin,xxcom,yyin,yysu; maxx=280; maxy=250; xxcom=maxx/12.0+100; xxfin=11.0*maxx/12.0+100; yyin=0.8*maxy; yysu=0.2*maxy; mxx=(xxfin-xxcom)/(xde-xiz); bxx=0.5*(xxcom+xxfin-mxx*(xiz+xde)); myy=(yyin-yysu)/(yinf-ysu); byy=0.5*(yysu+yyin-myy*(yinf+ysu)); } void ejes(double xmz,double yms,double xmd,double ymi,int offy,Graphics g, int tics) { //Plot the axis // 0.0 to 1.0 int mp,xpi,xpd,yps,ypi,xdvi,ypcen,i; char nver; int sig=4; mp=(int)((xmd-xmz)/tics); xpi=(int)(mxx*xmz+bxx); xpd=(int)(mxx*xmd+bxx); yps=(int)(myy*yms+byy); ypi=(int)(myy*ymi+byy); ypcen=(int)byy; g.drawLine(xpi,ypcen-30+offy,xpd,ypcen-30+offy); g.drawLine(xpi,yps-30+offy,xpi,ypi-30+offy); xdvi=xpi; for(i=1;i<=mp;i++){ xdvi=xdvi+tics; g.drawLine(xdvi,ypcen-40+offy,xdvi,ypcen-25+offy); } } void Arrow(Graphics g, int xsc, int ysc, int col){ int arrow; if(col==1)g.setColor(Color.black); if(col==0)g.setColor(getBackground()); arrow=(int)(40.0*xpos/50.0)+55; g.drawLine(xsc,ysc,arrow+xsc,ysc);//arrow g.drawLine(xsc+arrow-5,ysc+5,xsc+arrow,ysc); //upper tip g.drawLine(xsc+arrow-5,ysc-5,xsc+arrow,ysc); //lower tip } public void clock(Graphics g, double time, int xov,int yov) { //draws clock at given initial angle int r,xti,yti,xc,yc; float xpnt,ypnt; r=30; xpnt=(float)(0.5*r*Math.sin(3.141592*time/125.0)); ypnt=(float)(0.5*r*Math.cos(3.141592*time/125.0)); g.drawOval(xov,yov,r, r); xti=(int)xpnt; yti=(int)ypnt; xc=xov+15; yc=yov+15; g.drawLine(xc,yc,xc+xti,yc-yti); } public void Dinamplot(Graphics g) { double dx,dt,xx,xxb,vsi,uyb,xb,c,uxb; double uya,xa,ya,time,ybp,yap,xxpos,t0p,dtp,t0,yy0,xred; /* uya:lab vel part A y direc. uxb:lab vel part B x direc. is selected with scrollbar Uyb: rocket vel part B y direc. p: after collision, vertical vsi: relative velocity of one system w r t the other */ double Uya,Xa,Xa0,Yap,Xb,Time,Ybp,Uyb,Yb,Ya;//red particles //upper case primed system double ty,xacom,tiempo,tco,xin,Ybini,Tcol; int j,col,wri,vreli,tics, ticsv,jump; int nmaxi,k,i; int peb,peyb,pea,peya,pex,Peb,Pea,Peya,Peyb,pexb; dt=1.0; vsi=1.0; //vel of one system wrt the other tics=90; xxpos=(double)xpos; //from scrollbar uyb=1.0; //y vel b particle in lab time=0.0; ybp=0.0; Xb=0.0; jump=1; tiempo=0.0; uya=1.0; Pea=0; Peya=0; Tcol=0.0; //initialize time of colision in S prime from origin Peb=0; c=2.5; //light speed uxb=xxpos*c/100.0; //selected with scrollbar wri=(int)(1000.0*uxb/2.5); vwr=wri/1000.0; //to write in screen ty=y0/uyb; //time from y0 to x axis for b xa=uxb*ty; //initial position of A (same distance b) Xa0=xa; vsi=c*c/uxb-c*Math.sqrt(c*c/(uxb*uxb)-1.0);// v=ubx' //system v vreli=(int)(1000.0*vsi/c);//limit decimal places vrel=vreli/1000.0; vc2=vsi*vsi/c/c; sqv=Math.sqrt(1.0-vc2); ticsv=(int)(tics*sqv); Uyb=uyb*sqv/(1.0-uxb*vsi/c/c); Uya=uya*sqv; t0p=-xa*vsi/(c*c*sqv); t0=-t0p/sqv; //positive at the beginning xin=-t0*vsi; dtp=0.0; xb=-uxb*t0; ya=-y0-uyb*t0; yb=y0+uyb*t0; Yb=y0+Uyb*t0; //t0p is - yy0=yb; Ybini=Yb; ttpos=(int)(((double)tpos*0.03-0.2)*1000.0); //scale 0.1 to 3.1 trel=(double)ttpos/1000.0; tco=(2.0*y0)/(Uya+Uyb);//begins when the 2 systems coincide Tcol=(2.0*y0)/(Uya+Uyb);//begins when the 2 systems coincide xred=-vsi*t0; //motion of red system Xb=-vsi*t0;//arrives a y prime when blue does xacom=vsi*t0+2*vsi*tco;//initial position A in S prime Xa=xacom; Ya=-y0-Uya*t0; if(jj==1){ coormundo(0,220.0,800.0,-220.0); g.drawString("Laboratory system",15,100); g.drawString("Rocket system",310,300); peb=(int)(mxx*xb+bxx); Peb=(int)(mxx*Xb+bxx); Pea=(int)(mxx*Xa+bxx); peyb=(int)(myy*yb+byy-30);//-30 peya=(int)(myy*ya+byy-30);//-30 Peya=(int)(myy*Ya+byy-30); Peyb=(int)(myy*Yb+byy-30); pea=(int)(mxx*xa+bxx); Arrow(g,peb,peyb,1); g.setColor(Color.blue); clock(g,-t0, 35,35); bslx=peb; bsly=peyb; g.fillOval(peb-5,peyb-5,10,10); alx=pea; aly=peya; g.drawOval(pea-5,peya-5,10,10); g.drawString("A",pea-15, peya); g.drawString("B",160,30); ejes(0.0,220.0,800.0,-220.0,0,g,0); ejes(-xin,220.0,800.0-xin,-220.0,160,g,0); g.setColor(Color.red); clock(g,-t0,300,180); ejes(0,220.0,800.0,-220.0,160,g,0); ejes(xred,220.0,800.0,-220.0,0,g,0); brsx=Peb; brsy=Peyb; g.fillOval(Peb-5,Peyb-5+160,10,10); arx=Pea; ary=Peya; g.drawOval(Pea-5,Peya-5+160,10,10); } //loop over time for(k=1;k1)) { Tcol=t0+Tcol-dt*trel; if(Tcol<=0.0)time4=time4+1.0; pea=(int)(mxx*xa+bxx); g.drawString("Laboratory system",15,100); g.drawString("Rocket system",310,300); g.setColor(Color.red); ejes(0,220.0,800.0,-220.0,160,g,0); clock(g,-t0+trel*dt,300,180); // x tprime red g.setColor(Color.blue); clock(g,-t0+dt, 35,35); ejes(0,220.0,800.0,-220.0,0,g,0); xx=vsi*dt; // red axis moving xxb=trel*vsi*dt; //blue axis move xb=xb+ uxb*dt; Xa=xacom-trel*vsi*dtp; //motion of A rocket peb=(int)(mxx*xb+bxx); Xb=-vsi*t0+trel*vsi*tiempo; //rocket B if(Tcol>=0.0){ Yb=Ybini-Uyb*tiempo*trel; //rocket B // g.drawString("Tcol "+Tcol/trel,100,70); // g.drawString("Tcol/terel "+Tcol,100,40); Peb=(int)(mxx*Xb+bxx); Ya=-y0-Uya*t0+trel*Uya*dt; Pea=(int)(mxx*Xa+bxx); Peya=(int)(myy*Ya+byy-30); g.setColor(Color.red); Peyb=(int)(myy*Yb+byy-30); g.drawLine(brsx,brsy+160,Peb,Peyb+160); g.fillOval(Peb-5,Peyb-5+160,10,10); g.drawLine(arx,ary+160,Pea,Peya+160); g.drawOval(Pea-5,Peya-5+160,10,10); Brsx=Peb; Brsy=Peyb; Arx=Pea; Ary=Peya; } if(Tcol<=0.0) { g.setColor(Color.red); Ybp=y0+time4*Uyb*trel-tco*Uyb;//after colision b part. Yap=-y0+Uya*tco-time4*Uya*trel; Peb=(int)(mxx*Xb+bxx); Peyb=(int)(myy*Ybp+byy-30); Pea=(int)(mxx*Xa+bxx); Peya=(int)(myy*Yap+byy-30); g.fillOval(Peb-5,Peyb-5+160,10,10); g.drawLine(Brsx,Brsy+160,Peb,Peyb+160); g.drawLine(brsx,brsy+160,Brsx,Brsy+160); g.drawOval(Pea-5,Peya-5+160,10,10); g.drawLine(Arx,Ary+160,Pea,Peya+160); g.drawLine(arx,ary+160,Arx,Ary+160); } g.setColor(Color.blue); if((yb>0.0)){ yb=yy0-uyb*time; ya=-yy0+uya*time; peyb=(int)(myy*yb+byy-30);//-30 Arrow(g,peb,peyb,0); peya=(int)(myy*ya+byy-30);//-30 pex=(int)(mxx*xx+bxx); pexb=(int)(mxx*xxb+bxx); g.setColor(Color.red); ejes(xred+xx,220.0,800.0+xred+xx,-220.0,0,g,0); if(negat==1){ g.setColor(Color.blue); g.drawLine(bslx,bsly,peb,peyb); g.fillOval(peb-5,peyb-5,10,10); g.drawLine(alx,aly,pea,peya); g.drawOval(pea-5,peya-5,10,10); Bslx=peb; Bsly=peyb; Aly=peya; } if(negat1==-1){ time3=time3+1.0; g.setColor(Color.red); } }//yb>0.0 if(yb<=0.0) negat=-1; g.setColor(Color.blue); ejes(-xred-xxb,220.0,800.0-xred-xxb,-220.0,160,g,0); if(yb<=0.0){ g.setColor(Color.blue); ybp= uyb*time2; yap=-uya*time2; peyb=(int)(myy*ybp+byy-30); peya=(int)(myy*yap+byy-30); g.drawLine(Bslx,Bsly,peb,peyb); g.drawLine(bslx,bsly,Bslx,Bsly); g.fillOval(peb-5,peyb-5,10,10); g.drawLine(pea,Aly,pea,peya); g.drawOval(pea-5,peya-5,10,10); g.setColor(Color.red); time3=time3+1.0; } //if yb }//if k dt=dt+1.0; time=time+1.0; if(yb<=0.0)time2=time2+1.0; dtp=dtp+1.0; tiempo=tiempo+1.0; } //for k } //rutine public void paint(Graphics g){ int maxim; maxim=550; if(trel<0.5)maxim=1200; if(trel>1.5)maxim=300; Dinamplot(g); if((Flag==1)&&(scrollyes==1)){ jj=jj+1; } if(jj==maxim){ firstime=1; } } }//class can