www.pudn.com > java_celluar.zip > celluar.java


package celluar;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/**
 * Title:        Game of Life
 * Description:
 * Copyright:    Copyright (c) 2003
 * Company:      Http://agents.yeah.net
 * @author Keats
 * @version 1.0
 */

public class celluar extends Applet implements Runnable {//加implements Runnable是使这个类能作为线程独立运行
  Thread    runner;//为类定义一个线程,在start中启用
  int size=50;//世界的大小
  double per=0.1;//起始时生存者占整个世界的比率
  int refreshSteps=1;//每演化几步刷新屏幕一次
  //参考系数:size=100,per=0.08;网页中:size=50,per=0.1;

  boolean running;//运行停启标志
  boolean started;//开始标志
  int stepLeft=0;//单步运行时使用
  int steps=0;//演化的步数
  int cyclemax=2000;//cyclemax为记载历史数据的最大限度
  int HistoryData[];//历史数据,主要记载每次生存格子的比例
  int cCount=0;//每一步生存格子的比例
  int delay=50;//cpu每delay10计算一次
  int width=300,heightall=350;//定义长宽
  int height=300;//作图区域的高
  Button pausebutton;
  int grid[][][];//计算时确定格子的坐标
  int rule[][][][][][][][][];//判断规则,每次预先生成
  int cd=0;//用于grid[cd][][],值为0,1交换,实现实际数组和暂存数组的交换
  int square,fringe;//作图时方格及空隙大小
  boolean isStandalone = false;//系统参数,是否独立运行
  /**Get a parameter value*/
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**Construct the applet*/
  public celluar() {
      HistoryData=new int[cyclemax];
  }
  /**Initialize the applet*/
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  /**Component initialization*/
  private void jbInit() throws Exception {
    resize(width,heightall);//重置窗口大小

    pausebutton = new Button("Start");
    Button btn=new Button("set");
    Button btn1=new Button("+");
    Button btn2=new Button("-");
    setLayout(new BorderLayout());//定义对齐方式

    Panel p = new Panel();//定义一个面板
    this.setBackground(Color.black);
    p.setBackground(Color.lightGray);

    btn1.setLabel("faster");
    btn2.setLabel("slower");
    p.add(pausebutton);//加控件之后要添加进去
    p.add(new Button("reset"));
    p.add(btn);
    p.add(new Button("show"));
    p.add(btn1);
    p.add(btn2);
    add("South",p);
    reinit();//initial一次

  }

  public void reinit(){
    steps=0;//演化步数置零
    started=true;//置一下开始,而后129清空,并置为false等待运行
    square = width/size-1;
    fringe = 1;
    grid   = new int[2][size][size];
    rule   = new int [2][2][2][2][2][2][2][2][2];
    //随机赋值
    for(int i=0;i 0) {//stepleft为单步运行做的判断;而running为连续运行服务
      decision();//确定值
      if( (steps % refreshSteps == 0)|| (stepLeft > 0)) repaint();//每个几步画一次
      if (stepLeft > 0) stepLeft--;//单步时运行完一步就使stepleft小于0,然后就不再做decision
      showStatus("Steps:"+(steps++));//状态条
      try{Thread.sleep(delay);}catch(InterruptedException e){};//每隔delay秒cpu运算一次,也就是做一次decision
    }
    else{try{Thread.sleep(500);}catch(InterruptedException e){};}
  }
}

  public void decision(){
    int cCount=0;//每一步的生存者赋初值零
    for(int i=0;i0){
            delay=delay-50;
          }
        }
        return true;
 }else return false;
}


}