这一章我们来讨论下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)); //随机改变
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)); //随机改变