这一章我们来讨论下CarryDropSpace,它主要负责界面上的工作。来看一下它的代码:
- // CarryDropSpace.java
- package demo;
- import uchicago.src.sim.space.Object2DGrid; //这是space包中最常见的类,它是以一种非连续的格子来存放object,以x轴和y轴来标识。
- public class CarryDropSpace ...{
- private Object2DGrid moneySpace;
- public CarryDropSpace(int xSize, int ySize)...{
- moneySpace = new Object2DGrid(xSize, ySize);
- for(int i = 0; i < xSize; i++)...{
- for(int j = 0; j < ySize; j++)...{
- moneySpace.putObjectAt(i,j,new Integer(0)); //把所有的格子都放满 Integer(0) 这个对象。
- }
- }
- }
- public void spreadMoney(int money)...{
- // Randomly place money in moneySpace
- for(int i = 0; i < money; i++)...{
- // Choose coordinates
- int x = (int)(Math.random()*(moneySpace.getSizeX()));
- int y = (int)(Math.random()*(moneySpace.getSizeY()));
- // Get the value of the object at those coordinates
- int currentValue = getMoneyAt(x, y);
- // Replace the Integer object with another one with the new value
- moneySpace.putObjectAt(x,y,new Integer(currentValue + 1)); //随机改变money个格子里的对象为 Integer(1)
- }
- }
- public int getMoneyAt(int x, int y)...{
- int i;
- if(moneySpace.getObjectAt(x,y)!= null)...{
- i = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
- }
- else...{
- i = 0;
- }
- return i;
- }
- public Object2DGrid getCurrentMoneySpace()...{
- return moneySpace;
- }
- }
- // CarryDropModel.java
- package demo;
- import java.awt.Color;
- import uchicago.src.sim.engine.Schedule;
- import uchicago.src.sim.engine.SimInit;
- import uchicago.src.sim.engine.SimModelImpl;
- import uchicago.src.sim.gui.DisplaySurface;
- import uchicago.src.sim.gui.ColorMap;
- import uchicago.src.sim.gui.Value2DDisplay;
- public class CarryDropModel extends SimModelImpl ...{
- // Default Values
- private static final int NUMAGENTS = 100;
- private static final int WORLDXSIZE = 40;
- private static final int WORLDYSIZE = 40;
- private static final int TOTALMONEY = 1000;
- private int numAgents = NUMAGENTS;
- private int worldXSize = WORLDXSIZE;
- private int worldYSize = WORLDYSIZE;
- private int money = TOTALMONEY;
- private Schedule schedule;
- private CarryDropSpace cdSpace;
- private DisplaySurface displaySurf;
- public String getName()...{
- return "Carry And Drop";
- }
- public void setup()...{
- System.out.println("Running setup");
- cdSpace = null;
- if (displaySurf != null)...{
- displaySurf.dispose();
- }
- displaySurf = null;
- displaySurf = new DisplaySurface(this, "Carry Drop Model Window 1"); //为这个model创建一个DisplaySurface对象,当然,这个对象的一些性质肯定是依赖这个model的参数的。创建出来的DisplaySurface只是用了这个model的一些参数并没有相互绑定依赖,是一种初始化才干的事情。
- registerDisplaySurface("Carry Drop Model Window 1", displaySurf); //让这个model注册一个DisplaySurface,把DisplaySurface与这个model绑定起来
- }
- public void begin()...{
- buildModel();
- buildSchedule();
- buildDisplay();
- displaySurf.display();
- }
- public void buildModel()...{
- System.out.println("Running BuildModel");
- cdSpace = new CarryDropSpace(worldXSize, worldYSize);
- cdSpace.spreadMoney(money);
- }
- public void buildSchedule()...{
- System.out.println("Running BuildSchedule");
- }
- public void buildDisplay()...{
- System.out.println("Running BuildDisplay");
- ColorMap map = new ColorMap(); //顾名思义,这是一个color板,键值对应形式。
- for(int i = 1; i<16; i++)...{
- map.mapColor(i, new Color((int)(i * 8 + 127), 0, 0));
- }
- map.mapColor(0, Color.white);
- Value2DDisplay displayMoney =
- new Value2DDisplay(cdSpace.getCurrentMoneySpace(), map); // 创建一个Value2DDisplay,用来将Discrete2DSpace与ColorMap绑定。
- displaySurf.addDisplayable(displayMoney, "Money");
- }
- public Schedule getSchedule()...{
- return schedule;
- }
- public String[] getInitParam()...{
- String[] initParams = ...{ "NumAgents" , "WorldXSize", "WorldYSize", "Money" };
- return initParams;
- }
- public int getNumAgents()...{
- return numAgents;
- }
- public void setNumAgents(int na)...{
- numAgents = na;
- }
- 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 int getMoney() ...{
- return money;
- }
- public void setMoney(int i) ...{
- money = i;
- }
- public static void main(String[] args) ...{
- SimInit init = new SimInit();
- CarryDropModel model = new CarryDropModel();
- init.loadModel(model, "", false);
- }
- }
来看下面的列表 这些是DisplaySurface可以调用的方法是从 JComponent继承过来的

重点看一下 addDisplayable()这个方法,里面有个参数Displayable。这是一个接口,许多想要显示出来的类都必须实现该接口,由DisplaySurface显示出来。我们来看看那些类实现了这个接口

上面我们是用了 Value2DDisplay这个类来显示的。比如还可以在界面里添加Text
TextDisplay displayText = new TextDisplay(10,20,Color.black);
displayText.addLine("this is some text");
displaySurf.addDisplayable(displayText, "TestText");
现在应该对整个流程的框架有所了解了吧,以后的模拟都是基于这个基本框架来写的。

留言