楼主: mazed__fishs
2052 0

在用swarm编程时,怎么给不同类型的主体设置不同的颜色 [推广有奖]

  • 0关注
  • 1粉丝

已卖:95份资源

高中生

55%

还不是VIP/贵宾

-

威望
0
论坛币
272 个
通用积分
1.0600
学术水平
5 点
热心指数
10 点
信用等级
5 点
经验
277 点
帖子
11
精华
0
在线时间
50 小时
注册时间
2013-9-24
最后登录
2016-2-29

楼主
mazed__fishs 发表于 2014-9-10 20:52:12 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
在用swarm编程时,怎么给不同类型的主体设置不同的颜色。不断改变主体的类型,主体的颜色变化。我编写了一段简单的程序,虽然可以显示不同的主体不同的颜色,但不能实现变化的功能,求高手赐教!!!!
import swarm.Globals;
import swarm.Selector;
import swarm.defobj.Zone;
import swarm.gui.Colormap;
import swarm.gui.ColormapImpl;
import swarm.gui.ZoomRaster;
import swarm.gui.Raster;
import swarm.gui.ZoomRasterImpl;
import swarm.objectbase.Swarm;
import swarm.objectbase.SwarmImpl;
import swarm.simtoolsgui.GUISwarm;
import swarm.simtoolsgui.GUISwarmImpl;
import swarm.activity.ActionGroupImpl;
import swarm.activity.ScheduleImpl;
import swarm.activity.Activity;
import swarm.collections.ListImpl;
import swarm.objectbase.SwarmObjectImpl;
import swarm.space.Grid2dImpl;
import swarm.space.Object2dDisplay;
import swarm.space.Object2dDisplayImpl;
class agent extends SwarmObjectImpl {
public int xPos,yPos;
    public byte bugColor;
    public int type;
   
    public agent(Zone aZone, int X, int Y) {
     super(aZone);
     xPos=X;
     yPos=Y;
     double temp=Math.random();
     if(temp<=0.25){
         // bugColor =0;
      type = 0;
      setColor((byte)0);
      
     }
     else if(temp>0.25&&temp<=0.5)
     {
       //bugColor =1;
       type =1;
       setColor((byte)1);
      
      
     }
     else
     {
       //bugColor =2;
       type = 2;
       setColor((byte)2);
      
     }
   
    }
    /*public void type$color(){
     if(type == 0){
      setColor((byte)0);
     }
     if(type ==1){
      setColor((byte)1);
     }
     if(type ==2){
      setColor((byte)2);
     }
     
    }
*/   
    public void randomchangetype(){
     /*double r=Math.random();
     if(type == 0){
      if(r<=0.5){
       type = 1;
      }else{
       type =2;
      }
     }
     if(type ==1){
      if(r<=0.5){
       type = 0;
      }else{
       type =2;
      }
     }
     if(type ==2){
      if(r<=0.5){
       type = 1;
      }else{
       type =0;
      }
     }*/
     if(type==0){
      type=1;
     }
     if(type==1){
      type=2;
     }
     if(type==2){
      type=0;
     }
     
     //type$color();
     if(type == 0){
      setColor((byte)0);
     }
     if(type ==1){
      setColor((byte)1);
     }
     if(type ==2){
      setColor((byte)2);
     }
    }
    public void setColor(byte i){
     bugColor=i;
  }
   
public Object drawSelfOn (Raster r){
  //r.drawPointX$Y$Color (xPos, yPos, (byte)1);
  r.drawPointX$Y$Color (xPos, yPos, bugColor);
  return this;
    }
  }
