/* (C) 2010 by folkert@vanheusden.com * released under GPL2 */ import java.awt.*; import java.awt.image.*; import java.io.*; import java.util.*; import javax.imageio.*; import javax.swing.*; class jgof extends JPanel { final static int dim = 256; final static double cmul = 255.0 / (double)dim; public jgof() { } public void paintComponent(Graphics g) { super.paintComponent(g); } public void drawImage(BufferedImage image, boolean [][][] in) { for(int ary=0; ary=0; arz--) { int gray = 1 + (int)((double)arz * cmul); int rgb = gray << 8; int checkz = arz; // - dim / 2; if (checkz < 0) checkz += dim; if (in[arx][ary][checkz]) { image.setRGB(arx, ary, rgb); break; } } } } } public void loop(boolean emitPng) throws Exception { int it = 0; Graphics g = getGraphics(); Graphics2D g2d = (Graphics2D)g; assert g2d != null; boolean [][][] prev, cur = new boolean[dim][dim][dim]; /* init world */ Random r = new Random(); for(long nr=0; nr<(((long)dim * (long)dim * (long)dim) / 100L); nr++) { int x = r.nextInt(dim); int y = r.nextInt(dim); int z = r.nextInt(dim); cur[x][y][z] = true; } /* draw initial world */ BufferedImage image = new BufferedImage(dim, dim, BufferedImage.TYPE_INT_RGB); drawImage(image, cur); g2d.drawImage((Image)image, 0, 0, null); g.setColor(Color.RED); g.setFont(new Font("Arial", Font.PLAIN, 10)); for(;;) { prev = cur; cur = new boolean[dim][dim][dim]; for(int arz=0; arz 3) { } else { cur[arx][ary][arz] = true; } } else if (neighbours == 3) { cur[arx][ary][arz] = true; } } // arx } // ary } // arz image = new BufferedImage(dim, dim, BufferedImage.TYPE_INT_RGB); drawImage(image, cur); g2d.drawImage((Image)image, 0, 0, null); ++it; g2d.drawString("" + it, 10, 10); if (emitPng) { File f = new File("output" + String.format("%05d", it) + ".png"); ImageIO.write(image, "png", f); } } // endless loop } public static void main(String [] args) throws Exception { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); JFrame f = new JFrame(); f.setSize(dim, dim); GraphicsConfiguration gc = gd.getDefaultConfiguration(); jgof j = new jgof(); f.setContentPane(j); f.setVisible(true); j.loop(args.length > 0); } }