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.
Here are the links to the API's for the most recent versions of Java:
Setup
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.
Tasks
-
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( ArrayList<Cell> );
- public abstract void draw(Graphics g, int x, int y, int scale);
You will need to import the java.awt.Graphics package.
-
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( ArrayList<Cell> ) - 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); return; } - Optionally, add a toString method to the ClumpingCell class.
-
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<Cell> 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<Cell> 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.
- Compile and run the LandscapeDisplay class. Do you see the same behavior?
-
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.
- 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.
-
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.
-
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%.
-
Update your Simulation to incorporate the LifeCell option.
Create a movie of 50 steps of the LifeCell simulation.
Extensions
- 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.)
- Experiment with the rules for LifeCell. Can you make a simulation that seems stable?
- Make more interesting visualizations of your Cells.
- Use command line arguments to control more aspects of the simulation parameters.
Handin
Make your writeup for the project a wiki page in your personal space.
Your writeup should have a simple format.
- A brief description of the overall task, in your own words. This should be 2-3 sentences that describe the goals and purpose of the project.
- As explanation of your solution, focusing on the interesting bits. The interesting bits here are how you modified the basic design and implemented different simulations.
- Printouts, pictures, or results to show what you did. For this assignment, include your animated GIF movies.
- Other results to demonstrate extensions you undertook.
- A brief conclusion and description of what you learned.
- A list of people you worked with, including TAs, and professors. Include in that list anyone whose code you may have seen, such as those of friends who have taken the course in a previous semester.
Once you have written up your assignment, give the page the label:
cs231f14project4
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, put it in your folder on the Courses fileserver. Create a directory named