/* Initial conditions for the temperature of a bar vary as 100 sin(2pi x) for 0<= x <=1 Color code: red for positive temperatures, blue for negative, black for 0 T*/ import java.awt.*; import java.applet.*; public class Barsin2 extends Applet implements Runnable { Thread runstring; private Image offScreenImage; double mxx,bxx,myy,byy; int jj,Flag,firstime; public Barsin2() //to initialize Flag { setFlag(0); } public void setFlag(int fl) { Flag=fl; //Flag 0 do not begin string motion, 1 yes } public void start() { if(runstring==null) { if(Flag==0){ runstring=new Thread(this); runstring.start(); } } } public void stop() { //stops motion if(runstring != null){ runstring.stop(); System.gc(); runstring=null; } } public void run() { while(true){ repaint(); try{ Thread.sleep(300); } catch(InterruptedException e){ } } } public void init() { offScreenImage=createImage(size().width,size().height); add(new Button("Stop")); add(new Button("Start")); jj=1; Flag=0; firstime=0; } 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=600; maxy=450; xxcom=maxx/12.0; xxfin=11.0*maxx/12.0; 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) { //Plot the axis // 0.0 to 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-30,xpd,ypcen-30); g.drawLine(xpi,yps-30,xpi,ypi-30); yincr=(ymi-yms)/5.0; xincr=(xmd-xmz)/8.0; //tics in vertical axis for (i=0;i<6;i++){ ydiv=(int)(myy*(yms+yincr*i)+byy-30); g.drawLine(xpi-10,ydiv,xpi,ydiv); yte=yms+yincr*i; if(Math.abs(yte)<0.01)yte=0.0; g.drawString(Double.toString(yte),xpi-30,ydiv+5); g.drawString("Temperature",xpi,yps-30); } } public void Barsin2plot(Graphics g) { double ro,sph,diffk,cons,dx,dt,el,xx; double thk,divi,u[][],pi; int j,ngrid; int nmaxi,k,i,il,km,ii; int peo,peyo,pex,peye,tone; ngrid=101; u=new double[101][2]; sph=0.217; thk=0.49; ro=2.7; diffk=thk/(sph*ro); el=50.0; if(jj==1){ coormundo(0,120.0,100.0,-120.0); } /*the bar is divided in 100 parts (divi) */ divi=100.0; cons=0.1; dx=el/divi; dt=cons*dx*dx/diffk ; pi=3.14159265358979323846; // At t=0 ( j=1) the temperature distribution for(i=0;i<100;i++){ xx=0.01*i; u[i][0]=100.0*Math.sin(2.0*pi*xx); } //in i=1 and i=101 (ends) U=0 for all times for(j=0;j<2;j++){ u[0][j]=0.0; u[100][j]=0.0; } nmaxi=20000; //loop over time //loop 40 over space, end points not included (known) for(k=1;k0){ peo=(int)(mxx*(i-1)+bxx); peyo=(int)(myy*u[i-1][1]+byy-30); pex=(int)(mxx*i+bxx); peye=(int)(myy*u[i][1]+byy-30); g.drawLine(peo,peyo,pex,peye); } } //for i } for(i=1;i<100;i++)u[i][0]=u[i][1]; } //for k } //rutine public void paint(Graphics g){ Barsin2plot(g); if(Flag==1){ jj=jj+50; } } public boolean action(Event ev, Object whataction) { if(!(ev.target instanceof Button)) { return false; } String buttonLabel=(String) whataction; if(buttonLabel=="Start") { firstime=0; jj=1; //To start from initial conditions setFlag(1); runstring.start(); } if(buttonLabel=="Stop") { //really means pause, it doesnït kill applet setFlag(0); firstime=1; } return true; } }//class