A RePast Tutorial by John T. Murphy, University of Arizona & Arizona State University (contact)

Creating a Color Map

To create a color map so that 0 dollars in a cell shows up as red, 1 as slightly lighter, and so forth, we insert some code into the buildDisplay method. We first need to create a ColorMap object; this will, of course, require a new 'import' statement. An interesting note is that the 'map' variable doesn't have to be declared in the main declaration section of the model object; it can be local in scope to the buildDisplay method. The object that is created and is assigned to this variable is passed as an argument to a method of another argument, which maintains a pointer to the object even when the buildDisplay method ends and the 'map' object falls out of scope. (If this doesn't make sense, read it carefully twice more and then, if it still doesn't, ignore it- it doesn't matter that much right now.)

The ColorMap object has a method that assigns mappings between values and colors. Values are just integers; colors are a 'Color' object that is defined from the Java library (note the new import statement) and is created using Red/Green/Blue values.

We need to create 16 of these, so we do it in a loop. We make sure that zero (no money present) is set to white, so white will be, in effect, the background color.

The Value2DDisplay links a grid of cells and a color map. The grid of cells we want to link is the moneySpace grid of the CarryDropSpace object; we need to insert a method into that object that returns just this grid.

Finally, add the Value2DDisplay to the Display Surface

CarryDropModel

// CarryDropModel
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");

    registerDisplaySurface("Carry Drop Model Window 1", displaySurf);
  }

  public void begin(){
    buildModel();
    buildSchedule();
    buildDisplay();
  }

  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();

    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);

    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);
  }

}

CarryDropAgent

// CarryDropAgent
package demo;

public class CarryDropAgent {

}

CarryDropSpace

// CarryDropSpace
package demo;

import uchicago.src.sim.space.Object2DGrid;


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));
      }
    }
  }

  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));
    }
  }

  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;
  }

}

Previous: Some Visible Output

Next: Generating a Display

Go to Table of Contents


A RePast Tutorial by John T. Murphy, University of Arizona & Arizona State University (contact)