import java.awt.*; import java.awt.image.*; import java.awt.geom.*; import javax.swing.*; import java.util.Random; public class Wolfram extends Panel implements Runnable { int imageWidth, imageHeight; static double a, b; static double Pi = Math.PI; static double rho = 0.001; // static Random randNumGen = new Random(5); BufferedImage bufImage; Graphics2D bufImageGraphic; double[] cells = new double[400]; public Wolfram(int w, int h) { 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); setSize(w,h); 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.red); /* bufImageGraphic.setStroke( //new BasicStroke((float)pixelHeight) new BasicStroke( (float)pixelHeight, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND ) ); */ /* 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(int j) { int i; cells = stateTransition(cells); for (i = 0; i < 400; ++i) { Color c; float rgb1, rgb2, rgb3; double c1, c2, c3; c1 = (1-cells[i]); c2 = 0; c3 = cells[i]; //System.out.println(c1 + " " + c2 + " " + c3); rgb1 = (float) c1; rgb2 = (float) c2; rgb3 = (float) c3; c = new Color(rgb1, rgb2, rgb3); bufImageGraphic.setPaint(c); int s = 2; bufImageGraphic.fillRect(i*s, j*s, s, s); } } public void run( ) { try { int i,j; Color c; float rgb1, rgb2, rgb3; double c1, c2, c3; Random randGen = new Random((long)1); for (i = 0; i < 400; i++) { cells[i] = 0; } cells[200] = 1; for(j = 0; j < 6400; ++j) { //LOOPBODY: timeStep(j); repaint( ); Thread.sleep(500); } } catch (InterruptedException ie) {} } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; /* g2.setRenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); */ RenderingHints rh = new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); rh.put( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY ); g2.setRenderingHints(rh); g2.drawImage(bufImage, 0, 0, this); } static double[] stateTransition(double[] x) { //assumes x.length >= 2; int len = x.length; int last = len-1; double y[] = new double[len]; y[0] = poly(x[len-1],x[0],x[1]); y[last] = poly(x[last-1],x[last],x[0]); for(int i = 1; i < last; i++) y[i] = poly(x[i-1],x[i],x[i+1]); return y; } static double binarize(double x) { double acc = 0; double den = 0.5; for (int k = 0; k < 14; ++k) { if (acc + den <= x) acc += den; den *= 0.5; } return acc; } static double poly(double u, double x, double v) { /* return (1-u)*(1-x)*v + (1-u)*x*(1-v) + (1-u)*x*v + u*(1-x)*v + u*x*(1-v); */ return x + v - x*v - u*x*v; //return u + v - 2*u*v; } public static void main(String[] args) { a = Double.valueOf(args[0]).doubleValue(); b = Double.valueOf(args[1]).doubleValue(); JFrame frame = new JFrame(); frame.setSize(800,800); Wolfram plot = new Wolfram(800, 800); JScrollPane jsc = new JScrollPane(plot, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS ); jsc.setSize(800,800); frame.getContentPane().add(jsc); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setVisible(true); } }