Chris Pollett >
Old Classes >
CS151

   ( Print View )

Your Grade: Course Info: Homework Assignments: Practice Exams:
                            












HW #4 Page

HW#4 --- last modified January 16 2019 00:35:13..

A solution set is posted here.

Due date:  Oct. 31 start of class.
=========  Besides sending me the .jar file below
           turn in on paper a UML diagram of your project
           in which you point out where you have used the 
           Strategy, Iterator, and Factory patterns.

Files to be submitted: cs151sec3hw4file1.jar (this should contain
======================                        all your .java files
                                              and you should be able 
                                              to compile everything by
                                              typing:
                                              javac hw4.java
                                              This file might also
                                              contain any .gif files you
                                              use.
                                              )

Purpose:  To gain experience with the Strategy, Iterator and Factory
========  patterns as well as with the Java Collection's framework.

Specification:
==============
For this homework you will create an application that visually animates
in a JFrame monsters and victims that live on a 20 x 20 grid. The frame 
consists of the following components: (1) a subclass of JPanel on 
which the monsters and victims live and move once a second 
(called WorldPanel). (2) a JPanel with two buttons on it: 
(a) a JButton labelled ``Add Monster'' and (b) a JButton labelled 
``Add Victim''.  When either JButton is clicked all animation is paused.  
A sequence of two JOptionPane's which ask and get integers for the 
following information appear: 

(1) Start tile X position?
(2) Start tile Y position?

A Victim or a Monster is then to the WorldPanel using the WorldPanel method
addCreature(Creature c) and animation resumes. 

A factory should be used to create Creature's. Creature's are stored in the
WorldPanel in a LinkedList object. Creature is an abstract class which is
extended by two classes: Victim and Monster. It contains the two int's
used to store the answers to the above questions. It also has a
javax.swing.ImageIcon object to store the image of what a Victim or a Monster
looks like. One of the constructors for ImageIcon takes a String 
filename and can be used to read in the image from a .gif file you create.
The Creature class should have set and get methods for each of its two int's.
It has a method drawCreature(Graphics g, int x, int y) to draw the Creature
at location x and y (in pixels) in the panel and an abstract method animate(). 
drawCreature uses ImageIcon's paintIcon method (first value null) to draw 
the Creature. animate() is implemented by Victim and Monster.

Animation is controlled by an animation thread just as in the AnimationApplet 
idiom discussed in class. (If you want a more elegant way check out the 
class Timer.) After sleeping one second this thread wakes up and calls 
repaint(). This eventually causes the WorldPanel's paintComponent(Graphics g) 
method to be called. You will write paintComponent so that it first 
draws a grid of 20 x 20 blue tiles on a black background. The size of these 
tiles should be at least 16 pixels x 16 pixels. It then checks if any 
Victim's in the LinkedList are on tiles also occupied by Monster's and if so
deletes them from the LinkedList. It then uses an Iterator over 
the LinkedList of Creature's stored in the WorldPanel to draw
each Creature into its appropriate tile. If more than one Creature occupies 
a tile then the Creature later in the list is drawn over the one earlier. 
After repaint() completes the animation thread calls WorldPanel's 
animate() method. This again uses an Iterator and iterates through the
LinkedList of Creature's calling each of their animate() methods.

The animate() method for a Victim randomly picks a value from -1, 0, 1
and changes its Y tile coordinate by this value. If this gives a number 
less than 0 than the Y tile value becomes 20 and if this gives a value
greater than 20 the Y value becomes 0. The animate() method then randomly 
picks a value from -1, 0, 1 and modifies its X tile coordinate similarly.

The animate() method for a Monster is similar to that of a Victim
except that it pick random values from the set -3, -2, -1, 0, 1, 2, 3. 

Point Breakdown
===============
Departmental coding guidelines followed..1pt
UML diagram of your project.
 which points out where Stategy, Iterator
   and Factory patterns used.............1pt
WorldPanel's paintComponent works as
  describes..............................1pt
WorldPanel's, Victim's, and
 Monster's animate works as described....1pt
Animation thread works as described......1pt
Add Victim and Monster button works......1pt
Creature abstract class as described.....1pt
Whole program works......................1pt

Total....................................8pts                          

A solution set is posted here.