import java.awt.*; import java.applet.*; import java.awt.image.*; //copyright 2000 M Paez, U Antioquia, R landau, OSU public class Eqstring extends Applet implements Runnable { Thread runstring; private Image offScreenImage; public Scrollbar upDown,leftRight; private MyCanvas canv; private MyPanel thePanel; BorderLayout MyLay; int Flag; public Eqstring() //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); thePanel.init(); upDown=new Scrollbar(Scrollbar.VERTICAL,-5,0,-10,10); leftRight=new Scrollbar(Scrollbar.HORIZONTAL,50,0,5,95); canv=new MyCanvas(); canv.resize(400,200); //plot scrollbars panel and canvas add("Center",canv); add("West",upDown); add("North",leftRight); add("South", thePanel); offScreenImage=createImage(size().width,size().height); //to avoid screen flickering } public void start() { if(runstring==null); { if(Flag==1){ //start string motion runstring=new Thread(this); canv.jj=5; canv.flag=1; runstring.start(); } } } /* public void cont() { if(runstring==null); { runstring=new Thread(this); // if(Flag==1)canv.flag=1; runstring.start(); } }*/ /* public Insets insets() { return new Insets(350,280,120,50); }*/ public void stop() { //stops string motion if(runstring != null){ if(Flag==0)canv.flag=0; runstring.stop(); System.gc(); runstring=null; } } public void run() { while(true){ canv.repaint(); try{ Thread.sleep(250); } catch(InterruptedException e){ } } } public void update(Graphics g) { //To avoid screen flickering 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); } public void paint(Graphics g){ canv.paint(g); //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()); if(Flag==0) canv.repaint(); return true; } return false; } }//class class MyCanvas extends Canvas implements ImageObserver{ //all the painting occurs here double mxx,bxx,myy,byy; int jj,flag; int ypos, xpos; public void init() { jj=5; //every 5 time steps is plot the string int flag=0; //do not draw string } public MyCanvas() //to initialize class and variables { setWidth(80); //initial position of the horizontal setHeight(-5); //position } public void setHeight(int h) { ypos=h; //initial vertical position of the string } public void setWidth(int hx) { xpos=hx; } void coormundo(double xiz,double ysu,double xde,double yinf) { //linear relations to go from world coordinates to //screen coordinates double maxx,maxy,xxfin,xxcom,yyin,yysu; maxx=400; maxy=300; xxcom=0.15*maxx; //useful part of the xxfin=0.75*maxx; //canvas 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,Graphics g) { //Grafica los ejes // La entrada esta en coordenadas 0.0 a 1.0 int i,mp,xpi,xpd,yps,ypi,xdiv,ydiv,ypcen; double yincr,xincr,yte; //char nhorz[3],nver[15]; char nver; int sig=4; ydiv=1; 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,xpd,ypcen); } public void Equation(Graphics g,int jj) {//numerical solution to string pde double rho,ten; int max,peo,peye,pex,peyo; rho=0.1; ten=40.0; max=100; /* density per length */ int i,k; peo=0; peyo=0; pex=0; peye=0; double x[][]; x=new double[101][3]; if(jj==5){ coormundo(0,0.1,100,-0.1); } if(jj>=5) ejes(0,0.1,100,-0.1,g); for(i=0; i0){ peo=(int)(mxx*(i-1)+bxx); peyo=(int)(myy*x[i-1][2]+byy); pex=(int)(mxx*i+bxx); peye=(int)(myy*x[i][2]+byy); g.drawLine(peo,peyo,pex,peye); } } //for i } }//for }//rutine public void paint(Graphics g){ int xi,yi,x0,y0,yf,xf; double xxpos,yypos; xxpos=(double)xpos; yypos=-(double)(ypos)/100.0; coormundo(0,0.1,100,-0.1); ejes(0,0.1,100,-0.1,g); xi=(int)(mxx*xxpos+bxx); yi=(int)(myy*yypos+byy); x0=(int)bxx; xf=(int)(mxx*100+bxx); y0=(int)byy; //g.setColor(Color.yellow); g.drawString("Use Scrollbars to select initial shape",50,30); g.drawLine(xi,yi,xf,y0); //initial string position g.drawLine(x0,y0,xi,yi); //g.drawString("flag ",20,20); if(flag==1){ g.setColor(getBackground()); g.drawLine(xi,yi,xf,y0); g.drawLine(x0,y0,xi,yi); g.setColor(Color.black); Equation(g,jj); jj=jj+5; } } } class MyPanel extends Panel{ //controls Stop, start and scrollbars //transmits to Eqstring the events Eqstring 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(Eqstring 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.setFlag(0); runstring.stop(); } /* if(label.equals("Cont")){ // runstring.setFlag(1); runstring.cont(); } */ if(label.equals("Start")){ runstring.setFlag(1); runstring.start(); } return true; } else if(ev.target instanceof Scrollbar){ runstring.postEvent(ev);//sent to Eqstring handlEvent return true; } return false; } }