



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Due: 11:59pm Wednesday, November 25
Overview
You will create a simulation program that controls the movement of three types of Critters. The program will allow any number of Critters to be added to the world, which is the canvas of the applet. The Critter to be added is selected by pressing on the corresponding button for the Critter. The simulation can be started or stopped at any time, using the Start and Stop buttons. Additionally, all Critters can be removed from the simulation and from the canvas with the Clear button. Critters can be added at anytime, whether the simulation is running or not. If added in the running state, new Critters should start responding immediately. The simulation should only run if there are two or more Critters currently existing on the canvas.
GUI and Controller
Here is the screenshot of the how the program looks when it is first started up:
Your controller class should be named CrittersController.java. As the very first step, create all the GUI elements and lay them out as shown in the screenshot above. You will need to make use of multiple JPanels to create the layout, and add the panels to CrittersController. The CrittersController should extend WindowController and implement ActionListener. As an ActionListener, it should add itself to the 6 buttons that it will create for the GUI. In addition to the 6 buttons, there are two JLabels: one at the top indicating the status of the simulation, and one at the bottom indicating which Critter is currently selected to be placed on the canvas.
The status label at the top will contain one of three status messages at any given time. Use the following rules to determine what status message to display:
Note that it is possible for the status message to change by clicking ANY three of the control buttons on the top or by clicking on the canvas to place new Critters. Thus, you will probably want to create a method to which you delegate the task of figuring out and setting the status message, instead of having this logic spread out in different places. For example, hitting the Clear button in the started state should set the status to tell the user to add more critters. Adding two or more Critters in the started state to an initially empty canvas should change the status to “Simulation is running”. And so on. Simply call your method any such time it is needed, which will set the status based on the three rules above.
Once the Chaser, Runner, or Random button is clicked, clicking anywhere on the canvas will place that selected Critter on the clicked location. A Critter’s location is considered to be the point corresponding to its center. When placing Critters, you can switch between one Critter and another at any time. Also, when a Critter button is clicked, the creature selection label, which initially reads “Select which critter to place:”, should be changed to say “Click on canvas to place a [Critter]”, where [Critter] is Chaser, Runner, or Random, depending on which Critter was clicked.
When starting up, the program should begin in the start state.
Critter appearance and movement
Chaser
The Chaser will be drawn as a red circle using a FilledOval object. The diameter of the circle should be 15 pixels. The Chaser’s behavior is to move towards the Critter that is closest to it. However, a Chaser should NEVER chase after another Chaser. Thus, in determining which Critter is the closest to a Chaser, you should ignore all other Chasers.
Runner
The Runner will be drawn as a green square using a FilledRect object. The size of the square is 15x15 pixels. The Runner’s behavior is to move away from the Critter that is closest to it. However, if a Runner ever touches any of the four borders of the canvas, it should be randomly moved to a different location inside the canvas.
Random
The Random will be drawn as a blue X using two individual Line objects. The bounding box formed by the X should be 15x15 pixels. The Random’s behavior is to move randomly from its current location to a nearby location. Thus, the movement of the Random is not influenced by any other Critters around it. However, Chasers and Runners can still be influenced by nearby Randoms.
a boolean flag in CrittersSimulator, along with a setter and getter for the flag, to allow the simulation to be stopped and started. CrittersSimulator should run the simulation only in the started state, and only when there are 2 or more Critters on the canvas. The bulk of the work that CrittersSimulator’s run() method has to do is to calculate the distance from every Critter to every other Critter (with the Chaser-Chaser exception mentioned earlier), and then call the reactTo() method for each Critter after having determined the closest Critter to it.
For the run method, please stick to the following structure:
public void run(){ while(true){ pause(DELAY);
/* All the rest of your work should go here */
} }
Note the pause(DELAY). Please leave this pause() at the top level of the infinite while-loop as you see here, and nowhere else. Regardless of whether the simulation is currently running or stopped, the pause() should always be executed inside the infinite while-loop for the run() method. You can play around with different values of DELAY when you are doing the assignment, but please make sure to set DELAY to somewhere around 40-50 before you turn in the assignment.
Extra Credit
Add a fourth Critter called Imitator. The Imitator will be drawn as a black triangle using three Line objects. The bounding box created by the triangle should be 15x15 pixels. The Imitator’s behavior is to mimic the movement of the Critter that is closest to it. While imitating that Critter, the Imitator should change its color to the color of the Critter that it is imitating (but the shape should always stay a triangle). So, the Imitator should make its next move be exactly what the closest Critter would do if the closest Critter were at the same position as the Imitator. Like the Chaser-Chaser exception mentioned earlier in the assignment, all other Imitators should be ignored when finding the closest Critter to an Imitator. Remember to add a corresponding Imitator button and select creature text to the GUI controller.
You need to provide a README file with this assignment. Make sure that the filename is "README". Note that this is in all caps and has no extension. In your README, you should
Files to turn in
Due: 11:59pm Wednesday, November 25
START EARLY!!!