import java.awt.*; import java.applet.*; import java.awt.image.*; public class KronP extends Applet implements Runnable { Thread runstring; public Scrollbar leftRight; private MyCanvas canv; private MyPanel thePanel; BorderLayout MyLay; int Flag; public boolean stopped=false; public KronP() //to initialize Flag { setFlag(0); } public void setFlag(int fl) { Flag=fl; //Flag 0 do not begin string motion, 1 yes } public void init() { int width, hei; setLayout(MyLay=new BorderLayout()); thePanel=new MyPanel(this,canv); leftRight=new Scrollbar(Scrollbar.HORIZONTAL,90,0,40,110); canv=new MyCanvas(this); canv.init(); thePanel.init(); add("Center",canv); width=30; hei=120; Button IDOK; CheckboxGroup radios=new CheckboxGroup(); Checkbox onda2=new Checkbox("2",radios,false); Checkbox onda3=new Checkbox("3",radios,false); Checkbox onda4=new Checkbox("4",radios,false); Checkbox onda5=new Checkbox("5",radios,true); Checkbox onda6=new Checkbox("6",radios,false); add(onda2); add(onda3); add(onda4); add(onda5); add(onda6); onda2.reshape(10,hei,width,hei/10); onda3.reshape(10,hei+15,width,hei/10); onda4.reshape(10,hei+30,width,hei/10); onda5.reshape(10,hei+45,width,hei/10); onda6.reshape(10,hei+60,width,hei/10); 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(380);} 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; } if((ev.id==ev.MOUSE_DRAG)){ canv.Xmos=ev.x; canv.Ymos=ev.y; canv.erase=true; showStatus("Cursor" + ev.x + " " + ev.y); canv.repaint(); return true; } if(ev.id==ev.MOUSE_ENTER){ //to show black square at beginning //when mouse enters the applet canv.repaint(); return true; } if(ev.target instanceof Checkbox){ Checkbox button=(Checkbox)ev.target; String buttonLabel=button.getLabel(); showStatus(buttonLabel); if(buttonLabel.equals("2")&&ev.id==ev.MOUSE_DOWN)canv.nonda=2; if(buttonLabel.equals("3")&&ev.id==ev.MOUSE_DOWN)canv.nonda=3; if(buttonLabel.equals("4")&&ev.id==ev.MOUSE_DOWN)canv.nonda=4; if(buttonLabel.equals("5")&&ev.id==ev.MOUSE_DOWN)canv.nonda=5; if(buttonLabel.equals("6")&&ev.id==ev.MOUSE_DOWN)canv.nonda=6; return true; } return false; } } //class class MyCanvas extends Canvas implements ImageObserver{ //all the painting occurs here KronP runstring; double myp,mxp,byp,bxp; double mxx,bxx,myy,byy,yypos,xxpos,pi,dx,dt; double pmxx,pbxx,pmyy,pbyy; private Image offScreenImage; Graphics offScreenGraphics; int ypos,xpos,max,cor,nonda; int Xmos,Ymos,Xdef,Ydef; //mouse positions boolean erase=false; int first,incr,jj=1; double psr[][], psi[][], v[], p2[]; int comien, ancho; int flag=0; //do not draw equation public void init() { psr=new double[601][2]; psi=new double[601][2]; p2=new double[601]; v=new double[601]; jj=1; mxp=500/263.0; bxp=-mxp*17.0; myp=-600.0/155.0; byp=600*205.0/310.0; pi=3.14159265358979323846; max=600; dx=0.02; dt=0.9*dx*dx; Xdef=180; Ydef=45; incr=10; cor=-8; nonda=5; } public MyCanvas(KronP runstring) //to initialize class and variables { this.runstring=runstring; setWidth(50); //initial position of the horizontal } 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 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=500; maxy=250; 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)); } void coorpot(double xiz,double ysu,double xde,double yinf) { //to convert world to screen coordinates. Gives pmxx pbxx for //x_screen=pmxx*x_world +byy same for y. double maxx,maxy,xxfin,xxcom,yyin,yysu; maxx=500; maxy=250; xxcom=0.05*maxx; xxfin=0.8*maxx; yyin=0.98*maxy; yysu=0.2*maxy; pmxx=(xxfin-xxcom)/(xde-xiz); pbxx=0.5*(xxcom+xxfin-mxx*(xiz+xde)); pmyy=(yyin-yysu)/(yinf-ysu); pbyy=0.5*(yysu+yyin-myy*(yinf+ysu)); } public void rectangulo(Graphics g) { //draws instruction rectangle int conv,factor; factor=100; ancho=60; if(nonda==6){ factor=85; ancho=32; } if(nonda==5){ factor=100; ancho=30; } if(nonda==4){ factor=120; ancho=35; } if(nonda==3){ factor=150; ancho=58; } if(nonda==2){ factor=200; ancho=80; } conv=(int)(pmxx*(factor-12)+pbxx); g.drawRect(conv,25,ancho,155); // g.drawString("Drag mouse",142,60); // g.drawString("in this area",142,80); // g.drawString("to select potential",132,120); erase=false; } public void zeropot() { int i; for(i=0;icomien)&&(Ymos>24)&&(Ymos<191)){ g.setColor(getBackground()); inirec(g); g.setColor(Color.blue); g.drawLine(comien,97,comien,Ymos+cor); g.drawLine(comien,Ymos+cor,Xmos,Ymos+cor); g.drawLine(Xmos,Ymos+cor,Xmos,97); finis=Xmos-comien; for(i=1;i0){ peo=(int)(mxx*(i-1)+bxx); peyo=(int)(myy*p2[i-1]+byy); peyv0=(int)(myy*v[i-1]/250.0+byy); pex=(int)(mxx*i+bxx); peye=(int)(myy*p2[i]+byy); g.drawLine(peo,peyo,pex,peye); peyv1=(int)(myy*v[i]/250.0+byy); if(n !=1) g.drawLine(peo,peyv0,pex,peyv1); } } } } } public void Equation(Graphics g) { int peo,peye,pex,peyo,peyv0,peyv1; double x,fact2; double k0,vh; double expon,poten; int i,n,arrow; xxpos=(double)xpos; x=0.0; selectmom(g); g.setColor(Color.blue); g.drawLine(17,97,17,20); g.drawLine(400,97,400,20); g.drawLine(17,97,30,97); if(jj==1){ // initial conditions coormundo(0.0,1.5,max+1,-1.5); xxpos=(double)xpos; k0=pi*xxpos/10.0; g.setColor(Color.blue); for(i=0;i0){ peo=(int)(mxx*(i-1)+bxx); peyo=(int)(myy*p2[i-1]+byy); peyv0=(int)(myy*v[i-1]/250.0+byy); pex=(int)(mxx*i+bxx); peye=(int)(myy*p2[i]+byy); g.drawLine(peo,peyo,pex,peye); peyv1=(int)(myy*v[i]/250.0+byy); if(v[i] !=v[i-1])peo=pex; if(n !=1)g.drawLine(peo,peyv0,pex,peyv1); } } } // 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 KronP 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(KronP 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(); } return true; } //else if(ev.target instanceof Scrollbar){ runstring.postEvent(ev);//sent to Eqstring handlEvent return true; } if((ev.id==ev.MOUSE_DRAG)){ runstring.postEvent(ev); } return false; } }