// Applet to do inclined plane simulation // Original: Gary D. Westfall // Modisimplified: W. Bauer // August, 1997; October 1997 import java.awt.*; import java.applet.*; public class plane extends Applet { String imageName="hand.gif"; public MediaTracker tracker; public Image image; Image offscreenImage; public void init() { // Get the hand image tracker= new MediaTracker(this); image=this.getImage(getDocumentBase(),imageName); tracker.addImage(image,0); try { tracker.waitForID(0); } catch (InterruptedException e) { } setBackground(Color.white); setupMyImage(); centerCanvas c = new centerCanvas(offscreenImage,image); Panel westPanel; westPanel = new WestPanel(c); setLayout(new BorderLayout()); add("East",westPanel); add("Center",c); repaint(); } public void setupMyImage() { if (offscreenImage != null) { offscreenImage.flush(); } offscreenImage=createImage(470,300); } } class centerCanvas extends Canvas { int left=30; int right=385; int step=50; boolean myBegin,myReset; double ang=20.0;; double oldang=20.0; double coefk=0.3; double coefs=0.36; double m1=25.0; double oldm1=25.0; double m2=0.0; double oldm2=0.0; Rectangle r; Image myOffImage; Image handImage; Graphics offg; int numCollisions; WestPanel westPanel; double a; double grav=9.81; int yoff; int ymax=0; public centerCanvas(Image offImage,Image i1) { myOffImage=offImage; handImage=i1; offg = offImage.getGraphics(); myReset=true; repaint(); } public void update(Graphics g){ paint(g); } public void paint(Graphics g) { r = bounds(); if(myReset){ drawMyBackground(175,225,true,0.0); // Draw the off-screen image g.drawImage(myOffImage,30,0,this); myReset=false; } else if (myBegin){ drawMySim(g); // Draw the off-screen image myBegin=false; } } public void setvar(double angn,double coefkn,double coefsn,double m1n,double m2n){ ang=angn; coefk=coefkn; coefs=coefsn; m1=m1n; m2=m2n; if((oldang != ang) || (oldm1 != m1) || (oldm2 != m2)){ myReset=true; repaint(); } oldang=ang; oldm1=m1; oldm2=m2; } public void redraw(boolean beginn,boolean reset){ myBegin = beginn; myReset = reset; repaint(); } public int getColls(){ return numCollisions; } public void drawMySim(Graphics g){ int i; int m1x,m2y; double myR; int newx; double theta; double step=0.2; double t; long timeBegin,timeEnd; long timeDiff; int aint; double adis; int waitTime=50; int checkx,checky; double maxFs,maxFsAbs; double netForce,netForceAbs; theta=Math.PI*ang/180.0; offg.setFont(new Font("TimesRoman", Font.PLAIN, 12)); // Check to see if we can overcome static friction maxFs=m1*grav*coefs*Math.cos(theta); netForce=grav*(m2-m1*Math.sin(theta)); maxFsAbs=Math.abs(maxFs); netForceAbs=Math.abs(netForce); if(netForceAbs < maxFsAbs){ // We cannot overcome static friction, so do not move drawMyBackground(175,225,false,0.0); offg.setColor(Color.red); offg.drawString("Schwerkraftskomponente < Reibung",15,15); offg.drawString("Masse bleibt haften",15,30); g.drawImage(myOffImage,30,0,this); } else if(netForceAbs < 0.1){ g.setColor(Color.red); g.drawString("Nettokraft ist fast 0",15,15); g.drawString("Keine sichtbare Bewegung",15,30); } else if (netForce >= 0.1){ a=grav*(m2-m1*Math.sin(theta)-coefk*m1*Math.cos(theta))/(m1+m2); aint=(int)(a*100.0); adis=((double)(aint))/100.0; t=0.0; timeBegin=System.currentTimeMillis(); do { newx=(int)((0.5*a*t*t)+0.5); myR=(double)(newx); m1x=(int)((myR*Math.cos(Math.PI*ang/180.0))+0.5); m2y=newx; drawMyBackground(175+m1x,225+m2y,false,a); offg.setColor(Color.black); offg.drawString("a = "+adis+" m/s/s",15,15); g.drawImage(myOffImage,30,0,this); t=t+step; timeEnd=System.currentTimeMillis(); timeDiff=timeEnd-timeBegin; while (timeDiff < waitTime) { timeEnd=System.currentTimeMillis(); timeDiff=timeEnd-timeBegin; } timeBegin=timeEnd; } while (newx < 50); } else if(netForce < -0.1){ a=grav*(m2-m1*Math.sin(theta)+coefk*m1*Math.cos(theta))/(m1+m2); aint=(int)(a*100.0); adis=((double)(aint))/100.0; newx=0; t=0.0; timeBegin=System.currentTimeMillis(); checkx=500; checky=500; // while (newx > -50) { do { newx=(int)((0.5*a*t*t)+0.5); myR=(double)(newx); m1x=(int)((myR*Math.cos(Math.PI*ang/180.0))+0.5); m2y=newx; drawMyBackground(175+m1x,225+m2y,false,a); checkx=175+m1x; checky=225+m2y; offg.setColor(Color.black); offg.drawString("a = "+adis+" m/s/s",15,15); g.drawImage(myOffImage,30,0,this); t=t+step; timeEnd=System.currentTimeMillis(); timeDiff=timeEnd-timeBegin; while (timeDiff < waitTime) { timeEnd=System.currentTimeMillis(); timeDiff=timeEnd-timeBegin; } timeBegin=timeEnd; } while ((checky > ymax+30) && (checkx > 25)); } } public void drawMyBackground(int m1LeftEdge,int m2Center,boolean doHand,double accel){ int w,h; int i; int y; int xCoords[]={25,350,350,25}; int yCoords[]={250,250,200,250}; int xBox[]={100,150,150,100,100}; int yBox[]={250,250,200,200,250}; int xDiff; int xs1,ys1,xs2,ys2; int m2xcen,m2ycen; int vx,vy; int vxx,vyy; double myCos,mySin,myTan; double myCos90,mySin90; int vecx,vecy; int tension; double tensiond; offg.setFont(new Font("TimesRoman", Font.PLAIN, 12)); myCos=Math.cos(Math.PI*ang/180.0); mySin=Math.sin(Math.PI*ang/180.0); myCos90=Math.cos(Math.PI*(ang+90.0)/180.0); mySin90=Math.sin(Math.PI*(ang+90.0)/180.0); myTan=Math.tan(Math.PI*ang/180.0); w = r.width; h = r.height; tensiond=2.0*m2*(grav-accel)/grav; tension=(int)(tensiond); // Draw white background offg.setColor(Color.white); offg.fillRect(0,0,w,h); // draw plane offg.setColor(Color.orange); y=(int)(325.0*myTan); yCoords[2]=250-y; offg.fillPolygon(xCoords,yCoords,4); // Draw an arc to show ang on the inclined plane offg.setColor(Color.black); y=(int)(50.0*myTan); offg.drawArc(0,225,50,50,0,(int)ang); offg.drawString("phi",60,250-y/4+2); // Draw the table top offg.setColor(Color.gray); offg.fillRect(0,250,351,45); // Draw mass on the plane offg.setColor(Color.lightGray); xBox[0]=(int)(m1LeftEdge*Math.cos(ang*Math.PI/180.0)); yBox[0]=250-((int)((double)(xBox[0]-xCoords[0])*myTan)); //+0.5)); xBox[1]=xBox[0]+(int)(60.0*myCos); //+0.5); yBox[1]=yBox[0]-(int)(60.0*mySin); xBox[2]=xBox[0]+(int)(Math.sqrt(2.0)*60.0*Math.cos(Math.PI*(ang+45.0)/180.0)); //+0.5); yBox[2]=yBox[0]-(int)(Math.sqrt(2.0)*60.0*Math.sin(Math.PI*(ang+45.0)/180.0)); //+0.5); xBox[3]=xBox[0]+(int)(60.0*myCos90); //+0.5); yBox[3]=yBox[0]-(int)(60.0*mySin90); //+0.5); xBox[4]=xBox[0]; yBox[4]=yBox[0]; offg.fillPolygon(xBox,yBox,5); offg.setColor(Color.black); offg.drawPolygon(xBox,yBox,5); // Draw the force vectors on M1 //M1g offg.setColor(Color.red); vx=(xBox[0]+xBox[2])/2; vy=(yBox[0]+yBox[2])/2 + 1; offg.drawLine(vx,vy,vx,vy+(int)(2*m1)); offg.drawLine(vx,vy+(int)(2*m1),vx+2,vy+(int)(2*m1)-5); offg.drawLine(vx,vy+(int)(2*m1),vx-2,vy+(int)(2*m1)-5); offg.drawString("M g",vx+5,vy+(int)(2*m1)+5); vxx=(int)(2.0*m1*myCos*myCos90); vyy=(int)(2.0*m1*myCos*mySin90); offg.setColor(Color.blue); offg.drawString("N",vx+vxx-5,vy-vyy-5); drawMyArrow(offg,ang+90.0,vx+vxx,vy-vyy); offg.drawLine(vx,vy,vx+vxx,vy-vyy); offg.setColor(Color.black); drawMyArrow(offg,ang-90.0,vx-vxx,vy+vyy); // offg.drawLine(vx,vy,vx-vxx,vy+vyy); offg.drawString("M g cos(phi)",vx-vxx+5,vy+vyy+5); // vxx=(int)(2.0*m1*mySin*mySin90); vyy=(int)(2.0*m1*mySin*myCos90); drawMyArrow(offg,ang+180,vx-vxx,vy-vyy); offg.drawLine(vx,vy,vx-vxx,vy-vyy); offg.drawString("M g sin(phi)",vx-vxx-60,vy-vyy+5); // // Now force of friction if(coefk > 0.0){ offg.setColor(Color.red); vxx=(int)(2.0*coefk*m1*myCos*myCos); vyy=(int)(2.0*coefk*m1*myCos*mySin); if(accel > 0.0){ offg.drawLine(xBox[0],yBox[0],xBox[0]-vxx,yBox[0]+vyy); drawMyArrow(offg,ang+180.0,xBox[0]-vxx,yBox[0]+vyy); offg.drawString("Fk",xBox[0]-vxx-10,yBox[0]+vyy-3); } else if (accel < 0.0){ offg.drawLine(xBox[1],yBox[1],xBox[1]+vxx,yBox[1]-vyy); drawMyArrow(offg,ang,xBox[1]+vxx,yBox[1]-vyy); offg.drawString("Fk",xBox[1]+vxx+4,yBox[1]-vyy-6); } } // Draw the hand if(doHand){ offg.drawImage(handImage,xBox[3]-23,yBox[3]-18,this); } } public void drawMyArrow(Graphics g,double theta,int xx,int yy){ double arrowTheta=158.2; double d=5.385; int xn,yn; // Draw the arrow head // Top half xn=xx+(int)(d*Math.cos(Math.PI*(theta+arrowTheta)/180.0)); yn=yy-(int)(d*Math.sin(Math.PI*(theta+arrowTheta)/180.0)); g.drawLine(xx,yy,xn,yn); // Bottom half xn=xx+(int)(d*Math.cos(Math.PI*(theta-arrowTheta)/180.0)); yn=yy-(int)(d*Math.sin(Math.PI*(theta-arrowTheta)/180.0)); g.drawLine(xx,yy,xn,yn); } }