Lab14: More GUI

Objective

Setup

First, enter the cs190m directory:

$ cd cs190m

Then, download lab14.zip into the cs190m directory. Run the following commands:

$ unzip lab14.zip
$ cd lab14

The Simulator GUI

There's only one new class today, which is the Lab14 class. This class implements the GUI (note that it extends JFrame) as well as the main method. The two methods of interest are paint and loop, as well as a new variable of type Image. Much of the original main() method have been moved into the loop() method.

Loop

The contents of the loop method are similar to the main method in lab13. However, this time, instead of printing messages to the screen, you should use the createImage method and assign it to the image variable mentioned above. Then you should draw a visualization of the current state onto the image by retrieving a Graphics object from the image and drawing on it. Some helpful Graphics object methods are setColor (to distinguish between males and females) and fillOval. It is up to you specifically wish to represent the bodies.

Paint

The paint method is overriding a parent method. It should simply call drawImage on the Graphics object provided in the parameter and pass in the image instance variable, assuming the image is non-null. Don't worry about the last argument to drawImage; it should simply be null.

Testing Your Program

Simply running the Lab14 class will start a simulation. An example image of what your program might look like is given below:

Turning In Your Lab

To turn in your lab:

$ cd ~/cs190m/lab14
$ turnin -v -c cs190m -p lab14 *.java

Getting Help

Many of you will probably run across problems while programming at some point during a lab. If that's the case, here are the resources you should use, in order:

  1. Your textbook
  2. Lecture notes
  3. Java API documentation
  4. Your lab TA