CIT 591 Third Java Assignment: Moving Pictures
CIT 591, David Matuszek, Fall 2002

Purpose of this assignment:

Idea of program:

In this assignment you will write an application (not an applet) that opens a window, draws several figures in the window, and moves them around.

The essential concepts you need to do this program are how to create and use classes and objects, and how to access data in the same object and other objects. I chose this assignment because almost every object in this program will have a visible representation on the screen, and I think this will help you understand their relationships.

Overview:

Your program will consist of five or more classes, and objects derived from those classes. The classes are:

class MotionPicture extends Frame

This class will open a window, create some Figures, and tell those Figures to draw themselves in the window and to move around in the window. Its main data are information about the window and an array of Figures.

class Figure

A Figure has, as part of its state,

A Figure can do the following:

Three or more classes (of your own devising) that extend Figure

These figures inherit the data and methods of Figure, so you don't have to define that data again, or the move method. However, you will define a draw method in each class that overrides the inherited version.

Details:

I'm going to give you very specific instructions on how to write the program. Your job is to follow the instructions and make the program work. Parts of the code are just given to you, because we haven't yet covered in class how to do those things.

MotionPicture

Write a class MotionPicture that extends Frame (Frame is defined in java.awt.*, and essentially means "window"). In this class define the following method:

public static void main(String args[]) {
    MotionPicture p = new MotionPicture();
    p.doTheWork();
}
This main method, inside class MotionPicture, creates an object of type MotionPicture, and tells it to do some work. The visible representation of this object will be a "frame" around a window on the screen, with the usual title, with the usual iconify, expand, and close boxes.

Next you will need to write the following method, and "fill in the blanks" to complete it. The code near the beginning of the method (given to you) creates a Panel object and puts it in the empty area surrounded by the Frame; this Panel will not look any different from the empty area until we color it or draw things in it.

    void doTheWork() {
        
        // Declare any variables you need here
        
        // Create the window and get it ready to draw on
        Panel panel = new Panel();  // Creates a Panel to hold our drawing
        add(panel);                      // Puts the Panel in the window
        setSize(500, 400);            // Sets the size of the window (OK to change)
        setVisible(true);                // Makes the window visible on the screen
        Graphics g = panel.getGraphics();    // Gets the Graphics context
        
        // Put your code here to create some Figures and put
        // them into an array--these will not be instances of class
        // Figure, but will be instances of the subclasses of Figure
        
        // Set up a loop that runs through the following code
        // a few hundred times {
        
            // Tell each figure to move a small amount
        
            // Tell each figure to draw itself
        
            // This code will cause Java to pause for 1/20 second
           try { Thread.sleep(50); } catch (InterruptedException e) {}
            
           // Tell each figure to erase itself
       
        } // End of loop
        
        dispose(); // Get rid of the window
    }

Figure

Next, write a class Figure that describes the general characteristics of every figure you will put on the screen. It must define at least the following instance variables (you can have more if you like):

All the figures will move in the same way, so you should put a move method in the Figure class that can be inherited by each Figure. It should be declared as public void move() , and here's what it should do:

All figures will be erased the same way (by drawing a filled white rectangle over the figure), so put a public void erase(Graphics g) method in the Figure class.

All figures will not be drawn in the same way, but you need to put a "placeholder" method public void draw(Graphics g) in the Figure class. The purpose of this is to assure Java that every figure has such a method. The method should simply draw an unfilled black rectangle of the height and width of the figure.

Subclasses of Figure

Finally, create three or more subclasses of Figure, giving them meaningful names. Each subclass should assign values to its (inherited) width and height, its initial position (x = 0, y = 0 is OK), and its deltaX and deltaY. It should have a public void draw(Graphics g) method that draws an interesting figure (not too simple, but not too complex, either--somewhere between three and ten drawing commands is probably about right). Figure out the width and height of this drawing, and use those values for the width and height of this subclass (not all subclasses should have the same width and height).

Each instance of a figure that you create should have an initial deltaX and deltaY. It works out well to make these randomly chosen small numbers (say, between 1 and 5).

Bits and Pieces:

When you move a Figure, you should keep it within the Panel. This involves some arithmetic, and it also involves knowing the size of the Panel. You can find the size of the Frame or the size of the Panel by sending either the messages getWidth() and getHeight(); these methods return ints. You should check the Panel size each time you start to draw, because the user is able to resize the window. (What happens to a figure if the user suddenly makes the window so small that the figure is entirely outside it? Be sure to do something sensible!)


As I've defined the program, the Graphics object for the Panel is used as a parameter to the draw and erase methods. Another way to do this is to store the Graphics object in a class variable of the Figure class. That way you don't have to keep passing it as a parameter.


Notice that the Panel is slightly smaller than the Frame you put it in.


To choose a random number, import java.util.* (or just java.util.Random), create the object

static Random random = new Random();

and send it the message nextInt(n) to get a random integer between 0 and n - 1. Be sure not to create more than one Random object (that's why I suggest making it static).


Don't worry about objects "bumping" into one another. The right thing happens automatically--if you erase the figures when I said to erase them.

Due Date:

Wednesday, October 9, by midnight. Zip together all files created by BlueJ, including not only .class and .java files but also the README.TXT, .pkg, .pkh, and .ctxt files, and submit them via Blackboard. These are requirements, not suggestions.