public class girddiplayDemo extends GUISwarmImpl{
public ScheduleImpl displaySchedule;
public Object2dDisplay agentDisplay; //二维阵列对象类
public ZoomRaster agentRaster;
public int worldXSize = 80, worldYSize = 40;
public Grid2dImpl agentSpace;
    public ListImpl agentList;
    public double bugDensity = 0.2;
    agent Agent;
public girddiplayDemo (Zone azone){
  super(azone);
  }
public Object buildObjects() { //构建主体对象
  Colormap colormap;
  super.buildObjects();
  
  getControlPanel().setStateStopped();
  
  colormap = new ColormapImpl(getZone());  //创建调色板
  
  colormap.setColor$ToName((byte)0, "green");
  colormap.setColor$ToName((byte)1, "red");
  colormap.setColor$ToName((byte)2, "blue");
  
  agentSpace=new  Grid2dImpl(Globals.env.globalZone,worldXSize, worldYSize);
  agentList=new ListImpl(Globals.env.globalZone); //创建主体对象链表
   for (int y = 0; y < worldYSize; y++)
     for (int x = 0; x < worldXSize; x++)
    if ( Globals.env.uniformDblRand.getDoubleWithMin$withMax(0.0, 1) <= bugDensity){
     
       agent agentinst = new agent(Globals.env.globalZone, x, y);
      /* agentinst.randomchangetype();
       agentinst.type$color();*/
      
       agentSpace.putObject$atX$Y(agentinst, x, y); //把对象放在二维对象空间
       agentList.addLast(agentinst); //把对象加入链表
     }
  agentRaster = new ZoomRasterImpl (getZone(), "agentRaster");  //构建光栅
  agentRaster.setColormap (colormap);
  agentRaster.setZoomFactor (4);
  agentRaster.setWidth$Height(worldXSize,worldYSize);
  agentRaster.setWindowTitle ("agent World");
  agentRaster.pack();
     try{
       Selector sel = new Selector(Class.forName("agent"), "drawSelfOn",false);
       //Selector sel =getSelector("agent","drawSelfOn");
      
          agentDisplay = new Object2dDisplayImpl(getZone(), agentRaster,  agentSpace, sel);
          //二维空间中对象的显示Object2dDisplayImpl(Zone aZone,Raster r,Discrete2d c, Selector s)
       agentDisplay.setObjectCollection(agentList); //设置二维空间显示对象的集合
       }    catch (Exception e){}
   
     
     
          return this;
}
  public Object buildActions(){ //构建行为集
     super.buildActions();
  Selector sel;
  Agent = new agent(Globals.env.globalZone,worldXSize,worldYSize);
  ActionGroupImpl displayActions = new ActionGroupImpl(getZone());
   try{
   
    /*sel = new Selector(Class.forName("agent"), "type$color",false);
    displayActions.createActionTo$message(agentList, sel);*/
    sel = new Selector(Class.forName("agent"), "randomchangetype",false);
    displayActions.createActionTo$message(Agent, sel);
    //displayActions.createActionForEach$message(agentList, sel);
    sel = new Selector(agentRaster.getClass(), "drawSelf",false); //raster.drawself()绘制光栅,封装方法
    displayActions.createActionTo$message(agentRaster, sel); //加入行为集
    sel = new Selector(agentDisplay.getClass(), "display",false); //agentDisplay。display()显示所有主体对象
    displayActions.createActionTo$message(agentDisplay, sel);
   
    sel = new Selector(getActionCache().getClass(), "doTkEvents",false);  //捕获行为
    displayActions.createActionTo$message(getActionCache(), sel);
     }catch (Exception e){}
    displaySchedule = new ScheduleImpl(getZone(),1);//重复间隔时间1
  displaySchedule.at$createAction(0, displayActions);
  return this;
    }
    public Activity activateIn(Swarm swarmContext){ //激活行为集和行为调度表
  super.activateIn(swarmContext);
  displaySchedule.activateIn(this);
  return getActivity();
    }
public static void main (String[] args){
     Globals.env.initSwarm ("mygird", "first", "bug-swarm@santafe.edu", args);
        girddiplayDemo gri =  new girddiplayDemo (Globals.env.globalZone);
  gri.buildObjects();
  gri.buildActions();
  gri.activateIn(null);
   gri.go();
   gri.drop();
    }


public static Selector getSelector(String name, String method){
  Selector sel;
  try{
  sel = new Selector(Class.forName(name), method, false);
  } catch (Exception e){
  System.err.println("There was an error in creating a Selector for method "
  + method + "\nin Class " + name + ".");
  System.err.println (name + "." + method + " returns " + e.getMessage());
  System.err.println("The process will be terminated.");
  System.exit(1);
  return null;
  }
  return sel;
  }
  public static Selector getSelector(Object obj, String method){
  Selector sel;
  try{
  sel = new Selector(obj.getClass(), method, false);
  } catch (Exception e){
  System.err.println("There was an error in creating a Selector for method "
  + method + "\nin Class " +
  (obj.getClass()).getName() + ".");
  System.err.println ((obj.getClass()).getName() + "." + method
  + " returns " + e.getMessage());
  System.err.println("The process will be terminated.");
  System.exit(1);
  return null;
  }
  return sel;
  }
  }

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:SWARM warm ARM War collections import 程序

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-31 01:57