Thanks, I know I need a lot of 'fill in the blanks here'... I'll paste
in relevant code I have and would appreciate any filling in for help..
(I borrowed the core of this code from Steve Scott's primer pdf on
MASON, his turtle/llama2d program.
I've got the 3 main files - virus.java, virusController.java,
virusView.java
public class Virus implements Steppable {
private static final long serialVersionUID = 1;
private static ec.util.MersenneTwisterFast rnum = new
ec.util.MersenneTwisterFast();
private static int MAX_X = VirusController.gridWidth;
private static int MAX_Y = VirusController.gridLength;
private int xPos = 0;
private int yPos = 0;
private Color myColor;
public int getxPos() {
return xPos;
}
. . .
public void draw(Object object, Graphics2D graphics, DrawInfo2D
info) {
Virus obj = (Virus) object;
myColor = new
Color(rnum.nextFloat(),rnum.nextFloat(),rnum.nextFloat());
paint = myColor;
super.draw(obj, graphics, info) ???
}
public void step(SimState state) {
// get the VirusController state to access simulation variables
VirusController vc = (VirusController) state;
// calculate new location
move();
myColor = new
Color(rnum.nextFloat(),rnum.nextFloat(),rnum.nextFloat());
draw(this, ?, ?); ???
// update new location in sim space
vc.virusGrid.setObjectLocation(this, this.getxPos(),
this.getyPos());
}
}
package field_guide.virus;
import field_guide.llamas2D.Llama;
import sim.engine.SimState;
import sim.field.grid.Grid2D;
import sim.field.grid.IntGrid2D;
import sim.field.grid.SparseGrid2D;
import sim.util.IntBag;
public class VirusController extends SimState {
private static final long serialVersionUID = 1;
public VirusController(long seed) {
super(seed);
}
// declare the landscape grid
public static int gridWidth = 100;
public static int gridLength = 100;
public static int MAX_GRID_VALUE = 100;
// public static Random rnum = new Random(); // default Java random
number generator
private ec.util.MersenneTwisterFast rnum = new
ec.util.MersenneTwisterFast();
// declare the virus grid
public SparseGrid2D virusGrid = new
SparseGrid2D(gridWidth,gridLength);
// decclare array of Virus objects
private int populationSize = 100;
private Virus[] population = new Virus[populationSize];
public void start() {
super.start();
// set up patch landscape with radomlyseed resource values
seedGrid();
smoothGrid(4);
// set up a grid for the turtles
virusGrid= new SparseGrid2D(gridWidth, gridLength);
for(int i = 0; i < populationSize; i++) {
population[i] = new Virus();
virusGrid.setObjectLocation((Object) population[i],
population[i].getxPos(),
population[i].getyPos());
// put turtles on the Scheduler
schedule.scheduleRepeating(population[i]);
}
}
}
package field_guide.virus;
import java.awt.Color;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import sim.display.Console;
import sim.display.Controller;
import sim.display.Display2D;
import sim.display.GUIState;
import sim.engine.SimState;
import sim.portrayal.grid.FastValueGridPortrayal2D;
import sim.portrayal.grid.SparseGridPortrayal2D;
import sim.portrayal.simple.MovablePortrayal2D;
import field_guide.virus.VirusController;
public class VirusView extends GUIState {
public Display2D display;
public JFrame displayFrame;
public VirusView() {
super(new VirusController(System.currentTimeMillis()));
}
public VirusView(SimState state) {
super(state);
}
public static String getName() { return "Virus sim in MASON"; }
SparseGridPortrayal2D virusPortrayal = new SparseGridPortrayal2D();
// declare the portrayals we will use
// Initialize the portrayals
public void setupPortrayals() {
VirusController vc = (VirusController) state;
// display llamas as little green ovals rather than ImageIcon
virusPortrayal.setField(vc.virusGrid);
virusPortrayal.setPortrayalForAll(
new MovablePortrayal2D(
new
sim.portrayal.simple.OvalPortrayal2D(Color.orange)));
display.reset(); // reschedule the displayer
display.repaint(); // redraw the display
}
public void start() {
super.start();
setupPortrayals();
display.reset(); // reschedule displaer
display.repaint(); // redraw display
}
public void init(Controller c) {
super.init(c);
// Create a new 3D display
display = new Display2D(600, 600, this);
// make a display frame
displayFrame = display.createFrame();
c.registerFrame(displayFrame); // register frame to appear in
the Display list
displayFrame.setVisible(true);
// attach the resource and turtle portrayals
display.attach(elevationPortrayal, "Elevation");
display.attach(virusPortrayal, "Virus");
// specify the backdrop color
display.setBackdrop(Color.gray);
}
public static void main(String[] args) {
VirusView virusview = new VirusView();
Console c = new Console(virusview);
c.setVisible(true);
}
}
On 2015-05-06 16:08, Sean Luke wrote:
> On May 6, 2015, at 1:55 PM, Randolph Latimer <[log in to unmask]>
> wrote:
>
>> I'd like to have a 'color' field in an agent class, and have the color
>> field and agent color change at each step of the simulation.
>> Thanks,
>> Randy Latimer
>
> Sure, see pages 211 and 212 of the MASON manual here:
>
> https://cs.gmu.edu/~eclab/projects/mason/manual.pdf
>
> Sean
|