Before starting on today's lab, finish up Lab 13 from last Friday.
The goal of this lab is to design windows requesting
input from the user.
You can unpack the files you'll be working with by cloning the
repository for lab14
:
hg clone /cslab/class/csci235/labs/lab14 cd lab14
In the last lab, your work was mostly with the class
JButton
and interface ActionListener
, with a
little bit of work to create a frame and lay out the buttons in it.
This lab expands the set of components in your toolbox. We will talk
about the additional components in class, but you can try them out
now.
Change (cd
) into the samples
directory
within the lab. This has an example program from class that uses several
instances of JTextField
, which displays as a box in which
the user can type. As with JLabel
, you can call its
methods getText()
and setText()
.
You can
also use addActionListener()
: unless you do something
fancy, the listener's actionPerformed()
will be called
when the user hits the ENTER key while typing in the box. If you
uncomment the line for field.addActionListener
, it will
install the same listener that is also listening for a click on the button.
Note that the value in JTextField
must be a string.
To convert an input to a numeric value, you will need to use the
parsing method from class Integer
or
Double
. Those methods throw a
NumberFormatException
if the string is not parsable.
Once you are comfortable with this example, move on to the real
work, which is in the sprites
subdirectory.
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 its own way.
The grid we'll be working with today will be inhabited by Sprites. A Sprite is an object that moves about the grid in a way determined by the following parameters:
Then compile and run the program Sprites
.
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:. Sprites.java java -cp simulation.jar:. Sprites
By default there is a single Sprite.
If you inspect the code in Sprites.java
,
you'll see that the Sprite is added to the grid
in the line
model.setAgentAt(100, 100, new Sprite(100, 100, .25 * Math.PI, .125 * Math.PI, 3, Color.RED, model));
Compare this with the file Sprite.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 Sprites to the system as the simulation runs. Pressing the button Add Sprite should launch a dialog window asking the user for all the specification of the Sprite and add that Sprite to the grid. However, when you press the button, nothing happens. That's for you to write.
("Dialog" is not meant to be a Java-specific technical term; it just means a smallish window on the screen temporarily to get input from the user.)
Read the entire lab description before you start writing any code. It will help you to see the program as a whole, and there are also some hints lurking toward the end.
AddListener
classThe Add Sprite button has an action listener attached to it.
However, the actionPerformed()
method is
just a skeleton.
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,
It is a good idea to periodically compile and
run this class to see how the window looks so far,
and then make some adjustments.
Make the window look reasonably nice; play with
the dimensions and the layout until it looks right.
To make just looking at window easier, the class
AddListener
contains a simple main()
method
that simply shows the window.
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.
See the Java API documentation to find out more about this
class.
Mainly you will want to use the no-parameter constructor
and the method getColor()
.
Since it will be hard to indicate a value including π when entering a direction or rotational velocity, you should interpret the user's input in these fields as a coefficient of π. In other words, if the user enters ".5" into the direction field, you should interpret that to mean ".5π". Also, all these text fields should have an accompanying appropriate label.
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.
The provided code suggests that you have the constructor for
AddListener
do the work of setting up an invisible
window, with the associated action listener for its Ok button.
With that arrangement, this first actionPerformed()
method only needs to make that window visible.
Write another class, also implementing ActionListener
,
that 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.
The JColorChooser
is bigger than you might guess; it
is something like 420x220 pixels. You
probably want to your other components into a single
panel with its own layout, and put that and the color chooser into
your window with a flow layout.
An awkward thing about building a dialog this way is that it is
hard to know in advance how big the components in your window should
be. But if you choose your layout classes appropriately, you can do a
first version without worrying about getting the window size exactly
right. You can run that program using the main method in
AddListener
, then resize the window to make it look
good. Once you have that, you can use the command
xwininfo
to find out the size. Run the command in a
terminal window, then click on the window you want to find out about.
From the sprites
subdirectory, turn in the source files you created or modified with a command such as
/cslab/class/csci235/bin/handin lab14 *.java
Be sure to include all of your files on a single command.