You are going to do the first part of a space invaders game. You will create a bunch of spaceships that move back and forth across the screen.
The purpose of this lab is to help you to understand exceptions and objects.
You will write a program that draws at least 4 spaceships on the screen and moves them back and forth across the window.
Your program should have:
The program should have the same functionality whether the spaceships are drawn as GImage's or something else (GOval, GPolygon, etc.).
You will need to use the ACM library, so you may want review the tutorial on using the ACM library.
Start by creating a GImage that reads from a JPG file. Once you are able to read the image (and catch the exception (see Further tips for more info) if the file doesn't exist) you can move on to the next phase.
Once you have the spaceships created, move them across the screen with a simulation loop.
When you have this working, reverse direction when you get to the edge of the page.
Again, take a look at the examples.
We require that you use a separate .java file for your Spaceship class. However, you will notice that if it is in a separate file, using the add() method won't magically work like it did before. That is because add() is a method of the GraphicsProgram class. In examples like Car, the wheel class can simply call “add(circle)” because the “wheel” class is declared inside of the “car” class, which extends “GraphicsProgram”. So the wheel automatically has access to the methods of whatever GraphicsProgram that created it.
To keep classes in separate files, do what is done in the Balls example. Notice that the final parameter of the “Ball” class constructor is a “BouncingBall” object (which extends GraphicsProgram). This allows the Ball class to invoke the “add” method of the indicated GraphicsProgram without being in the same file.
Another option (and some of you may discovered this while doing the previous graphics lab) is to make your Spaceship class extend GCompound. You can look up information about GCompounds on the ACM Graphics Page. The basic idea is that you can add GOvals and GImages to your Spaceship class, and then you can add Spaceships directly to your panel in your run method.
When you have your program working, you will need to show it to a TA. The TA will evaluate your code based on the following criteria:
In order to receive points for part 1.4, you must define an object for your spaceship and call a method on that object to make it move.
5 points extra credit: Throw exceptions when you hit the edge and have your simulation loop catch the exception and tell the object to reverse direction.
Extra Credit: Pass off early (1 point per day (excluding Sunday) up to 3 points)
When a new GImage object is created with an invalid file name, it doesn't throw a “FileNotFoundException”. It actually throws a “acm.util.ErrorException”. That is the one you will need to catch.
For ideas on how to draw some different shapes and colors, see ACM library tips