CS 231: Assignment #4

Animating Simulations

This week we're going to add a visual component to our simulations. We'll also re-organize the code to make use of an abstract class to represent the parent of different Cell types.

Documentation for Java 1.6 is located at: Java 1.6 SE API


Download LandscapeDisplay.java. This file implements a simple window into which you can draw visual representations of cells.

The overall organization will be similar to project 3: a Simulation has a Landscape, a Landscape has a LinkedList of Cell Objects, and Cell objects are in charge of updating their own state. This week, however, the basic Cell class will become an abstract class, and each simulation will extend the Cell class in a different way. Using this organization, we'll recreate the simulations of the past two projects (including the Game of Life) but display them visually in a window so you can see the simulation occur in real time on a single window.


  1. Copy over your Cell.java file from last week. Make Cell an abstract class and give it the following abstract functions.
    • public abstract boolean isNeighbor( Cell cell );
    • public abstract void updateState( Landscape scape );
    • public abstract void draw(Graphics g, int x, int y, int scale);

    You will need to import the java.awt.Graphics package.

    [Optionally, add a field to represent the type of the Cell and modify the constructor arguments to include an integer type and create accessors for the Type field. This field should receive a unique number from each child class.]

  2. Create a class ClumpingCell that extends Cell. It should have a constructor with two arguments (x and y location) that calls the parent constructor. The ClumpingCell class also needs to implement the three abstract functions.
    • public boolean isNeighbor( Cell cell ) - returns true if the argument cell is within some radius of this cell. Use something between 2.0 and 5.0 to start.
    • public void updateState( Landscape scape ) - updates the state of the ClumpingCell. This should be the same rule as the Cell class from project 3. Have the Cell move no more than +/- 3 to start. Note that this means the Cell's motion can be any continuous value between +3 and -3.
    • public void draw(Graphics g, int x0, int y0, int scale) - use the following definition to start. You are free to change it to other shapes and colors, as you wish.
        public void draw(Graphics g, int x0, int y0, int scale) {
          int x = x0 + (int)(this.getX() * scale);
          int y = y0 + (int)(this.getY() * scale);
          g.setColor(new Color(0.2f, 0.6f, 0.3f));
          g.fillRect(x, y, scale, scale);
    • Optionally, add a toString method to the ClumpingCell class.
  3. Update your Landscape class.
    • Make your width and height fields be type double. Update your current constructor and also create a Constructor that takes two double arguments. Then modify your getRows and getCols to return the height and width rounded to integers.
    • public double getHeight() - returns the height as a double.
    • public double getWidth() - returns the width as a double.
    • public ArrayList getNeighbors(Cell qcell) - returns the list of neighbors of qcell. The new getNeighbors function should make use of the isNeighbor method of a Cell. It should implement the following algorithm.
      Create an ArrayList to hold the neighbors
      For each Cell a in the agents list
        if a is a neighbor of qcell
          add a to the list of neighbors
      return the list of neighbors
    • Update your main function so it adds ClumpingCell agents to the landscape. Run the simulation to test it.
  4. Compile and run the LandscapeDisplay class. Do you see the same behavior?
  5. Create a new Simulation.java class that follows the main function of LandscapeDisplay but uses the save method of the LandscapeDisplay class to save a series of images of the simulation. You will want to save the images with names so they appear in order when you type ls. I suggest using .png as the suffix of the filename. Then use the convert tool to create an animated GIF of the simulation.

    convert -delay 60 *.png mysim.gif

    Create a movie of 50 steps of the ClumpingCell simulation.

  6. Create a new child class of Cell called PreferenceCell. Use it to implement the same simulation as Cell2 from project 3, but use three categories. In your draw method, draw each category as a different color.
  7. Update your Simulation class so that you can control from the command line which type of Cell gets added to the Landscape and simulated.

    Create a movie of 50 steps of the PreferenceCell simulation.

  8. Create a new child class of Cell called LifeCell. Use it to implement the following update rules.
    • If a LifeCell has at least 3 neighbors, it is alive and refreshed, which means it has exactly three lives.
    • If a LifeCell has 5 or 6 neighbors, it can create a new LifeCell nearby, which should be added to the list of agents.
    • If the LifeCell has fewer than 3 neighbors, then it loses one of its lives. A LifeCell with zero lives should be removed from the list of agents.
    • A LifeCell that has between 3, 4, or 5 neighbors has a 1% chance of moving each round. A LifeCell with fewer than 3 or more 5 will move.

    You may want to remove cells outside the viewing window or your simulation will likely grind to a halt. You will also want to add a removeAgent method to your Landscape. The experiment is a bit more interesting if you make the chance of moving more like 10%.

  9. Update your Simulation to incorporate the LifeCell option.

    Create a movie of 50 steps of the simulation.


  1. Make another type of Simulation. Can you implement the Game of Life in this organization? (Hint: you can, but you will need to be able to clone a Landscape.)
  2. Experiment with the rules for LifeCell. Can you make a simulation that seems stable?
  3. Make more interesting visualizations of your Cells.
  4. Use command line arguments to control more aspects of the simulation parameters.


Make your writeup for the project a wiki page in your personal space.

Your writeup should have a simple format.

Once you have written up your assignment, give the page the label:


You can give any page a label when you're editing it using the label field at the bottom of the page.

Do not put code on your writeup page or anywhere it can be publicly accessed. To hand in code, attach it to an email and send it to the prof.