/* * fosa.java - revised 16 Jan 08 - width 425, height 321 * identical masses hanging from springs one below the other * they are released from rest with the upper spring unstretched and * Case 1: with the lower mass supported by the lower spring * Case 2: with the lower mass raised enough to generate the * low-frequency normal mode * Case 3: with the lower mass pulled down enough to generate the * high-frequency normal mode * each spring (length 3*d) stretches d under the weight of one mass * @author jackord@kw.igs.net */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class fosa extends Applet implements ActionListener { int kk=0; int plt=0; int first=0; // Declarations double dt=.1; double d, ya, va, aa, yb, vb, ab; String b1s="Case1"; Button b1=new Button(b1s); String b2s="Case2"; Button b2=new Button(b2s); String b3s="Case3"; Button b3=new Button(b3s); String b4s="Motion"; Button b4=new Button(b4s); Image bim; Graphics bgr; public void init() { setBackground(new Color(211, 211, 211)); // Light Gray Background add(b1); add(b2); add(b3); add(b4); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); } double td(double yt) { double t, ta, tb, vb1; t=0; ya=3*d; yb=yt; ta=0; tb=0; aa=d-2*ya+yb; ab=4*d-yb+ya; va=aa*dt/2; vb=ab*dt/2; vb1=vb; // Feynman half-step do { t=t+dt; ya=ya+va*dt; yb=yb+vb*dt; // Step ya, yb aa=d-2*ya+yb; ab=4*d-yb+ya; // Newton's law va=va+aa*dt; vb=vb+ab*dt; // Step va, vb if ((ta==0)&&(va<0)) { ta=t+dt/2-va/aa; } if ((tb==0)&&(vb*vb1<0)) { tb=t+dt/2-vb/ab; } } while ((ta==0)||(tb==0)); return Math.abs(ta-tb); } public void paint(Graphics g) { // Paint int ya1, ya2, yb1, yb2, s1, s2, r, del, j; double pi, dd, yt, x, xo; long tt; int [] xs=new int[121]; // Spring drawing x's g.setColor(Color.black); g.drawRect(0, 0, 424, 320); r=6; d=25; dd=d/4; pi=Math.PI; for (int i=0; i<=120; i=i+1) { xs[i]=10+(int)(8*Math.sin(i*pi/10)); } if (first==0) { bim=createImage(19, 321); // Animation buffer bgr=bim.getGraphics(); first=1; } if (kk>0) { if (kk==1) { yb=7*d; } if (kk>1) { yt=(4*kk-2)*d; x=td(yt); do { yt=yt+dd; xo=x; x=td(yt); if (x>xo) { dd=-dd/2; } } while (Math.abs(dd)>.02); yb=yt+2*dd; g.drawString("Amplitude Ratio = "+(int)(1000*(9-yb/d))/2000., 145, 50); } j=20; ya=3*d; ya1=(int)(ya); yb1=(int)(yb); aa=d-2*ya+yb; ab=4*d-yb+ya; va=aa*dt/2; vb=ab*dt/2; // Feynman half-step tt=System.currentTimeMillis(); del=20; g.drawLine(20, 125, 420, 125); g.drawLine(20, 225, 420, 225); do { ya2=(int)(ya); yb2=(int)(yb); if (j>20) { g.setColor(Color.blue); g.drawLine(j-1, ya1, j, ya2); g.setColor(Color.red); g.drawLine(j-1, yb1, j, yb2); } ya1=ya2; yb1=yb2; bgr.setColor(getBackground()); // Clear plot buffer bgr.fillRect(1, 1, 18, 319); bgr.setColor(Color.black); bgr.drawLine(0, 0, 0, 320); bgr.drawLine(0, 0, 18, 0); bgr.drawLine(0, 320, 18, 320); s1=0; for (int i=1; i<=120; i=i+1) { // Draw upper spring s2=(int)(ya*i/120); bgr.drawLine(xs[i-1], s1, xs[i], s2); s1=s2; } for (int i=1; i<=120; i=i+1) { // Draw lower spring s2=(int)(ya+(yb-ya)*i/120); bgr.drawLine(xs[i-1], s1, xs[i], s2); s1=s2; } bgr.setColor(Color.blue); // Draw mass A bgr.fillArc(10-r, ya2-r, 2*r, 2*r, 0, 360); bgr.setColor(Color.red); // Draw mass B bgr.fillArc(10-r, yb2-r, 2*r, 2*r, 0, 360); g.drawImage(bim, 0, 0, null); // Show plot buffer ya=ya+va*dt; yb=yb+vb*dt; // Step ya, yb aa=d-2*ya+yb; ab=4*d-yb+ya; // Newton's law va=va+aa*dt; vb=vb+ab*dt; // Step va, vb tt=tt+del; while (tt>System.currentTimeMillis()) { } j=j+1; if (j==421) { kk=0; plt=0; } } while (plt==1); } } public void actionPerformed(ActionEvent e) { // Buttons String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { kk=1; } if (b2s.equals(tst)) { kk=2; } if (b3s.equals(tst)) { kk=3; } if (b4s.equals(tst)) { if (kk>0) { plt=1; } } repaint(); } }