细胞模拟程序

xiaoxiao2022-06-13  88

这是我在论坛上一个朋友出的一道题目,然后我再用java来写的。总共3个类。        public class mainapp {    /**     * 1. 如果一个细胞只有0或1个邻居,它将因为孤独而死;      * 2. 如果一个细胞有4到8个邻居,它将因为拥挤而死;      * 3. 如果一个细胞恰有2或者3个邻居,它将继续生存下去;      * 4. 如果一个空格子恰有3个邻居,将“生”出一个新细胞;      * 5. 其他的空格子继续维持原状。     */    public static void main(String[] args) {        // TODO Auto-generated method stub        Cellsgroup test = new Cellsgroup();        test.setVisible(true);    }} /* * 此类是一个窗体,用来装cell的容器 */import java.awt.Container;import java.awt.Dimension;import java.awt.GridLayout;import java.awt.Toolkit;import javax.swing.JFrame;public class Cellsgroup extends JFrame {    private static final long serialVersionUID = 1L;    public int rows    = 10;                          //行    public int coloums = 10;                          //列数    Cell[]     Cellgroup = new Cell[rows*coloums];    //装载Cellgroup的类        //构造方法    public Cellsgroup(){        super("cell");                                 //设置标题        Toolkit mykit = this.getToolkit();        Dimension wndsize = mykit.getScreenSize();        int size = wndsize.width/2;        this.setBounds((wndsize.width-size)/2,(wndsize.height-size)/2                       ,size,size);                           //设置大小        this.allcell();        this.setDefaultCloseOperation(EXIT_ON_CLOSE);         //设置推出        this.setVisible(true);    }        //添加元素    private void allcell() {        GridLayout layout = new GridLayout(rows,coloums);     //设置网格布局管理器        Container  content = this.getContentPane();                    content.setLayout(layout);                    for(int i = 0;i<Cellgroup.length;i++){                //初始化,放入cell                Cellgroup[i] = new Cell(i,this);            content.add(Cellgroup[i]);        }        for(Cell cell:Cellgroup){            cell.initialfrinends(cell.getNo());        }    }} /*细胞,为了处理方便,这里把其做为一个Label表示。*/import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JLabel;import javax.swing.Timer;public class Cell extends JLabel {    private static final long serialVersionUID = 1L;    private boolean         status;                     //表示细胞存活的状态。    private Cell[]          cellfriend;                 //存储附近细胞的类     private Cellsgroup      Cellsgroup;                 //载入窗体    private int             no;                         //窗体编号    public int getNo() {        return no;    }    Timer                   t ; //计时器        //获取状态    public boolean isStatus() {                                 return status;    }        //设定状态    public void setStatus(boolean status) {        if(status){            this.setText("Cell");                      //表示出现新的细胞        }else{            this.setText("");                     //细胞死亡        }        this.repaint();        this.status = status;    }        //构造函数    public Cell(int no,Cellsgroup Cellsgroup){        this.Cellsgroup = Cellsgroup;        this.initialStatus();                           //初始化状态        this.no   = no;        t = new Timer((int) (200*Math.random()+900),new timelistener());        t.start();                                      //打开计时器            }        //初始化状态    private void initialStatus(){                               if(Math.random()>0.5){                          //当随机数大于0.5时,表示细胞存活             this.setStatus(true);                       //为存活        }else{            this.setStatus(false);                      //为死亡        }    }        //设定朋友群    public void initialfrinends(int no){        cellfriend = new Cell[8];                          //表示8个方位        this.setOneFriend(1, no-Cellsgroup.coloums);       //正上方        if(((no-1)
转载请注明原文地址: https://www.6miu.com/read-4936200.html

最新回复(0)