Homework #4: Simple Swing event handling

For this homework, you will demonstrate your mastery of mouse, keyboard, and window events by creating the skeleton of a 2-dimensional "Lunar Lander" game. The application will draw a simple spaceship, animate its thrusters in response to keyboard events, and move it around the window in response to mouse clicks. (We aren't going to simulate the physics yet.)

The requirements for the application are as follows:


Hint 1: Give a lot of thought to how you store and draw the 'shapes' of the rocket and the flames. You will be drawing lines, rather than Shape2Ds; ragged 2-dimensional arrays are worth considering. If you find yourself drawing the spaceship with a bunch of statements like

g.drawLine(x+ 7.0,y+ 3.0,x+21.0,y-17.0);
g.drawLine(x+21.0,y-17.0,x+25.0,y-19.0);
g.drawLine(x+25.0,y-19.0,x+10.0,y-12.0);
...

you are definitely doing something wrong.

Hint 2: This is a course in object-oriented programming. Your solution should involve more classes than just a subclass of JFrame. A couple of questions to ask yourself are, "How hard would it be to change the design of my spaceship?" and "How hard would it be to display multiple spaceships in the same window?"

Hint 3: You should be able to implement the entire application in fewer than 300 lines of code. Some of them might even be amazingly similar to the code from homework #3!

Hint 4: The javax.swing.Timer class described in Chapter 6 could be useful in animating the fuel consumption display.

Hint 5: If you decide to tackle the extra-credit part of the assignment, you will probably be interested in the java.awt.geom.AffineTransform class.


Due date: The homework is due at the beginning of class on 7 October.

Turn in: (1) A listing of the source file(s) for the application. (2) A screen dump of the application, with at least one rocket motor firing if possible. (Color output is not necessary.)

E-mail: A Zip or tar archive file containing all of your source files (including package.html) to grading@bozoid.com. The subject of the e-mail should be "EventTest04", and the archive name should be hw04.zip or hw04.tar.gz. In addition to your source files, the archive file should include a text file named readme.txt containing your name and any other identifying information you think is appropriate. Do not include .class files, javadoc files, or screen dumps in the e-mail.

Note: Staple all the pages of your submission together, and make sure you put your name on your homework!