?? celluar.java~1~
字號:
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;//每演化幾步刷新屏幕一次 //參考系數(shù):size=100,per=0.08;網(wǎng)頁中:size=50,per=0.1; boolean running;//運行停啟標志 boolean started;//開始標志 int stepLeft=0;//單步運行時使用 int steps=0;//演化的步數(shù) int cyclemax=2000;//cyclemax為記載歷史數(shù)據(jù)的最大限度 int HistoryData[];//歷史數(shù)據(jù),主要記載每次生存格子的比例 int cCount=0;//每一步生存格子的比例 int delay=50;//cpu每delay10計算一次 int width=300,heightall=350;//定義長寬 int height=300;//作圖區(qū)域的高 Button pausebutton; int grid[][][];//計算時確定格子的坐標 int rule[][][][][][][][][];//判斷規(guī)則,每次預先生成 int cd=0;//用于grid[cd][][],值為0,1交換,實現(xiàn)實際數(shù)組和暫存數(shù)組的交換 int square,fringe;//作圖時方格及空隙大小 boolean isStandalone = false;//系統(tǒng)參數(shù),是否獨立運行 /**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("Step")); p.add(btn); p.add(new Button("show")); p.add(btn1); p.add(btn2); add("South",p); reinit();//initial一次 } public void reinit(){ steps=0;//演化步數(shù)置零 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<size;i++){ for(int j=0;j<size;j++){ double ran=Math.random(); if (ran<per){ grid[cd][i][j]=1; } else{ grid[cd][i][j]=0; } } } //自動生成規(guī)則 for(int i1=0;i1<2;i1++){ for(int i2=0;i2<2;i2++){ for(int i3=0;i3<2;i3++){ for(int i4=0;i4<2;i4++){ for(int i5=0;i5<2;i5++){ for(int i6=0;i6<2;i6++){ for(int i7=0;i7<2;i7++){ for(int i8=0;i8<2;i8++){ rule[0][i1][i2][i3][i4][i5][i6][i7][i8]=0; rule[1][i1][i2][i3][i4][i5][i6][i7][i8]=0; int addition=i1+i2+i3+i4+i5+i6+i7+i8; if(addition==2){ rule[1][i1][i2][i3][i4][i5][i6][i7][i8]=1; } else if(addition==3){ rule[0][i1][i2][i3][i4][i5][i6][i7][i8]=1; rule[1][i1][i2][i3][i4][i5][i6][i7][i8]=1; } } } } } } } } } for(int i=0;i<cyclemax;i++){ HistoryData[i]=-1;//給歷史紀錄賦值 } repaint();//畫出圖 update(this.getGraphics());//用于網(wǎng)頁中 } /**Start the applet*/ //必須用到的函數(shù),如果用到implements Runnablepublic void run() { repaint(); while (true) { if (running || stepLeft > 0) {//stepleft為單步運行做的判斷;而running為連續(xù)運行服務(wù) decision();//確定值 if( (steps % refreshSteps == 0)|| (stepLeft > 0)) repaint();//每個幾步畫一次 if (stepLeft > 0) stepLeft--;//單步時運行完一步就使stepleft小于0,然后就不再做decision showStatus("Steps:"+(steps++));//狀態(tài)條 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;i<size;i++){ for(int j=0;j<size;j++){ //邊界可循環(huán) int x1=((i-1)+size)%size,x3=((i+1)+size)%size,y1=((j-1)+size)%size,y3=((j+1)+size)%size; //先把值給暫存數(shù)組 grid[1-cd][i][j]=rule[grid[cd][i][j]][grid[cd][x1][y1]][grid[cd][i][y1]][grid[cd][x3][y1]][grid[cd][x1][j]][grid[cd][x3][j]][grid[cd][x1][y3]][grid[cd][i][y3]][grid[cd][x3][y3]]; if (grid[1-cd][i][j]==1) cCount++;//記錄生存者 } } cd=1-cd;//轉(zhuǎn)換成現(xiàn)實數(shù)組 int index=steps%cyclemax;//最大記錄為cyclemax個 HistoryData[index]=cCount;//記錄每次生存者數(shù)目 } public void start() { if(runner==null){runner=new Thread(this);runner.start();}//啟用線程 } /**Stop the applet*/ public void stop() { if(runner!=null){ runner.stop(); runner = null; } } /**Destroy the applet*/ public void destroy() { if(runner!=null){ runner.stop(); runner = null; } } /**Get Applet information*/ public String getAppletInfo() { return "Applet Information"; } /**Get parameter info*/ public String[][] getParameterInfo() { return null; } /**Main method*/ public static void main(String[] args) { celluar applet = new celluar(); applet.isStandalone = true; Frame frame; frame = new Frame() { protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public synchronized void setTitle(String title) { super.setTitle(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } }; frame.setTitle("Applet Frame"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(300,320); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); }public void update(Graphics g) { paint(g);}//網(wǎng)頁中畫圖public void paint (Graphics g) { if(started) {g.clearRect(0,0,width,height);started=false;}//初始時清空面板 for(int i=0;i<size;i++){ for(int j=0;j<size;j++){ Color cl; if(grid[cd][i][j]==1){ cl=Color.orange; }else{ cl=Color.black; } g.setColor(cl); g.fillRect(i*(square+fringe),j*(square+fringe),square,square); } }}//按鈕事件public boolean action(Event ev, Object arg){ if ( ev.target instanceof Button) {//instanceof實例 String button = (String) arg; if (button.equals("Stop")) { pausebutton.setLabel("Start"); running = false; } else if (button.equals("Start")) { pausebutton.setLabel("Stop"); running = true; } else if (button.equals("Step")) {//單步運行 stepLeft = 1; if (running) { pausebutton.setLabel("Start"); running = false; } } else if (button.equals("set")){//設(shè)置參數(shù) running=false; CelluarControl ctl=new CelluarControl(this); ctl.setSize(300,300); ctl.show(); } else if (button.equals("show")){//顯示數(shù)據(jù) ViewLog ctl=new ViewLog(this); ctl.setSize(550,400); ctl.show(); } else if(button.equals("slower")){ if (delay<1000){ delay=delay+50; } } else if(button.equals("faster")){ if (delay>0){ delay=delay-50; } } return true; }else return false;}}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -