⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lifesim.java

📁 Autolife模型是一个能够进行“开放式进化”的人工生命系统。每个Agent模型采用可以变化规则表长度的有限自动机模型建模。一方面Agent可以进行自我繁殖
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  }  public boolean action(Event evt, Object o) {    if(evt.target==btnConfig){      Configer ctl=new Configer(this);      ctl.setSize(400,300);      ctl.show();      haveSetup=true;      return true;    }else if(evt.target==btnStart){      running=!running;      if(!running){			btnRestart.enable(true);			btnStep.enable(true);			btnConfig.enable(true);			btnStart.setLabel("Continue");      }else{			btnRestart.enable(false);			btnStep.enable(false);			btnConfig.enable(false);			btnStart.setLabel("Stop");      }      return true;    }else if(evt.target==btnRestart){    	boolean run=running;    	running=false;    	localwd.reinit();    	canvas.repaint();    	running=run;    }else if(evt.target==btnStep){      localwd.process();      return true;    }else if(evt.target==btnShow){      CurveShow ctl=new CurveShow(this);      ctl.setSize(500,400);      ctl.show();      return true;    }else if(evt.target==choiceAgType){    	localwd.agType=choiceAgType.getSelectedIndex();    	if(localwd.agType==0&&localwd.FoodAddNum==0){    		localwd.FoodAddNum=100;			txtRanAdd.setText(Integer.toString(localwd.FoodAddNum));    	}    	if(localwd.agType==0){			for(int i=0;i<localwd.agents.size();i++){						agent ag=(agent)localwd.agents.elementAt(i);						ag.outputNum=4;						ag.ChangeRule();			}    	}else{			for(int i=0;i<localwd.agents.size();i++){						agent ag=(agent)localwd.agents.elementAt(i);						ag.outputNum=5;						//ag.ChangeRule();			}			if(!haveSetup){				localwd.EatEnergy=100;				localwd.maxAge=500;				localwd.maxContaining=5000;				localwd.turnEnergy=1;				localwd.stepEnergy=2;				localwd.normalEnergy=1;			}    	}		slbAddFood.setValue(localwd.FoodAddNum);    }else if(evt.target==choiceDrawType){    	localwd.drawType=choiceDrawType.getSelectedIndex();    } else if(evt.target==choiceObj){      objSelect=choiceObj.getSelectedIndex();      if(objSelect==2){      	shapeType=0;      	choiceShapeType.select(shapeType);      	choiceShapeType.enable(false);      }else{		choiceShapeType.enable(true);      }    }else if(evt.target==choiceShapeType){      shapeType=choiceShapeType.getSelectedIndex();      if(shapeType==1){      	objSelect=0;      	choiceObj.select(objSelect);      	choiceObj.enable(false);      }else{      	choiceObj.enable(true);      }    }else if(evt.target==choiceFill){	      fillType=choiceFill.getSelectedIndex();	      if(fillType==1){	            shapeType=1;	      		choiceShapeType.select(shapeType);		  		objSelect=0;		  		choiceObj.select(objSelect);		  		choiceObj.enable(false);	      }else{	      	choiceObj.enable(true);	      }    }else if(evt.target==choiceLineWidth){    	lineWidth=choiceLineWidth.getSelectedIndex()+1;    }else if(evt.target==choiceDynEnv){    	localwd.dynType=choiceDynEnv.getSelectedIndex();    	if(!haveSetup&&localwd.agType==0){			if(localwd.dynType>0){				localwd.EatEnergy=1000;				localwd.maxAge=5000;				localwd.maxContaining=10000;				localwd.turnEnergy=1;				localwd.stepEnergy=4;				localwd.normalEnergy=1;			}else{				localwd.EatEnergy=100;				localwd.maxAge=500;				localwd.maxContaining=5000;				localwd.turnEnergy=1;				localwd.stepEnergy=2;				localwd.normalEnergy=1;			}    	}    }    else if(evt.target==btnSave){      running=false;     // dataBase.SaveAll(this,0);      //running=true;    }    return false;  }  /**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) {    lifesim applet = new lifesim();    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(400,320);    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);    frame.setVisible(true);  }   void canvas_mousePressed(MouseEvent e) {   		//if(!running)return;		mousePressed=true;		startPoint=new Point(e.getX(),e.getY());		endPoint=new Point(startPoint);   }   void canvas_mouseReleased(MouseEvent e) {   	   		if(mousePressed){   			if(objSelect==2){	   			zoomView zv=new zoomView(this,startPoint,endPoint);	   			zv.setSize(600,500);	   			zv.show();   			}			Point pt1=new Point(startPoint);			Point pt2=new Point(endPoint);			  if(startPoint.x>endPoint.x){				pt1.x=endPoint.x;				pt2.x=startPoint.x;			  }			  if(startPoint.y>endPoint.y){				pt1.y=endPoint.y;				pt2.y=startPoint.y;			  }   			if(shapeType==0){   				if(objSelect==0){					localwd.DealWithSelected(pt1,pt2,null,3,1);   				}else if(objSelect==1){					localwd.DealWithSelected(pt1,pt2,null,0,1);   				}   			}   			if(objSelect==0&&shapeType==1){   					if(fillType==0){						localwd.DealWithSelected(startPoint,endPoint,null,4,2);   					}else if(fillType==1){						localwd.DealWithSelected(startPoint,endPoint,null,4,gValue);   					}   			}			//canvas.repaint();   		}   		startPoint=new Point(0,0);   		endPoint=new Point(0,0);		mousePressed=false;   }  void canvas_mouseDragged(MouseEvent e) {		if(mousePressed&&e.getX()<localwd.width&&e.getY()<localwd.height&&e.getX()>=0&&e.getY()>=0){			if(running){				running=false;				localwd.clearAgentsWorld();				for(int i=0;i<localwd.agents.size();i++){					agent ag=(agent)localwd.agents.elementAt(i);					int xx=ag.x;int yy=ag.y;					localwd.AgentsWorld[xx][yy]=1;				}			}			//canvas.repaint();			int i1=startPoint.x;			int i2=endPoint.x;			if(i1>i2){i1=endPoint.x;i2=startPoint.x;}			int j1=startPoint.y;			int j2=endPoint.y;			Graphics g=canvas.getGraphics();			if(j1>j2){				j1=endPoint.y;j2=startPoint.y;			}			for(int i=i1;i<=i2;i++){				for(int j=j1;j<=j2;j++){					g.setColor(COLOR_BACK);					if(localwd.WorldState[i][j]==1){						g.setColor(COLOR_GRASS);					}					if(localwd.WorldState[i][j]==2){						if(localwd.drawType==1){							g.setColor(COLOR_SEED);						}					}					if(localwd.AgentsWorld[i][j]==1){						g.setColor(COLOR_LIFE);					}					g.fillRect(i,j,1,1);				}			}			g.setColor(Color.white);			endPoint=new Point(e.getX(),e.getY());			int xx=startPoint.x;			int yy=startPoint.y;			if(xx>endPoint.x)xx=endPoint.x;			if(yy>endPoint.y)yy=endPoint.y;			switch(shapeType){			case 0:				g.drawRect(xx,yy,Math.abs(endPoint.x-startPoint.x),Math.abs(endPoint.y-startPoint.y));				break;			case 1:				g.drawLine(startPoint.x,startPoint.y,endPoint.x,endPoint.y);				break;			case 2:				g.drawOval(xx,yy,Math.abs(endPoint.x-startPoint.x),Math.abs(endPoint.y-startPoint.y));				break;			}		}  }  void txtGValue_textValueChanged(TextEvent e) {  		if(txtGValue.getText().equals(""))gValue=0;  		else gValue=Double.valueOf(txtGValue.getText()).doubleValue();  		slbGradiant.setValue((int)(gConstant*gValue));  }  public boolean handleEvent(Event evt)	  {		  switch (evt.id)		  {			  case Event.SCROLL_ABSOLUTE:			  case Event.SCROLL_LINE_DOWN:			  case Event.SCROLL_LINE_UP:			  case Event.SCROLL_PAGE_DOWN:			  case Event.SCROLL_PAGE_UP:			  if(evt.target==slbGradiant){				gValue=(float)(slbGradiant.getValue())/(float)gConstant;				txtGValue.setText(Float.toString((float)(gValue)));			  }else if(evt.target==slbAddFood){			  	localwd.FoodAddNum=slbAddFood.getValue();			  	txtRanAdd.setText(Integer.toString(localwd.FoodAddNum));			  }		  }		  return(super.handleEvent(evt));	  }  void txtRanAdd_textValueChanged(TextEvent e) {  	if(txtRanAdd.getText().equals(""))localwd.FoodAddNum=0;  	else localwd.FoodAddNum=Integer.valueOf(txtRanAdd.getText()).intValue();  	slbAddFood.setValue(localwd.FoodAddNum);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -