/* * fosb.java - revised 9 Jan 08 - width 381, height 241 * an undamped oscillator driven at its natural frequency * @author jackord@kw.igs.net */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class fosb extends Applet implements ActionListener { int kk=0; // Declarations String bs="Plot"; Button b=new Button(bs); Image bim; Graphics bgr; public void init() { setBackground(new Color(211, 211, 211)); // Light Gray Background add(b); b.addActionListener(this); } public void paint(Graphics g) { // Paint int x1, x2, y1, y2, z1, z2, s1, s2, r, d, del; double m, k, f, ff, w, dt, x, v, pi, q; int [] xs=new int[121]; // Spring drawing x's int [] xa=new int[7]; // Arrow drawing x's int [] ya=new int[7]; // Arrow drawing y's m=2; k=8; f=6; x1=20; y1=120; z1=120; s1=0; r=6; w=Math.sqrt(k/m); pi=Math.PI; dt=pi/100; for (int i=0; i<=120; i=i+1) { xs[i]=10+(int)(8*Math.sin(i*pi/10)); } xa[0]=9; xa[1]=6; xa[2]=10; xa[3]=14; xa[4]=11; xa[5]=11; xa[6]=9; g.setColor(Color.black); g.drawRect(0, 0, 380, 240); g.drawLine(20, 120, 375, 120); g.setColor(Color.red); // Draw mass g.fillArc(10-r, z1-r, 2*r, 2*r, 0, 360); g.setColor(Color.black); // Draw spring for (int i=1; i<=120; i=i+1) { g.drawLine(xs[i-1], 241-i, xs[i], 240-i); } if (kk==1) { bim=createImage(19, 241); // Animation buffer bgr=bim.getGraphics(); bgr.drawLine(0, 0, 18, 0); bgr.drawLine(0, 240, 18, 240); bgr.drawLine(0, 0, 0, 240); x=0; v=0; // Feynman algorithm long tt=System.currentTimeMillis(); del=12; for (int jt=1; jt<=710; jt=jt+1) { ff=f*Math.sin(w*jt*dt); x=x+v*dt; v=v+(ff-k*x)/m*dt; x2=20+jt/2; d=(int)(5*ff); y2=120-d; z2=120-(int)(5*x); g.setColor(Color.blue); g.drawLine(x1, y1, x2, y2); g.setColor(Color.red); g.drawLine(x1, z1, x2, z2); x1=x2; y1=y2; z1=z2; bgr.setColor(getBackground()); // Clear plot buffer bgr.fillRect(1, 1, 18, 239); bgr.setColor(Color.red); // Draw mass bgr.fillArc(10-r, z2-r, 2*r, 2*r, 0, 360); bgr.setColor(Color.black); s1=240; // Draw spring for (int i=1; i<=120; i=i+1) { s2=240-(int)((120+5*x)*i/120); bgr.drawLine(xs[i-1], s1, xs[i], s2); s1=s2; } if (Math.abs(d)>3) { // Draw arrow ya[0]=z2-d+4*Math.abs(d)/d; ya[1]=ya[0]; ya[3]=ya[0]; ya[4]=ya[0]; ya[2]=z2-d; ya[5]=z2; ya[6]=ya[5]; bgr.setColor(Color.blue); bgr.drawPolygon(xa, ya, 7); } g.drawImage(bim, 0, 0, null); // Show plot buffer tt=tt+del; while (tt>System.currentTimeMillis()) { } } kk=0; } } public void actionPerformed(ActionEvent e) { // Button String tst; tst=e.getActionCommand(); if (bs.equals(tst)) { kk=1; repaint(); } } }