/* * nm1.java - revised 23 Nov 07 width 526, height 231 * @author jackord@kw.igs.net * string of length L, clamped at both ends * "Pluck" and "Pulse" initial displacements * motion generated by superposition of waves of wavelength 2L * travelling in opposite directions * y(L/4) is plotted versus time */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class nm1 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); Image bim; Graphics bgr; Thread tmr; public void init() { add(b1); add(b2); add(b3); b1.addActionListener(this); b2.addActionListener(this); b3.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; plt=0; } public void run() { repaint(); } public void paint(Graphics g) { int n=384; int jt, ia, ib, x1, y1, x2, y2, r, del, delt; // Declarations int [] xx=new int[n+1]; int [] yy=new int[n+1]; int [] y=new int[2*n+1]; g.setColor(Color.black); g.drawRect(0, 0, 525, 230); g.drawLine(393, 129, 521, 129); // y[L/4] plot axis g.setColor(Color.red); if (first==0) { bim=createImage(n+1, 200); // Animation buffer bgr=bim.getGraphics(); first=1; } x1=0; y1=0; x2=0; y2=0; r=4; if (kk>0) { for (int i=0; i<=n; i=i+1) { xx[i]=i; } if (kk==1) { // Init pluck for (int i=0; i<=n/2; i=i+1) { y[i]=i; y[n-i]=y[i]; } } else { // Init pulse for (int i=3*n/8; i<=n/2; i=i+1) { y[i]=4*(i-3*n/8); y[n-i]=y[i]; } } for (int i=0; i<=n; i=i+1) { // Extend wave to 2L y[n+i]=-y[i]; } long tt=System.currentTimeMillis(); del=20; jt=0; do { // Main loop bgr.setColor(getBackground()); // Clear plot buffer bgr.fillRect(0, 0, 384, 249); bgr.setColor(Color.black); bgr.drawLine(0, 80, 0, 120); bgr.drawLine(384, 80, 384, 120); bgr.setColor(Color.blue); for (int i=0; i<=n; i=i+1) { ia=i+jt; while (ia>2*n) { ia=ia-2*n; } // Wave moving left ib=i-jt; while (ib<0) { ib=ib+2*n; } // Wave moving right yy[i]=100-(y[ia]+y[ib])/4; // Superposition } bgr.drawPolyline(xx, yy, n); // Draw string bgr.setColor(Color.red); bgr.fillArc(xx[n/4]-r, yy[n/4]-r, 2*r+1, 2*r+1, 0, 360); x2=393+2*jt/12; y2=yy[n/4]+29; if (jt>0) { g.drawLine(x1, y1, x2, y2); // Plot y[L/4] vs time } x1=x2; y1=y2; g.drawImage(bim, 4, 29, null); // Show plot buffer if (tt>System.currentTimeMillis()) { delt=del; } else { delt=0; } try { Thread.sleep(delt); } catch (InterruptedException e) { stop(); } tt=tt+del; jt=jt+1; if (jt==2*n+1) { kk=0; plt=0; } } while (plt==1); } stop(); } public void actionPerformed(ActionEvent e) { // Buttons String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { kk=1; start(); } if (b2s.equals(tst)) { kk=2; start(); } if (b3s.equals(tst)) { if (kk>0) { plt=1; } start(); } } }