现在的位置: 首页 > 综合 > 正文

一步一步学Repast 第二章(把界面显示出来2)

2018年05月21日 ⁄ 综合 ⁄ 共 3302字 ⁄ 字号 评论关闭

上一章,我们用了TextDisplay和Value2DDisplay来显示界面,Value2DDisplay用 Discrete2DSpace和ColorMap配合来进行显示。现在我们讨论下 Object2DDisplay,看看它的构造函数:uchicago.src.sim.gui.Object2DDisplay.Object2DDisplay(Discrete2DSpace)
可以看出,没有了ColorMap,那么它是如何进行绘制的呢?按照我们的推论,肯定是Discrete2DSpace里的Object实现了某种绘制的接口。比如  :
new Object2DGrid(50,50).putObjectAt(20,20,myObject)    这里的myObject一定已经实现了某种绘制功能。
查看uchicago.src.sim.gui 包,我们发现,有一个叫Drawable接口, 有如下描述
Interface for those objects in 2D spaces that wish to be drawn on a DisplaySurface by a Displayable. Typically agents will be implement this interface and thus when added to a space can be drawn when the space is drawn. Integers and other Numbers can be mapped to colors and drawn that way, so this interface does not apply to them.
大致意思是,Agents实现了这个接口后,可以在加入到space的时候进行绘制,整型和其他数字类型能被ColorMap绘制,因此不需要这个接口。

我们现在来写一个简单的Agent

package wxy;

import java.awt.Color;

import uchicago.src.sim.gui.Drawable;
import uchicago.src.sim.gui.SimGraphics;

public class RoundAgent implements Drawable {
    
    
private int x;
    
private int y;
    
    
public RoundAgent(int x,int y){
        
this.x = x;
        
this.y = y;
        
    }


    @Override
    
public void draw(SimGraphics G) //当把这个Agent加入到space的时候对其进行绘制,画一个圈。
        G.drawCircle(Color.WHITE);
    }


    @Override
    
public int getX() //这2个方法肯定是用来指定在哪里绘制这个圈
        return x;
    }


    @Override
    
public int getY() {
        
return y;
    }

    
}

Model主程序


package wxy;

import uchicago.src.sim.engine.Schedule;
import uchicago.src.sim.engine.SimInit;
import uchicago.src.sim.engine.SimModelImpl;
import uchicago.src.sim.engine.SimpleModel;
import uchicago.src.sim.gui.DisplaySurface;
import uchicago.src.sim.gui.Object2DDisplay;
import uchicago.src.sim.space.Object2DGrid;

public class DrawableModel extends SimpleModel {

  
private int worldXSize = 50;
  
private int worldYSize = 50;
  
  
private DisplaySurface displaySurf;


  
public void setup(){
      
super.setup();
      
      
if (displaySurf != null){
          displaySurf.dispose();
      }

      displaySurf 
= null;

      displaySurf 
= new DisplaySurface(this"DrawableObject");

      registerDisplaySurface(
"DrawableObject", displaySurf);
  }


  
public void begin(){
    buildModel();
    buildSchedule();
    buildDisplay();
    
    displaySurf.display();
  }


  
public void buildModel(){
      agentList.add(
new RoundAgent(15,15));
  }


  
public void buildSchedule(){
      
super.buildSchedule();
  }


  
public void buildDisplay(){

      Object2DDisplay displayAgents 
= new Object2DDisplay(new Object2DGrid(worldXSize,worldYSize));
      displayAgents.setObjectList(agentList);
      
    
//我们这里没有使用Object2DGrid里的putObjectAt(x, y, object)方法,直接使用了displayAgents.setObjectList(agentList);效果是一样的,只要对Agent的X,Y
    
//进行初始化即可。
      
      displaySurf.addDisplayable(displayAgents, 
"Round");
      
  }



  
public String[] getInitParam(){
    String[] initParams 
= {"WorldXSize""WorldYSize" };
    
return initParams;
  }



  
public int getWorldXSize(){
    
return worldXSize;
  }


  
public void setWorldXSize(int wxs){
    worldXSize 
= wxs;
  }


  
public int getWorldYSize(){
    
return worldYSize;
  }


  
public void setWorldYSize(int wys){
    worldYSize 
= wys;
  }


  
public static void main(String[] args) {
    SimInit init 
= new SimInit();
    DrawableModel model 
= new DrawableModel();
    init.loadModel(model, 
""false);
  }


}

下面我们来看看SimGraphics 有那些方法可以使用

基本上涵盖了所有我们需要使用的绘制功能。

更多repast资料查阅
repast-tool

抱歉!评论已关闭.