sawat #5385(2008/01/18 10:32 GMT) [ Java ] Rating0/0=0.00
Swingで。特に工夫はなし。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
import java.awt.*; import java.util.Random; import java.util.concurrent.*; import javax.swing.*; public class Life extends JFrame { private boolean[] cells; private final int pitch, w, h; public static void main(String[] args) { Life f = new Life(20, 20, 0.3); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public Life(final int w, final int h, final double rate) { super("Life"); this.w = w; this.h = h; pitch = 5; cells = new boolean[h*w]; initialize(rate); getContentPane().add(new JLabel(new Icon() { public int getIconHeight() { return h * pitch + pitch; } public int getIconWidth() { return w * pitch + pitch; } public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.BLUE); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if(cells[at(i, j)]) g.fillRect(j*pitch, i*pitch, pitch, pitch); } })); Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() { public void run() { update(); repaint(); } }, 1000L, 200L, TimeUnit.MILLISECONDS); } private void initialize(double rate) { final Random random = new Random(System.currentTimeMillis()); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cells[at(i, j)] = random.nextDouble() < rate; } private int at(int i, int j) { return i*w + j; } protected void update() { boolean[] newG = cells.clone(); for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) newG[at(i,j)] = next(i, j); cells = newG; } private boolean next(int i, int j) { final int u = (i+h-1)%h, d = (i+1)%h, l = (j+w-1)%w, r = (j+1)%w; int count = 0; if(cells[at(u, j)]) count++; if(cells[at(u, r)]) count++; if(cells[at(i, r)]) count++; if(cells[at(d, r)]) count++; if(cells[at(d, j)]) count++; if(cells[at(d, l)]) count++; if(cells[at(i, l)]) count++; if(cells[at(u, l)]) count++; return ((cells[at(i, j)] && (count == 2 || count == 3)) || (!cells[at(i, j)] && count == 3)); } }
Rating0/0=0.00-0+
[ reply ]
sawat
#5385()
[
Java
]
Rating0/0=0.00
Swingで。特に工夫はなし。
Rating0/0=0.00-0+
[ reply ]