import java.awt.*; import java.awt.image.*; import java.awt.geom.*; import javax.swing.*; import java.util.Random; public class Orbit extends JComponent implements Runnable { int imageWidth, imageHeight; double delta = 0.001; double xLowerLeft, yLowerLeft, xUpperRight, yUpperRight; double pixelWidth, pixelHeight, invPixelWidth, invPixelHeight; double[] pv = new double[4]; double ax, ay; int counter = 0; Random randNumGen = new Random(5); BufferedImage bufImage; Graphics2D bufImageGraphic; public Orbit(int w, int h, double xLL, double yLL, double xUR, double yUR) { pv[0] = 1.0; pv[1] = 0.0; pv[2] = 0.0; pv[3] = -1.0; imageWidth = w; imageHeight = h; xLowerLeft = xLL; yLowerLeft = yLL; xUpperRight = xUR; yUpperRight = yUR; pixelWidth = (xUR - xLL)/((double)w); invPixelWidth = ((double)w)/(xUR - xLL); pixelHeight = (yUR - yLL)/((double)h); invPixelHeight = ((double)h)/(yUR - yLL); System.out.println(imageWidth + " " + imageHeight); System.out.println(pixelWidth + " " + pixelHeight); bufImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); bufImageGraphic = bufImage.createGraphics( ); bufImageGraphic.setPaint(Color.white); //bufImageGraphic.setPaint(Color.black); bufImageGraphic.fillRect(0, 0, w, h); bufImageGraphic.setPaint(Color.blue); bufImageGraphic.setStroke(new BasicStroke((float)(0.1*pixelHeight))); AffineTransform at = new AffineTransform(invPixelWidth, 0, 0, -invPixelHeight, -invPixelWidth*xLL, invPixelHeight*yUR ); bufImageGraphic.setTransform(at); Thread t = new Thread(this); t.start( ); } public void timeStep( ) { double[] u = new double[4]; u = DOrbit(pv); Line2D.Double L = new Line2D.Double(pv[0], pv[1], u[0], u[1]); bufImageGraphic.fill(L); pv[0] = u[0]; pv[1] = u[1]; pv[2] = u[2]; pv[3] = u[3]; } public void run( ) { try { for(counter = 0; counter < 10000000; ++counter) { //Loopbody: timeStep( ); repaint( ); Thread.sleep(0); } } catch (InterruptedException ie) {} } double[] DOrbit(double[] s) { double r = Math.sqrt(s[0]*s[0] + s[1]*s[1]); double u[] = new double[4]; u[0] = delta*s[2] + s[0]; u[1] = delta*s[3] + s[1]; u[2] = -delta*s[0]*(1.0/(r*r*r)) + s[2]; u[3] = -delta*s[1]*(1.0/(r*r*r)) + s[3]; return u; } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); //g2.setPaint(Color.blue); //g2.drawImage(image, imageX, imageY, this); g2.drawImage(bufImage, 0, 0, this); } public static void main(String[] args) { JFrame frame = new JFrame("Orbit"); frame.setBackground(Color.white); Orbit plot = new Orbit(800, 800, -2, -2, 2, 2); frame.getContentPane( ).add(plot); frame.setSize(800, 800); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }