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

标签:, , , , , , , , ,

这一章我们来讨论下CarryDropSpace,它主要负责界面上的工作。来看一下它的代码:

  1. // CarryDropSpace.java
  2. package demo;
  3.  
  4. import uchicago.src.sim.space.Object2DGrid; //这是space包中最常见的类,它是以一种非连续的格子来存放object,以x轴和y轴来标识。
  5.  
  6.   
  7.  
  8.  
  9. public class CarryDropSpace ...{
  10. private Object2DGrid moneySpace;
  11.  
  12.   public CarryDropSpace(int xSize, int ySize)...{
  13.     moneySpace = new Object2DGrid(xSize, ySize);
  14.  
  15.     for(int i = 0; i < xSize; i++)...{
  16.       for(int j = 0; j < ySize; j++)...{
  17.         moneySpace.putObjectAt(i,j,new Integer(0))//把所有的格子都放满 Integer(0) 这个对象。
  18.       }
  19.     }
  20.   }
  21.  
  22.   public void spreadMoney(int money)...{
  23.     // Randomly place money in moneySpace
  24.     for(int i = 0; i < money; i++)...{
  25.  
  26.       // Choose coordinates
  27.       int x = (int)(Math.random()*(moneySpace.getSizeX()));
  28.       int y = (int)(Math.random()*(moneySpace.getSizeY()));
  29.  
  30.       // Get the value of the object at those coordinates
  31.       int currentValue = getMoneyAt(x, y);
  32.       // Replace the Integer object with another one with the new value
  33.       moneySpace.putObjectAt(x,y,new Integer(currentValue + 1));   //随机改变money个格子里的对象为 Integer(1)
  34.      }
  35.   }
  36.  
  37.   public int getMoneyAt(int x, int y)...{
  38.     int i;
  39.     if(moneySpace.getObjectAt(x,y)!= null)...{
  40.       i = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
  41.     }
  42.     else...{
  43.       i = 0;
  44.     }
  45.     return i;
  46.   }
  47.  
  48.   public Object2DGrid getCurrentMoneySpace()...{
  49.     return moneySpace;
  50.   }
  51.  
  52. }

  1. // CarryDropModel.java
  2. package demo;
  3.  
  4. import java.awt.Color;
  5.  
  6. import uchicago.src.sim.engine.Schedule;
  7. import uchicago.src.sim.engine.SimInit;
  8. import uchicago.src.sim.engine.SimModelImpl;
  9. import uchicago.src.sim.gui.DisplaySurface;
  10. import uchicago.src.sim.gui.ColorMap;
  11. import uchicago.src.sim.gui.Value2DDisplay;
  12.  
  13. public class CarryDropModel extends SimModelImpl ...{
  14.   // Default Values
  15.   private static final int NUMAGENTS = 100;
  16.   private static final int WORLDXSIZE = 40;
  17.   private static final int WORLDYSIZE = 40;
  18.   private static final int TOTALMONEY = 1000;
  19.  
  20.   private int numAgents = NUMAGENTS;
  21.   private int worldXSize = WORLDXSIZE;
  22.   private int worldYSize = WORLDYSIZE;
  23.   private int money = TOTALMONEY;
  24.  
  25.   private Schedule schedule;
  26.  
  27.   private CarryDropSpace cdSpace;
  28.  
  29.   private DisplaySurface displaySurf;
  30.  
  31.   public String getName()...{
  32.     return "Carry And Drop";
  33.   }
  34.  
  35.   public void setup()...{
  36.     System.out.println("Running setup");
  37.     cdSpace = null;
  38.  
  39.     if (displaySurf != null)...{
  40.       displaySurf.dispose();
  41.     }
  42.     displaySurf = null;
  43.  
  44.     displaySurf = new DisplaySurface(this, "Carry Drop Model Window 1")//为这个model创建一个DisplaySurface对象,当然,这个对象的一些性质肯定是依赖这个model的参数的。创建出来的DisplaySurface只是用了这个model的一些参数并没有相互绑定依赖,是一种初始化才干的事情。
  45.  
  46.     registerDisplaySurface("Carry Drop Model Window 1", displaySurf)//让这个model注册一个DisplaySurface,把DisplaySurface与这个model绑定起来
  47.   }
  48.  
  49.   public void begin()...{
  50.     buildModel();
  51.     buildSchedule();
  52.     buildDisplay();
  53.  
  54.     displaySurf.display();
  55.   }
  56.  
  57.   public void buildModel()...{
  58.     System.out.println("Running BuildModel");
  59.     cdSpace = new CarryDropSpace(worldXSize, worldYSize);
  60.     cdSpace.spreadMoney(money);
  61.   }
  62.  
  63.   public void buildSchedule()...{
  64.     System.out.println("Running BuildSchedule");
  65.   }
  66.  
  67.   public void buildDisplay()...{
  68.     System.out.println("Running BuildDisplay");
  69.  
  70.     ColorMap map = new ColorMap();   //顾名思义,这是一个color板,键值对应形式。
  71.  
  72.     for(int i = 1; i<16; i++)...{
  73.       map.mapColor(i, new Color((int)(i * 8 + 127), 0, 0));
  74.     }
  75.     map.mapColor(0, Color.white);
  76.  
  77.     Value2DDisplay displayMoney =
  78.         new Value2DDisplay(cdSpace.getCurrentMoneySpace(), map); // 创建一个Value2DDisplay,用来将Discrete2DSpace与ColorMap绑定。
  79.  
  80.     displaySurf.addDisplayable(displayMoney, "Money");
  81.  
  82.   }
  83.  
  84.   public Schedule getSchedule()...{
  85.     return schedule;
  86.   }
  87.  
  88.   public String[] getInitParam()...{
  89.     String[] initParams = ...{ "NumAgents" , "WorldXSize", "WorldYSize", "Money" };
  90.     return initParams;
  91.   }
  92.  
  93.   public int getNumAgents()...{
  94.     return numAgents;
  95.   }
  96.  
  97.   public void setNumAgents(int na)...{
  98.     numAgents = na;
  99.   }
  100.  
  101.   public int getWorldXSize()...{
  102.     return worldXSize;
  103.   }
  104.  
  105.   public void setWorldXSize(int wxs)...{
  106.     worldXSize = wxs;
  107.   }
  108.  
  109.   public int getWorldYSize()...{
  110.     return worldYSize;
  111.   }
  112.  
  113.   public void setWorldYSize(int wys)...{
  114.     worldYSize = wys;
  115.   }
  116.  
  117.   public int getMoney() ...{
  118.     return money;
  119.   }
  120.  
  121.   public void setMoney(int i) ...{
  122.     money = i;
  123.   }
  124.  
  125.   public static void main(String[] args) ...{
  126.     SimInit init = new SimInit();
  127.     CarryDropModel model = new CarryDropModel();
  128.     init.loadModel(model, "", false);
  129.   }
  130.  
  131. }

来看下面的列表  这些是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");

现在应该对整个流程的框架有所了解了吧,以后的模拟都是基于这个基本框架来写的。







留言