CS 231

Hawks and Squirrels Application


For this project, please use a terminal instead of BlueJ to compile and run your programs. On a mac, you can use either the Terminal application or create an xterm in X Windows. To create your code, you can use BlueJ or any other IDE such as Xcode, NetBeans, or Eclipse. Other alternatives include the terminal-based editors vi and emacs and the application BBEdit, which is a nice editor designed for writing code.

Before starting, create a working directory in which to keep all of your java files. In a terminal, the command for creating a new directory with the name hawks is mkdir hawks.

To compile a java file, use the command javac MyClass.java. Once it is compiled, use the command java MyClass to execute the main method of MyClass. Note that your top level program class must have a main method. Any time you change a file, you will need to recompile it.

Keep all of your class files in the same directory so that Java can find them all as it needs them. Note that all of the java files will need to be compiled individually.


The purpose of this project is to create a graphical-user-interface [GUI] for your Hawks and Squirrels simulation using the java windowing tooklkit Swing. The interface should allow you to set the parameters for a simulation, step through the simulation, and visualize the results at each step. The interface should also let you re-initialize the interface and begin again with new parameters. If you want to figure it out on your own, stop reading and go for it. Otherwise, what you need to know is below or will come from class lectures.

Note that there is a lot of material on the java site and elsewhere describing how to use Swing. All of the Swing classes are also described in detail in the Java class library. (Unfortunately, very little of what I've found is really useful if you are just starting out. Once you have a sense of what you're doing, the Java class library documentation is probably the most helpful place to go to answer specific questions about a class.) What you really need to know will come from class exercises and the description below.

  1. Check out the Swing overview
  2. Create a HawkWindow class: Using the Circles.java file as a template, create a new class HawkWindow, that extends JFrame and will be the basis for the interface. The main method for HawkWindow will need to create a HawkWindow, then pack the window and set it to visible. Most of the work will be done in the constructor for the HawkWindow.
  3. Create a SimPanel class: Similar to the Circles case, you will want to have a private class that extends JPanel and overrides the paintComponent method in order to create an area to draw the Greenfield. You do not want to make the actual drawing function part of the private Panel class. Instead, the overridden paintComponent method should call a Simulation method with a Graphics object as an argument (possibly others as well). The Simulation method will do the actual drawing. You can even pass off the drawing task for individual entities to the Hawk and Squirrel classes. As you need access to the simulation in the panel class, this is a good place to have a field that holds the Simulation object.
  4. Create the layout for the HawkWindow: You will need a drawing panel and a toolbar. You may want to have several panels within the toolbar to create rows for various settings. There are many different parameters you might want to change for the simulation. Draw out a tree for your layout before starting. It is often necessary with dynamic layout systems to create several layers of groups in order to get the effect you want.

    As part of the toolbar layout, you will need to have at least two buttons that do stuff: reset and iterate. The reset button should be associated with fields for the initial numbers of hawks and squirrels, and whatever other parameters you want to be able to set in the window. The iterate button should be associated with a field for the number of iterations to execute.

  5. Attach actions to each button: Each button will need to have an instance of a class that implements the ActionListener interface. The simplest approach is to design separate private classes for each button. These classes should probably have fields for references to the SimPanel (for drawing) and to the text fields (for getting their values).
  6. Create the drawing functions: The main drawing function should be in the Simulation class, similar to the write() function already there. A simple approach is to color empty field grids green, color locations with Hawks brown, and color any remaining locations with squirrels grey. Then hook the simulation drawing method into the action classes and test it out.
  7. Demonstrate your working system to the prof in person.
  8. Final task: go to the US Patent Office web site. Pick a search term, or terms, that seem relevant to the final project. Spend a few minutes trying to find a patent that is related to some aspect of the project (interface, design environment, simulation, modeling, population dynamics). Summarize the patent in your own words as part of your project writeup, and discuss in a few sentences whether you think your project infringes on the patent in any way.



Follow the writeup instructions to create a web page for your assignment. Send the instructor an email with the code in a zip or tar file as well as a pointer to the URL for the writeup.