import java.awt.*; import java.applet.*; import java.awt.image.*; public class Asym extends Applet implements Runnable { Thread runstring; public Scrollbar leftRight; private MyCanvas canv; private MyPanel thePanel; BorderLayout MyLay; int Flag; public boolean stopped=false; public Asym() //to initialize Flag { setFlag(0); } public void setFlag(int fl) { Flag=fl; //Flag 0 do not begin string motion, 1 yes } public void init() { setLayout(MyLay=new BorderLayout()); thePanel=new MyPanel(this,canv); leftRight=new Scrollbar(Scrollbar.HORIZONTAL,80,0,40,110); canv=new MyCanvas(this); canv.init(); thePanel.init(); add("Center",canv); add("North",leftRight); add("South", thePanel); } public void start() { if(! stopped) { if(Flag==1){ canv.jj=1; canv.flag=1; runstring=new Thread(this); runstring.start(); } } } public void stop() { if(runstring != null){ runstring.suspend(); runstring=null; } } public void brk() { //stops motion temporarily runstring.stop(); stopped=false; setFlag(0); canv.jj=1; } public void run() { while(true){ try{ Thread.currentThread().sleep(350);} catch (InterruptedException e){ } canv.repaint(); } } public void paint(Graphics g){ canv.repaint(); //called many times } public boolean handleEvent(Event ev){ //events sent by Mypanel and scrollbar interpreted here if(ev.target instanceof Scrollbar){ if(ev.target==leftRight)canv.setWidth(leftRight.getValue()); canv.repaint(); return true; } return false; } } //class class MyCanvas extends Canvas implements ImageObserver{ //all the painting occurs here Asym runstring; double mxx,bxx,myy,byy,xxpos; private Image offScreenImage; Graphics offScreenGraphics; int ypos,xpos; int first,incr,jj=1; double psr[][], psi[][], v[], p2[]; int flag=0; //do not draw equation public void init() { psr=new double[401][2]; psi=new double[401][2]; p2=new double[401]; v=new double[401]; jj=1; } public MyCanvas(Asym runstring) //to initialize class and variables { this.runstring=runstring; setWidth(50); //initial position of the horizontal // setHeight(20); //position } public void update(Graphics g) { //To avoid screen flicker use double buffer technique offScreenImage=createImage(size().width,size().height); 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); } /* public void setHeight(int h) { ypos=h; //initial vertical }*/ public void setWidth(int hx) { xpos=hx; } void coormundo(double xiz,double ysu,double xde,double yinf) { //to convert world to screen coordinates. Gives mxx bxx for //x_screen=mxx*x_world +byy same for y. double maxx,maxy,xxfin,xxcom,yyin,yysu; maxx=350; maxy=280; xxcom=0.05*maxx; xxfin=0.8*maxx; yyin=0.98*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)); } public void Equation(Graphics g) { int peo,peye,pex,peyo,peyv0,peyv1; double dt,x,fact2,fact22; double pi,dx,k0,vh; double expon,expon2; int i,n,max,arrow; pi=3.14159265358979323846; max=400; xxpos=(double)xpos; dx=0.02; dt=0.9*dx*dx; x=0.0; g.drawString("Momentum",60,10); // g.drawString("M.J. Paez",250,200); g.setColor(Color.red); g.drawRect(5,10,340,190); g.setColor(Color.blue); if(jj==1){ // initial conditions coormundo(0.0,1.5,max+1,-1.5); incr=10; xxpos=(double)xpos; k0=pi*xxpos/7.0; arrow=(int)(2.0*xxpos/5.0)+50; g.setColor(Color.yellow); g.drawLine(130,85,130+arrow,85);//arrow g.drawLine(130+arrow-5,80,130+arrow,85); //upper tip g.drawLine(130+arrow-5,90,130+arrow,85); //lower tip g.setColor(Color.blue); for(i=0;i0){ peo=(int)(mxx*(i-1)+bxx); peyo=(int)(myy*p2[i-1]+byy); pex=(int)(mxx*i+bxx); peye=(int)(myy*p2[i]+byy); g.drawLine(peo,peyo,pex,peye); } } } // new iterations are now the old ones for(i=1;i=incr+1)first=jj-incr; Equation(g); jj=jj+incr; } } } class MyPanel extends Panel{ //controls Stop, start and scrollbars //transmits to Eqstring the events Asym runstring; GridBagLayout GBLay; GridBagConstraints c; MyCanvas canv; public void init(){ GBLay=new GridBagLayout(); c=new GridBagConstraints(); setLayout(GBLay); c.gridx=1; c.gridy=1; c.gridheight=1; c.fill=GridBagConstraints.BOTH; /* Button sta=new Button("Cont"); GBLay.setConstraints(sta,c); add(sta);*/ // c.gridx=2; Button stp=new Button("Stop"); GBLay.setConstraints(stp,c); add(stp); c.gridx=2; Button rest=new Button("Start"); GBLay.setConstraints(rest,c); add(rest); } public MyPanel(Asym runstring, MyCanvas canv) { this.canv=canv; this.runstring=runstring; } public boolean action(Event ev, Object arg) { if (ev.target instanceof Button) { String label=(String)ev.arg; if (label.equals("Stop")){ runstring.brk(); } if(label.equals("Start")){ runstring.setFlag(1); runstring.stopped=false; runstring.start(); canv.flag=1; } return true; } else if(ev.target instanceof Scrollbar){ runstring.postEvent(ev);//sent to Eqstring handlEvent return true; } return false; } }