/* * nm2.java - revised 25 Nov 07 - width 526, height 261 * @author jackord@kw.igs.net * motion of 8-segment string clamped at both ends * time dependence from Newton's law * 'Pluck', 'Pulse', and Normal Mode initial displacements * y[n/4] plotted versus time for Pluck and Pulse displacements * dispersion relation plotted for the Normal Modes */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class nm2 extends Applet implements ActionListener, Runnable { int kk=0; int plt=0; int first=0; // Declarations String b1s="Pluck"; Button b1=new Button(b1s); String b2s="Pulse"; Button b2=new Button(b2s); String b3s="Motion"; Button b3=new Button(b3s); String b4s="Nmodes"; Button b4=new Button(b4s); Image bim; Graphics bgr; Thread tmr; public void init() { add(b1); add(b2); add(b3); add(b4); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); setBackground(new Color(211, 211, 211)); } public void start() { if (tmr==null) { tmr=new Thread(this); tmr.setPriority(Thread.MIN_PRIORITY); tmr.start(); } } public void stop() { tmr=null; } public void run() { repaint(); } public void wait(int del) { try { Thread.sleep(del); } catch (InterruptedException e) { stop(); } } public void paint(Graphics g) { int n=8; int mode=0; long tt; // Declarations int [] xx=new int[n+1]; int [] yy=new int[n+1]; double [] y=new double[n+1]; double [] v=new double[n+1]; int x1, y1, x2, y2, r, jt, tst, del; double t, tf, dt, pi; x1=0; y1=0; x2=0; y2=0; pi=Math.PI; g.setColor(Color.black); g.drawRect(0, 0, 525, 260); if (first==0) { bim=createImage(393, 230); // Animation buffer bgr=bim.getGraphics(); first=1; } for (int i=0; i<=n; i=i+1) { xx[i]=4+384/n*i; } if ((kk==1)||(kk==2)) { g.drawLine(395, 144, 523, 144); // y[n/4] plot axis g.setColor(Color.red); dt=0.1*n/768; if (kk==1) { // Init pluck for (int i=0; i<=n/2; i=i+1) { y[i]=224.*i/n; y[n-i]=y[i]; } } if (kk==2) { // Init pulse for (int i=3*n/8; i<=n/2; i=i+1) { y[i]=896.*(i-3*n/8)/n; y[n-i]=y[i]; } } for (int i=1; i<=n-1; i=i+1) { // Feynman half-step v[i]=v[i]+(y[i-1]+y[i+1]-2*y[i])*dt/2; } jt=0; x1=395; y1=144-(int)(y[n/4]); r=4; tt=System.currentTimeMillis(); del=12; do { // Main loop x2=395+jt/12; y2=144-(int)(y[n/4]); if (x2>x1) { g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; // Plot y[n/4] versus t } bgr.setColor(getBackground()); // Clear plot buffer bgr.fillRect(0, 0, 393, 230); bgr.setColor(Color.black); bgr.drawLine(4, 95, 4, 135); bgr.drawLine(388, 95, 388, 135); bgr.setColor(Color.blue); for (int i=0; i<=n; i=i+1) { // Draw blue string yy[i]=115-(int)(y[i]+.5); } bgr.drawPolyline(xx, yy, n+1); if (n<96) { for (int i=0; i<=n; i=i+1) { if (i==n/4) { bgr.setColor(Color.red); } bgr.fillArc(xx[i]-r, yy[i]-r, 2*r+1, 2*r+1, 0, 360); if (i==n/4) { bgr.setColor(Color.blue); } } } g.drawImage(bim, 1, 29, null); // Show plot buffer jt=jt+1; // Step time for (int j=1; j<=10; j=j+1) { for (int i=1; i<=n-1; i=i+1) { // Step y[] y[i]=y[i]+v[i]*dt; } for (int i=1; i<=n-1; i=i+1) { // Step v[] (F=ma) v[i]=v[i]+(y[i-1]+y[i+1]-2*y[i])*dt; } } if (tt>System.currentTimeMillis()) { wait(del); } tt=tt+del; if (jt==1537) { kk=0; plt=0; } } while (plt==1); // Close main loop } if (kk==3) { g.drawLine(395, 144, 523, 144); g.drawLine(395, 16, 395, 144); g.drawLine(395, 16, 403, 16); g.drawString("0.4 Hz", 407, 21); g.drawString("Mode", 395+64-12, 176); do { // Normal mode loop mode=mode+1; t=0; tf=10; dt=0.05/mode; tst=1; r=4; for (int i=0; i<=n; i=i+1) { y[i]=108*Math.sin(pi*mode*i/n); } for (int i=1; i<=n-1; i=i+1) { // Feynman half-step v[i]=(y[i-1]+y[i+1]-2*y[i])*dt/2; } tt=System.currentTimeMillis(); del=30; do { // Main loop bgr.setColor(getBackground()); // Clear plot buffer bgr.fillRect(0, 0, 392, 229); bgr.setColor(Color.black); bgr.drawLine(4, 95, 4, 135); bgr.drawLine(388, 95, 388, 135); bgr.setColor(Color.blue); for (int i=0; i<=n; i=i+1) { // Draw blue string yy[i]=115-(int)(y[i]+.5); bgr.fillArc(xx[i]-r, yy[i]-r, 2*r+1, 2*r+1, 0, 360); } bgr.drawPolyline(xx, yy, n+1); g.drawImage(bim, 1, 29, null); // Show plot buffer if (t==0) { wait(1000); tt=tt+1000; } t=t+dt; // Step time for (int i=1; i<=n-1; i=i+1) { // Step y[] y[i]=y[i]+v[i]*dt; } for (int i=1; i<=n-1; i=i+1) { // Step v[] (F=ma) v[i]=v[i]+(y[i-1]+y[i+1]-2*y[i])*dt; } if ((tst==1)&&(y[1]<0)) { tf=4*(t-y[1]/v[1]); tst=0; } if (tt>System.currentTimeMillis()) { wait(del); } tt=tt+del; } while (t