import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import javax.swing.text.StyleConstants; import javax.swing.text.MutableAttributeSet; import javax.swing.text.SimpleAttributeSet; public class Life extends JFrame implements ActionListener{ String alive = "☀"; String dead = "・"; int ct = 0; boolean running = false; String[] now = { "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・☀・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・☀・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・☀・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・☀・・・・・・・", "・・・・・・・・・・・・・・・☀☀☀☀☀・・・・・・・・・・・・☀・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・☀・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・☀・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・☀☀☀・・・・・・・・・・☀・・・・☀・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・☀☀☀☀☀・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・", "・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・" }; String[] nxt; JTextPane text; JButton btn; public Life() { setDefaultCloseOperation(EXIT_ON_CLOSE); String initlife = ""; for(int y=0; now.length>y; y++){ initlife += now[y]+"\n"; } initlife += "世代数:0"; text = new JTextPane(); SimpleAttributeSet a = new SimpleAttributeSet(); StyleConstants.setFontSize(a, 16); StyleConstants.setLineSpacing(a, -0.2f); text.setParagraphAttributes(a, true) ; text.setText(initlife); btn = new JButton("start"); setLayout(new BorderLayout()); add(text, BorderLayout.CENTER); add(btn, BorderLayout.SOUTH); btn.addActionListener(this); pack(); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btn) { if (running==false ){ LifeSpread ls = new LifeSpread(now,text); ls.start(); btn.setText("busy"); running = true; } } } public static void main( String[] args ) { Life life = new Life(); } } class LifeSpread extends Thread { String[] now,nxt; JTextPane text; String alive = "☀"; String dead = "・"; public LifeSpread(String[] now,JTextPane text){ this.now = now; this.text = text; nxt = new String[now.length]; } @Override public void run() { for(int ct=0;1000>ct;ct++){ System.out.printf("世代数:%d\n",ct); try { Thread.sleep(500); } catch(InterruptedException ex) { System.err.println(ex); } System.out.println("bgnNext"); setNext(); System.out.println("endNext"); textDisp(ct); } } public void setNext(){ int jb,ib,je,ie,alivect; int ymax = now.length; int xmax = now[0].length(); for(int y=0; ymax>y; y++){ nxt[y] = ""; for(int x=0; xmax>x; x++){ jb = y-1; je = y+1; ib = x-1; ie = x+1; if(0>jb) {jb = 0;} if(je>=ymax){je = ymax-1;} if(0>ib) {ib = 0;} if(ie>=xmax){ie = xmax-1;} alivect=0; for(int j=jb; je>=j; j++){ for(int i=ib; ie>=i; i++){ if (now[j].substring(i,i+1).equals(alive)) alivect++; } } if (now[y].substring(x,x+1).equals(alive)){ alivect--; if(alivect==2 || alivect==3) { nxt[y]+=alive; } else { nxt[y]+=dead; } } else{ if(alivect==3) { nxt[y]+=alive; } else { nxt[y]+=dead; } } }/* x */ }/* y */ for(int y=0; ymax>y; y++){ now[y] = nxt[y]; } }//end of setNext public void textDisp(int ct){ String nowstr = ""; for(int y=0; now.length>y; y++){ nowstr += now[y]+"\n"; } nowstr += "世代数:"+ct; text.selectAll(); text.replaceSelection(nowstr); text.repaint(); } }