/* * fplw.java - revised 26 Mar 08 - width 401, height 401 * central-force motion in a power-law force field * @author jackord@kw.igs.net */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class fplw extends Applet implements ActionListener { int kk=0; int first=0; // Declarations String b1s="-2"; Button b1=new Button(b1s); String b2s="-1"; Button b2=new Button(b2s); String b3s="+1"; Button b3=new Button(b3s); 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 paint(Graphics g) { double t, dt, x, dx, y, dy, vx, vy, xo, yo, rm, rmin, rc; int x1, y1, x2, y2, test, loop; if (first==0) { b1.setBounds(6, 4, 26, 18); // screen layout b2.setBounds(42, 4, 26, 18); b3.setBounds(78, 4, 26, 18); g.setFont(new Font("TimesRoman", Font.PLAIN, 14)); first=1; } pin(g); dt=.001; test=1; loop=1; rmin=0; rc=0; xo=0; yo=0; t=0; x=1; y=0; vx=0; vy=.488; x1=380; y1=200; long tt=System.currentTimeMillis(); if (kk==-2) { // gravity rm=Math.pow(x*x+y*y, 1.5); vx=vx-x/rm*dt/2; vy=vy-y/rm*dt/2; do { // Feynman loop xo=x; yo=y; t=t+dt; x=x+vx*dt; y=y+vy*dt; if ((test==1)&&(y<=0)&&(yo>0)) { rmin=Math.abs(x-vx*y/vy); test=0; } x2=200+(int)(180*x+.5); y2=200-(int)(180*y+.5); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; rm=Math.pow(x*x+y*y, 1.5); vx=vx-x/rm*dt; vy=vy-y/rm*dt; if ((y>=0)&&(yo<0)) { loop=0; } while ((System.currentTimeMillis()-tt)<3000*t) { } } while (loop==1); t=t-y/vy; rc=x-vx*y/vy; g.setColor(Color.black); g.drawString("Gravity: n=-2", 4, 314); g.drawString("Closure Test: Rmax = "+(float)(rc), 4, 330); g.drawString("Closest Approach: Rmin = "+(float)(rmin), 4, 346); g.drawString("Apsidal Angle = 180 degrees", 4, 362); g.drawString("Torbit/Tcircle = "+(float)(t/2/Math.PI), 4, 378); g.drawString("Kepler's Law a^1.5 = "+(float)(Math.pow((rc+rmin)/2, 1.5))+" where a = (Rmin+Rmax)/2", 4, 394); } if (kk==-1) { // inverse R rm=x*x+y*y; vx=vx-x/rm*dt/2; vy=vy-y/rm*dt/2; do { // Feynman loop if (test==1) { xo=x; yo=y; rmin=rm; } t=t+dt; x=x+vx*dt; y=y+vy*dt; x2=200+(int)(180*x+.5); y2=200-(int)(180*y+.5); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; rm=x*x+y*y; vx=vx-x/rm*dt; vy=vy-y/rm*dt; if (rm>rmin) { test=0; } while ((System.currentTimeMillis()-tt)<500*t) { } } while (t<47.5); rmin=Math.sqrt(rmin); g.setColor(Color.black); g.drawString("Inverse R: n=-1", 4, 362); g.drawString("Rmin = "+(float)(rmin), 4, 378); g.drawString("Apsidal Angle = "+(float)(180* (1+Math.atan(yo/xo)/Math.PI))+" = 360*11/32 degrees", 4, 394); } if (kk==1) { // spring vx=vx-x*dt/2; vy=vy-y*dt/2; do { // Feynman loop xo=x; yo=y; t=t+dt; x=x+vx*dt; y=y+vy*dt; if ((test==1)&&(x<=0)&&(xo>0)) { rmin=y-vy*x/vx; test=0; } x2=200+(int)(180*x+.5); y2=200-(int)(180*y+.5); g.drawLine(x1,y1,x2,y2); x1=x2; y1=y2; vx=vx-x*dt; vy=vy-y*dt; if ((y>=0)&&(yo<0)) { loop=0; } while ((System.currentTimeMillis()-tt)<1300*t) { } } while (loop==1); t=t-y/vy; rc=x-vx*y/vy; g.setColor(Color.black); g.drawString("Spring: n=+1", 4, 330); g.drawString("Closure Test: Rmax = "+(float)(rc), 4, 346); g.drawString("Closest Approach : Rmin = "+(float)(rmin), 4, 362); g.drawString("Apsidal Angle = 90 degrees", 4, 378); g.drawString("Torbit/Tcircle = "+(float)(t/2/Math.PI), 4, 394); } kk=0; } public void pin(Graphics g) { g.drawRect(0, 0, 400, 400); g.drawString("Select Power", 16, 36); g.setColor(Color.green); g.fillArc(182, 182, 37, 37, 0, 360); g.setColor(Color.red); g.fillArc(375, 195, 11, 11, 0, 360); g.setColor(Color.black); g.drawLine(377, 176, 380, 170); g.drawLine(380, 200, 380, 170); g.drawLine(383, 176, 380, 170); g.setColor(Color.blue); } public void actionPerformed(ActionEvent e) { // buttons String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { kk=-2; } if (b2s.equals(tst)) { kk=-1; } if (b3s.equals(tst)) { kk=1; } repaint(); } }