[cellautomaton_ap.java] -- import java.awt.*; import java.applet.Applet; import java.awt.event.*; /* */ /* セルオートマトン実習解答描画 アプレット版 */ /* */ /* Copyright (C) Junro Yoshino 2000 */ /* */ public class cellautomaton_ap extends Applet implements ItemListener, ActionListener, Runnable{ private volatile Thread th; private boolean threadSuspended; Button butn = new Button(); Choice fig=new Choice(); int tt; int size=9; int cell[][]=new int[size][size]; public void init(){ int i; setstart(0); add(butn); add(new Label("Figure")); for(i=1; i<=3; i=i+1) fig.addItem(""+i); add(fig); fig.addItemListener(this); butn.setLabel("start"); butn.addActionListener(this); threadSuspended = true; th=new Thread(this); } public void start(){ th.start(); } public synchronized void stop(){ th = null; notify(); } public void run(){ Thread thisThread = Thread.currentThread(); int c, k; while(th == thisThread){ try{ thisThread.sleep(100); synchronized(this){ while(threadSuspended) wait(); } }catch(InterruptedException e){ } if(tt!=9){ cycle(); tt++; repaint(); } else{ k=fig.getSelectedIndex(); setstart(k); butn.setLabel("start"); threadSuspended = true; } } } public synchronized void actionPerformed(ActionEvent e){ if(!threadSuspended){ threadSuspended = true; butn.setLabel("start"); repaint(); } else { threadSuspended = false; butn.setLabel("stop"); notify(); } } public void itemStateChanged(ItemEvent e){ int k; if(threadSuspended){ k=fig.getSelectedIndex(); setstart(k); repaint(); } } public void paint(Graphics g){ int i, j; g.drawString("t="+tt, 40, 40); for(j=0; j