The goal of this lab is to design windows requesting input from the user.
Today we go back to a simulation of the same sort as the predator-prey example and the ball-catching project. We will have a grid inhabited by agents, each acting in their own way.
The grid we'll be working with today will be inhabited by Skeets. A Skeet is an object that moves about the grid in a way determined by the following parameters:
Make a directory for this lab and copy the appropriate files from the course directory.
cp /homes/tvandrun/Public/cs235/lab15/* .
Then compile and run the program Skeets
.
Notice that there is a jar
file;
you'll need to refer to it on the command line when you
compile and run.
javac -cp simulation.jar:. Skeets.java java -cp simulation.jar:. Skeets
By default there is a single Skeet.
If you inspect the code in Skeets.java
,
you'll see that the Skeet is added to the grid
in the line
model.setAgentAt(100, 100, new Skeet(100, 100, .25 * Math.PI, .125 * Math.PI, 3, Color.RED, model));
Compare this with the file Skeet.java
,
particularly the instance variables and constructor,
to understand all that this means.
The intent of this program is to allow the user to add new Skeets to the system as the simulation runs. Pressing the button Add Skeet should launch a dialog box asking the user for all the specification of the Skeet and add that Skeet to the grid. However, when you press the button, nothing happens. That's for you to write.
("Dialog box" is not meant to be a technical term; it just means a smallish window on the screen temporarily to get input from the user.)
AddListener
classThe Add Skeet button has an action listener attached to it.
However, the actionPerformed()
method is
empty.
Your main task is to fill in this method, which
will prompt the user with a dialog box.
As you add things to the window in your program, I recommend that you periodically compile and run the program to see how the window looks so far and make some adjustments. Make the window look reasonably nice; play with the dimensions and the layout until it looks right.
Here are things that ought to be in the dialog box:
JColorChooser
.
This displays a color palette which the user
may click on to select a color.
Then the program can read that color from
the JColorChooser
object.
Go to the Java API to find out more about this
class.
Mainly you will want to use the no-parameter constructor
and the method getColor()
.
The actionPerformed()
method should also
make the dialog box appear and it should attach
another action listener to the Ok button, but
that action listener will be the subject of the next
part.
Write another class, also implementing ActionListener
which will react to the Ok button being pressed.
Specifically, the actionPerformed()
method
should
setVisible(false)
.
First, you should think about what pieces of information
this action listener needs to know (hint: a lot).
These will become instance variables
and parameters to the constructor.
Then writing the actionPerformed()
method will be easier.
Please email the files you wrote or modified
to Sarah.Cobb@my.wheaton.edu
.