/** * rungekutta.java * * Title: rungekutta * worked on problem with Dan Chak */ package rungekutta; import java.awt.*; import java.awt.event.*; import java.applet.*; public class rungekutta extends java.applet.Applet implements Runnable{ // IMPORTANT: Source code between BEGIN/END comment pair will be regenerated // every time the form is saved. All manual changes will be overwritten. // BEGIN GENERATED CODE // member declarations // END GENERATED CODE boolean isStandalone = false; Dimension size; Image buffer; // The off-screen image for double-buffering Graphics bufferGraphics; // A Graphics object for the buffer Thread animator; // Thread that performs the animation boolean please_stop; float stepper = 0; float[] yStart = new float[2]; float dt; float t; float stopt; float[] yEnd = new float[2];; int last_x; int last_y; int last_mx; int last_my; int colCount; Color[] col = new Color[100]; boolean clear; public rungekutta() { } public float[] calculate(float h, float xStart, float yStart[], float yEnd[]) { float[] k1 = new float[2]; float[] k2 = new float[2]; float[] k3 = new float[2]; float[] k4 = new float[2]; float[] dydxk1 = new float[2]; float[] dydxk2 = new float[2]; float[] dydxk3 = new float[2]; float[] dydxk4 = new float[2]; float[] yStartk2 = new float[2]; float[] yStartk3 = new float[2]; float[] yStartk4 = new float[2]; // calculate k1 = h * f(x, y(x)) // find f(x, y(x)) dydxk1 = dydx(xStart, yStart, dydxk1); for (int i = 0; i < 2; i++) { k1[i] = h * dydxk1[i]; } // calculate k2 = h * f(x + h/2, y(x) + k1/2) // find f(x + h/2, y(x) + k1/2) for (int i = 0; i < 2; i++) { yStartk2[i] = yStart[i] + k1[i] / 2.0f; } dydxk2 = dydx(xStart + h/2.0f, yStartk2, dydxk2); for (int i = 0; i < 2; i++) { k2[i] = h * dydxk2[i]; } // calculate k3 = h * f(x + h/2, y(x) + k2/2) for (int i = 0; i < 2; i++) { yStartk3[i] = yStart[i] + k2[i] / 2.0f; } dydxk3 = dydx(xStart + h/2.0f, yStartk3, dydxk3); for (int i = 0; i < 2; i++) { k3[i] = h * dydxk3[i]; } // calculate k4 = h * f(x + h, y(x) + k3 for (int i = 0; i < 2; i++) { yStartk4[i] = yStart[i] + k3[i]; } dydxk4 = dydx(xStart + h, yStartk4, dydxk4); for (int i = 0; i < 2; i++) { k4[i] = h * dydxk4[i]; } // calculate y(x + h) for (int i = 0; i < 2; i++) { yEnd[i] = yStart[i] + k1[i] / 6.0f + k2[i] / 3.0f + k3[i] / 3.0f + k4[i] / 6.0f; } return yEnd; } public float[] dydx(float x, float y[], float dydx[]){ // y[0] is position // y[1] is velocity, dy[0]/dx // d/dx(dy[0]/dx) = acceleration = - y[0] float acceleration = -1.0f * y[0]; dydx[0] = y[1]; dydx[1] = acceleration; return dydx; } // Retrieve the value of an applet parameter public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } // Get info on the applet parameters public String[][] getParameterInfo() { return null; } // Get applet information public String getAppletInfo() { return "Applet Information"; } // Initialize the applet public void init() { try { initComponents(); } catch (Exception e) { e.printStackTrace(); } // double buffering size = this.size(); buffer = this.createImage(size.width, size.height); bufferGraphics = buffer.getGraphics(); please_stop = false; yStart = new float[2]; yStart[0] = 1; yStart[1] = 0; dt = 0.01f; t = 0; stopt = (float)(100.0f*Math.PI); colCount = 0; last_x=100; last_y=300; clear = true; for(int i=0;i<100;i++) { col[i] = new Color((float)Math.random(),(float)Math.random(),(float)Math.random()); } //yEnd = new float[2]; this.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { last_mx = e.getX(); last_my = e.getY(); if ((last_mx>400)&&(last_mx<410)&&(last_my>300)&&(last_my<310)) { clear = true; } } }); // event model 1.1 mouse listener this.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { int x = e.getX(), y = e.getY(); if ((x>100)&&(x<200)&&(y>300)&&(y<310)){ //last_x = Math.min(x,800); //last_y = Math.min(y,600); dt = (x-99)/100.0f; last_x = x; last_y = y; } } }); } public void initComponents() throws Exception { // IMPORTANT: Source code between BEGIN/END comment pair will be regenerated // every time the form is saved. All manual changes will be overwritten. // BEGIN GENERATED CODE // the following code sets the frame's initial state setLocation(new java.awt.Point(0, 0)); setLayout(null); setSize(new java.awt.Dimension(800, 400)); // event handling // END GENERATED CODE } public void paint(Graphics screen) { if (clear) { bufferGraphics.setColor(Color.white); bufferGraphics.fillRect(0, 0, size.width, size.height); // clear the buffer clear = false; } bufferGraphics.setColor(col[colCount]); bufferGraphics.drawRect((int)(t*2.0f)+30,(int)(yEnd[0]*100.0f)+150,1,1); bufferGraphics.setColor(Color.white); bufferGraphics.fillRect(30,270, 400, 50); bufferGraphics.setColor(Color.black); bufferGraphics.drawLine(30,150,658,150); bufferGraphics.drawRect(30,50,628,200); bufferGraphics.drawString(" 0.0",5,150); bufferGraphics.drawString(" 0.5",5,100); bufferGraphics.drawString("-0.5",5,200); bufferGraphics.drawString(" 1.0",5,50); bufferGraphics.drawString("-1.0",5,250); bufferGraphics.drawRect(400,300,10,10); bufferGraphics.drawString(" clear screen", 410,310); bufferGraphics.drawRect(100,300,100,10); bufferGraphics.fillRect(last_x-1,300,2,10); bufferGraphics.drawString("Stepsize: " + String.valueOf(dt),last_x-1,295); //draw screenbuffer screen.drawImage(buffer, 0, 0, this); } // this one is for updating during runtime Graphics screeng; public void update() { if (screeng == null) screeng = this.getGraphics(); paint(screeng); } public void run() { while(!please_stop) { while (true){ if (t> stopt) { yStart[0] = 1; yStart[1] = 0; dt = 0.01f; last_x =(int)((dt*100.0f)+100); t = 0; if (colCount>100) {colCount=0;} colCount += 1; clear = false; } yEnd = calculate(dt, t, yStart, yEnd); for (int i = 0; i < 2; i++) { yStart[i] = yEnd[i]; } t += dt; //yEnd[0] = (float)Math.sin(t); update(); } } animator = null; } public void start() { if (animator == null) { please_stop = false; animator = new Thread(this); animator.start(); } } // Standard method to stop the applet public void stop() { please_stop = true; } // Standard method to destroy the applet public void destroy() { } // Main entry point when running standalone public static void main(String[] args) { rungekutta applet = new rungekutta(); applet.isStandalone = true; Frame frame = new Frame(); frame.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { Frame f = (Frame) e.getSource(); f.setVisible(false); f.dispose(); System.exit(0); } }); frame.setTitle("Applet Frame"); frame.add( applet, BorderLayout.CENTER ); applet.init(); applet.start(); frame.setSize( 400, 320 ); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation( (d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible( true ); } }