CS 202 - Computer Science II - Spring 2008
Lab 9 - Model/View/Controller
Loyola College >
Department of Computer Science >
Dr. James Glenn >
CS 202 >
Labs >
Lab 9
Due
Wednesday, April 9th at 11:59pm.
Labs submitted one day late will be assessed a
20% penalty. Labs will not be accepted more than one day late.
Objectives
- to manage communication between model, view, and controller
- to handle mouse events
Reading
Wu, Chapter 14
Introduction
The evil Seeborgs have overrun most of planet Javalon. Only the
capital city of Oopsonia remains under Javalonian control. Forward Base
Omega is the last of the city's defenses, but its control system was
damaged in the last Seeborg attack. Dr. Richard G. James, head of
Javalonian computer systems, was almost finished reimplementing the
control system but went out for a donut break and was never seen again.
Presumably he was captured by the Seeborgs. Fortunately he left a prototype
and instructions for completing the control system.
You are Javalon's last hope!
Assignment
Complete the code in the Spaceship and OmegaWindow classes.
Spaceship needs paint reimplemented to draw the
Seeborg spaceships correctly (the code that is in paint now
simply draws a colored rectangle). Your new version of paint
should use the x, y, width, and height
fields of the Spaceship object to determine the coordinates to
use to draw the ship.
Note that those fields are doubles and so must be cast
to ints to be passed to the Graphics methods.
OmegaWindow needs to be updated to include classes that implement
listeners for mouse movement (to aim the cannon) and mouse clicks
(to fire the cannon). Instances of those listener classes must be registered
on the view object. The event handlers should invoke the
aimCannon and fireCannon methods in the model.
There is also code that needs to be added to the
actionPerformed method in the TimerListener inner class
to update the view by getting the values for score, time left, and energy
from the model, displaying those values in the appropriate components, and
then redrawing the cannon and spaceships.
Files
The Java archive contains
Advice
- First, add code to the actionPerformed method in
the TimerListener inner class that updates the view.
The main part of the view (the view object) can be updated by
invoking its repaint method. The other parts
(scoreDisplay, timeDisplay, and energyDisplay)
can be updated by invoking the accessors in the model (the model
object) to get the score, time, and energy, and then invoking the
components' setText or setBackground
methods (the color used for the energy display is (255, x, x) where x
is 0 if the cannon is fully charged and 255 if it is completely discharged).
To test your code at this stage, run the still unfinished OmegaWindow.
You will not be
able to aim or fire, but you will be able to see crudely drawn
ships flying across the screen.
- Next, rewrite paint in Spaceship. The
version of the applet shown above uses one fillOval and one
fillArc to draw the Seeborg ships. Running the game now
should display nicely drawn ships.
- Once the Seeborg ships are drawn to your liking,
create an inner class in OmegaWindow
(you can call it FireListener)
and in the constructor, create an instance of that class and register it
to listen for events from the main game view.
FireListener will have to define five methods (see the appropriate
page in the Java API documentation to see which five); four should be
empty and the other should simply update the model by invoking its
fireCannon method. If you test the game at this point, you should
be able to fire the cannon (but not aim it).
- Finally, create another inner class
(AimListener), and add code to the constructor that creates an
instance of that class and registers it to receive mouse movement events
from view (again, check the Java API documentation to see which
methods this inner class must define, and which once receives the events
we're interested in). The event handler method should simply pass to
the model's aimCannon method the x- and y-coordinates
to which the mouse has moved. You can get those coordinates using
the appropriate methods of the event object. At this point you should
be able to aim and fire the cannon.
Exercises
The code that updates the score, time, and energy displays is currently
in the OmegaWindow class in one of the listeners. The listeners,
however, are the controller part of the Model/View/Controller architecture.
Updating the displays should be the responsibility of the view. Explain
how the code can be refactored to fix this design flaw. What other
violations of the model/view/controller architecture do you see? (Hint:
consider the Spaceship class, which is part of the model.)
Submissions
Submit the source code (.java file) for your completed OmegaWindow and Spaceship classes.