/* * fplw.java - revised 14 Nov 2010- width 401, height 401 * central-force motion in a power-law force field F=-k*r^n * for n = 1, 0, -1, -2, -3, and -5 * @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; // Declarations String b1s="Plot"; Button b1=new Button(b1s); Choice ch=new Choice(); public void init() { ch.addItem("n=1"); ch.addItem("n=0"); ch.addItem("n=-1"); ch.addItem("n=-2"); ch.addItem("n=-3"); ch.addItem("n=-5"); add(b1); add(ch); b1.addActionListener(this); setBackground(new Color(211, 211, 211)); ch.setBackground(getBackground()); } public void paint(Graphics g) { double t, dt, x, dx, y, dy, vx, vy, xo, yo, r, rm, rmin; int n, x1, y1, x2, y2, test; g.setFont(new Font("Arial", Font.BOLD, 14)); g.drawString("F = - k*r^n", 6, 18); g.setFont(new Font("Arial", Font.PLAIN, 12)); ch.setBounds(6, 24, 50, 20); // screen layout b1.setBounds(60, 24, 30, 20); g.drawRect(0, 0, 400, 400); 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); n=1-ch.getSelectedIndex(); if (n==-4) { n=-5; } dt=.0001; test=1; rmin=0; xo=0; yo=0; t=0; x=1; y=0; vx=0; vy=.488; x1=380; y1=200; if (n==-3) { vy=.999; } if (n==-5) { vy=1/Math.sqrt(2); } if (kk==1) { r=Math.sqrt(x*x+y*y); vx=vx-dt/2; // ax=-1, ay=0 at t=0 do { // Feynman loop if (test==1) { xo=x; yo=y; rmin=r; } 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; r=Math.sqrt(x*x+y*y); rm=Math.pow(r, n-1); vx=vx-x*rm*dt; vy=vy-y*rm*dt; if (r>rmin) { test=0; } } while ((t<47.3)&&(180*r>18)); if (n>-2.5) { g.setColor(Color.magenta); g.fillArc(x2-5, y2-5, 11, 11, 0, 360); g.setColor(Color.black); g.drawString("Apsidal Angle = "+(float)(180* (1+Math.atan(yo/xo)/Math.PI))+" degrees", 4, 394); g.drawString("Rmin = "+(float)(rmin), 250, 394); } if (n==-3) { g.setColor(Color.black); g.drawString("Vinit = 0.999", 4, 394); } if (n==-5) { t=0; x=1; y=0; vx=-dt/2; vy=-1/Math.sqrt(2); x1=380; y1=200; do { 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; r=Math.sqrt(x*x+y*y); rm=Math.pow(r, n-1); vx=vx-x*rm*dt; vy=vy-y*rm*dt; } while (180*r>18); g.setColor(Color.black); g.drawString("Vinit = 1/sqrt(2)", 4, 394); } } kk=0; } public void actionPerformed(ActionEvent e) { // button String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { kk=1; } repaint(); } }