The purpose of this project is to get you warmed up on programming in Java, and to start you thinking about program structures that make use of polymorphism and inheritance.
Your program will start with a class named Animation.java
, which
will have the main
method that launches the program. You'll
run your program via a command line like this:
java Animation sprites.txt 500 300
where sprites.txt
contains a textual description of the
various objects (sprites)
that will be moving around in your animation, and 500 and 300 are examples
of the animation window width and height, in pixels.
When you run this program, some sort of animation will take place. Spaceships in space, butterflies in a meadow, cars in a city, zombies in the park, or whatever strikes your fancy. The keys to this program will be the use of Java and adherence to the structure described in the next section, so the particulars of your animation don't matter a great deal.
I have provided a simple bouncing ball example in Animation.java, AnimationPanel.java, and Ball.java. I have also created a short sample FileScanner.java showing how to read a text file one line at a time and parse each line for structured information. Feel free to borrow any of this code for your program.
Your program should model itself after the Animation/AnimationPanel/Ball
program. Here's what I recommend. (If you have a better architecture, go for
it, but I do want to see a top-level Sprite
class plus at
least two subclasses of Sprite
.)
First, please read the comments in Animation.java, AnimationPanel.java, and Ball.java to familiarize yourself with the strategy I have used in this program.
Modify AnimationPanel.java
to store an ArrayList
of Sprite
objects instead of a single
Ball
object. This modification should include a method with
signature "public void addSprite(Sprite sprite)
" to enable you to add
Sprite
s one at a time to the AnimationPanel
object.
To begin the replacement of the Ball
class, create a
Sprite
class like this (see
Sprite.java):
Modify the main
method in
Animation.java to look something
like this:
Sprite
subclass and call the AnimationPanel
's add
method to add it to the panel.
// Begin animation
do forever
sleep for some time
call the AnimationPanel
's step
method
Sprite
,
representing the various kinds of objects you want to include in
your animation. These classes will contain overrides of the
methods in Sprite
to arrange for the drawing and stepping.The sprite file will consist of lines of text, one per sprite, of this format:
For example:
would, presumably, cause the instantiation of an object of type Goat
with initial position (200, 100) and initial velocity (10, 0) and initial
color purple.
You are free to add features to your particular sprite types, making corresponding additions to the "other info" section of the sprite description line in the sprites file.