Hawks & Squirrels Application

Anita Buragohain & Adam Lowenstein

 

A b s t r a c t

 

The goal of the Hawks & Squirrels Application was to create a graphical user interface for the program created in our previous project in which we simulated a predator and prey situation with hawks and squirrels. To create the interface in this project, we implemented the Java Swing API using the Windows terminal. The graphical user interface, or GUI, contained two major components: the window in which the simulation was visualized, and the toolbars at the bottom that controlled the user-specified parameters. We elected to give the user control over the number of hawks, the number of squirrels, and the number of iterations used in the simulation. The user either can click a “Run Once” button to run one iteration of the simulation or can set the simulation to run any number of iterations (although iterations over 10,000 tend to slow the computer significantly). While ensuring an error-free project was our goal as always, we also focused on usability in the creation of this project. Overall, the simulation ran as it should, and the final user interface was clean, informative, and easy to use, so we deemed this project a success.

 

P r o j e c t    D e s c r i p t i o n

 

As described above, the problem that we were faced with in this project was the creation of a visualization of the hawks and squirrels simulation. The hawks and squirrels simulation was of one of predator versus prey. Last time, we looked at the changes in population of both species given their initial populations, their birth and death rates, and their survival skills (hawks must hunt to survive; squirrels must dodge the hunt to survive.) Our write-up for that project is available here. We were able to obtain some sort of conclusive data by analyzing each population over time after adjusting some or all of these parameters, but rarely was the step-by-step evolution of the scenario understood fully. Thus, developing a clear visualization of the simulation was a problem that we were anxious to overcome.


What we found out, however, was that the problem actually consisted of two parts: first, we had to create a functional visualization of the simulation, but then, we weren’t finished—we still had to make sure that the GUI was effective at conveying information and controlling the simulation. We created various extensions to achieve this goal. Among these, we implemented an integer-based (RGB) color scheme that set hawks (blue circles) against squirrels (yellow-green circles) on a black background. Furthermore, if a hawk and a squirrel were ever at the same location on the grid at a given time, that position contained a red square to emphasize the competition between the species (note that no colors were changed if only multiple hawks or multiple squirrels were on the same location—only a competition between species merited a red square). To determine whether to add a red square, we added a method to one of our earlier classes, EntityList, that checked the list of entities (either hawks or squirrels) at every location and returned true ONLY if at least one hawk and one squirrel is found. We also added a draw() method to the Simulator class that actually created the shapes seen in the visualization.

 

T h e o r y    a n d    A l g o r i t h m s

 

To create the graphical user interface, we first had to import the Java Swing API. We used Swing to extend JFrame, from which all our visualization components (buttons, text fields, windows, toolbars, etc.) were constructed. There was not as much in-depth coding involved in this project (much more emphasis was placed on the GUI), but one major algorithm was created within the Simulator class to integrate the simulation into the window. This algorithm, called draw(), was based on the provided draw() method from Circles.java but was extended greatly. In the code below, one can see the method we derived for deciding what color and shape to place at each location in the visualization:

 

 

The visualization itself was created within a class called HawkWindow, an extension of the JFrame class. We created three ActionListeners that, when initiated by the JButtons (with parameters given in the JTextFields), each did one of three things:

 

1.      IterateListener iterated a user-specified number of times given user-specified values for the hawk and squirrel populations. As it iterated, it updated the three corresponding fields (hawk population, squirrel population, and number of iterations thus far) displayed in the toolbar.

2.      IterateOnceListener ran the simulation for only one iteration and also updated the display fields.

3.      ResetListener restarted the simulation given the parameters specified by the hawk- and squirrel-population textboxes. As an extension, when the visualization first ran, the “Reset” button was displayed as “Populate,” rather than “Reset,” in order to communicate more effectively with the user. We also have all three fields set to zero initially so as to create a truly user-defined experience.


Since IterateListener was affected by the user-specified number of iterations (unlike IterateOnceListener), we implemented a dynamically-updating button label for the button that executed the specified number of iterations. Instead of reading “Run Multiple Times,” the button read, “Run X Times,” where X was equal to the number of iterations specified by the user. We attempted to make it even more dynamic by updating the button text as the user was typing, but unfortunately, we were unable to succeed in this regard.

 

R e s u l t s

 

This project produced excellent results. As the goal of the project was to create a visualization, no new data was created. Instead, we took the data from a previous project and projected it into a stunning visual experience. Originally, our program printed an alphanumeric display to the terminal window to give some indication of the current scenario in the simulation:

 

 

By implementing the Java Swing API, we were able to improve our visualization drastically. We did so by creating shapes and colors to represent hawks, squirrels, and potential hunts (hawks and squirrels at the same location). Below is an image of one particular visualization involving 400 squirrels and 200 hawks:

 

 

As noted before, hawks are represented by blue circles, squirrels are represented by yellow-green circles, and locations containing at least one hawk and one squirrel are represented by red squares. There is no separate representation of locations containing only multiple hawks or multiple squirrels, as the relationship within the species is not one that could be described as predator-prey. Thus, it would be unnecessarily confusing to represent them on the map.

 

The second part of creating the GUI emerged as an equally difficult aspect of the project, one that involved very precise coding and labeling to perfect. This part was that in which we created the actual interface—the buttons, the text fields, and the display that make the GUI so interactive. A screenshot of the full GUI is shown below:

 

 

The dynamic button text is clearly visible in the image above.

 

D i s c u s s i o n    a n d    S u m m a r y

 

We believe that our project was very successful. Though it was at first daunting to take an extremely complex and intricate project (i.e., the hawks and squirrels project that we created earlier this semester) and represent it entirely in a small visualization window, we were able not only to achieve the goal but also to do it in a usable and aesthetically-pleasing way.

 

As discussed in a Colby computer science lecture earlier this year, one of the main reasons that computer programs fail is for a lack of usability and reliability. We spent extra time making our project both usable and reliable. For example, when we were confused about what was happening on screen, as an extension we added an extra line to the toolbar that displays the number of hawks, the number of squirrels, and the number of iterations thus far. This extension in turn gave the user a much greater understanding of the visualization and its consequences.

 

We found this project to be a great addition to the hawks and squirrels application for a variety of reasons, but primarily because it allowed us to visualize clearly the large amounts of data that the simulation generated. The predator and prey relationship is one that has governed the evolution of life on earth, and we are now one step closer to creating an accurate representation of a real-world predator and prey simulation.

 

U S    P a t e n t    S e a r c h

 

We searched for various patents, but the most intriguing and project-related patent we discovered was “Change detection in a graphical user interface.” One of our main extensions was the use of dynamically-updating button text. However, the button text did not update immediately following a user’s input; rather, the user was forced to push the button once before it would “read in” the data and update the label on the button.

 

This patent (number 7,237,204) deals with this issue on much broader terms. It specifies three processes: one that creates and sends data, one that reads in the data to determine whether it has changed, and a final process to update the display with the data. We do not believe that our project infringes on this patent because we were unable to make the first and third processes connect simultaneously. In other words, we could read data from the text field, but not until the button was pressed. Similarly, we could update the display on the button, but not until the button had been pressed to “re-send” the data—we could not send the data immediately as it was input (i.e., send it directly as the user typed it). The patent for “Change detection in a graphical user interface” is especially relevant to our project because it provides a “missing link” that would allow our project to be potentially more usable for many users.