import java.awt.*; import java.applet.*; import java.awt.image.*; public class Harmos2 extends Applet implements Runnable { Thread runstring; public Scrollbar upDown,leftRight; private MyCanvas canv; private MyPanel thePanel; BorderLayout MyLay; int Flag; public boolean stopped=false; public Harmos2() //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); upDown=new Scrollbar(Scrollbar.VERTICAL,35,0,10,65); leftRight=new Scrollbar(Scrollbar.HORIZONTAL,50,0,10,100); canv=new MyCanvas(this); canv.init(); thePanel.init(); add("Center",canv); add("West",upDown); 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; // runstring.start(); } public void run() { while(true){ try{ Thread.currentThread().sleep(250);} 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==upDown)canv.setHeight(upDown.getValue()); 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 Harmos2 runstring; double mxx,bxx,myy,byy,yypos,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() //canvas { psr= new double[301][2]; psi=new double[301][2]; v=new double [301]; p2=new double [301]; jj=1; } public MyCanvas(Harmos2 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; } /* 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=250; xxcom=0.05*maxx; xxfin=0.9*maxx; yyin=0.95*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; int pvx,pvy,max,arrow; double pi,dx,k0; int i,n; double x; double dt; //k0: initial momentum given to the wave packet dx=0.02; dt=dx*dx/4.0; max=300; pi=3.1415926535897932385; // k0=3*pi; xxpos=(double)xpos; k0=3*pi*xxpos/50.0; x=-3.0; g.drawString("Momentum Scrollbar",150,10); g.drawString("Potential Scrollbar",20,155); g.setColor(Color.red); g.drawRect(5,10,320,150); g.setColor(Color.blue); if(jj==1){ coormundo(0.0,3.5,301,0.0); incr=20; max=300; yypos=(double)ypos; xxpos=(double)xpos; arrow=(int)(40.0*xxpos/50.0)+165; g.setColor(Color.yellow); g.drawLine(165,105,arrow,105);//arrow g.drawLine(arrow-5,100,arrow,105); //upper tip g.drawLine(arrow-5,110,arrow,105); //lower tip g.setColor(Color.blue); for(i=0;i0){ peo=(int)(mxx*(i-1)+bxx); peyo=(int)(myy*(p2[i-1]+0.0015*v[i-1])+byy); peyv0=(int)(myy*0.0015*v[i-1]+byy); pex=(int)(mxx*i+bxx); peye=(int)(myy*(p2[i]+0.0015*v[i])+byy); g.drawLine(peo,peyo,pex,peye); peyv1=(int)(myy*0.0015*v[i]+byy); g.drawLine(peo,peyv0,pex,peyv1); } } } // new iterations are now the old ones for(i=0;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 Harmos2 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(Harmos2 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; } }