import java.awt.*; // export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/fortran/lib:/usr/local/fortran/lib64 // /usr/local/fortran/bin/gcj --main=RungeKutta RungeKutta.java -o RK public class RungeKutta extends Frame implements Runnable { int x,y; //window coordinates of the mass double h=0.02; // time increment double q=1.0; //initial data double p=0.0; double k1,k2,l1,l2,k3,l3; // RK variables int frameNumber = -1; Thread RKThread; boolean stopped = false; RungeKutta(int fps, String windowTitle) { super(windowTitle); } public void startRK() { if (stopped) { } else { if (RKThread == null) { RKThread = new Thread(this); } RKThread.start(); } } public void stopRK() { RKThread = null; } public boolean mouseDown(Event e, int x, int y) { if (stopped) { stopped = false; startRK(); } else { stopped = true; stopRK(); } return true; } public void run() { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); Thread currentThread = Thread.currentThread(); //All of the work is done here while (currentThread == RKThread) { frameNumber++; double t=h*(double)frameNumber; k1=h*p; l1=-2.0*h*q*q*q; k2=h*(p+0.5*l1); l2=-2.0*h*(q+0.5*k1)*(q+0.5*k1)*(q+0.5*k1); k3=h*(p+l2); l3=-2.0*h*(q+k2)*(q+k2)*(q+k2); q=q+(k1+4.0*k2+k3)/6.0; p=p+(l1+4.0*l2+l3)/6.0; x=(int)(40.0*q); y=(int)(40.0*p); repaint(); try {Thread.sleep(5); } catch (InterruptedException e){break;} } } public void paint(Graphics g) { g.setColor(Color.white); g.drawRect(0,0,250,250); g.setColor(Color.black); g.drawLine(125, 30, 125, 220); g.drawLine(30, 125, 220, 125); g.drawString("q",210, 135); g.drawString("p",130, 40); g.setColor(Color.red); g.drawOval(125+x,125+y,4,4); } public static void main(String args[]) { RungeKutta RK = null; int fps = 60; // lots of frames per second RK = new RungeKutta(fps, "Oscillator"); RK.setSize(250, 250); RK.show(); RK.startRK(); } }