// Spring.java spring and mass import java.awt.*; import java.awt.event.*; public class Spring extends Frame { int xm, ym, bm; // mouse position int xc = 300; // disc/rectangle location int yc = 150; int xp; // last mouse location during drag int yp; boolean tracking = false; // tracking mouse motion boolean loose = false; // loose to move double x = 0.0; // integrated position double vx = 0.0; // integrated velocity boolean double_k1 = false; // yellow buttons boolean double_k2 = false; boolean disc = true; Spring() { System.out.println("Spring.java running"); setTitle("Spring"); setSize(600,400); setBackground(Color.white); setForeground(Color.black); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); this.addMouseListener (new mousePressHandler()); this.addMouseListener (new mouseReleaseHandler()); this.addMouseMotionListener (new mouseMotionHandler()); new MyThread("physics").start(); } boolean in_rect(int x, int y, int w, int h) { return xm>x && xmy && ym 1.5 not very visible f2 = (double)(580-xc)/280.0; while(t<32.0*Math.PI) // heuristic { t = t+0.1; // 0.05 flicker yn = (b - b*Math.cos(t)); xs = b*Math.sin(t); xxn = f1*a*t + xs; // if(double_k1) g.setLineWidth(2); ?? no such function // if(!double_k1) g.setLineWidth(1); g.drawLine(20+(int)(xx *35.0), 150+(int)(y *200.0), 20+(int)(xxn*35.0), 150+(int)(yn*200.0)); xn = f2*a*t + xs; // if(double_k2) g.setLineWidth(2); // if(!double_k2) g.setLineWidth(1); g.drawLine(xc+(int)(x *35.0), 150+(int)(y* 200.0), xc+(int)(xn*35.0), 150+(int)(yn*200.0)); xx = xxn; x = xn; y = yn; } } public static void main(String args[]) { new Spring(); } }