Here is a Sub-standard example of Project #1



Class Relationships


Here is the interface to my AnimatedSprite class


class AnimatedSprite extends Raster {
    int x, y;           // current position of sprite on playfield
    int width, height;  // size of a sprite's frame
    int frames;         // frames in sprite
    short track[][];    // animation tracks
    short length[];     // length of each track
    short curAnim;      // track number of current animation
    short curState;     // frame of current track
    short nextAnim;     // next track

    public AnimatedSprite(Image images, int frames);                    // 9 lines
    public void addState(int t, int frame, int ticks, int dx, int dy);  // 17 lines
    public void Draw(Raster bgnd);                                      // 16 lines
    public void nextState();                                            // 5 lines
}

Here is the interface to my PlayField class


class PlayField extends Raster {
    Raster background;          // background image
    AnimatedSprite sprite[];    // list of sprites on this playfield

    public PlayField(Image bgnd, int numSprites);                       // 5 lines
    public void addSprite(int i, AnimatedSprite s);                     // 1 line
    public void Draw( );                                                // 4 lines
}
Here is the entire source to the Applet shown above:

import java.applet.*;
import java.awt.*;
import java.util.*;
import AnimatedSprite;
import PlayField;

public class Rastest extends Applet {
    PlayField playfield;
    AnimatedSprite sprite;
    private boolean changed;
    private int cycle;
    Image output;
    int calls;

    public void init()
    {
        playfield = new PlayField(getImage(getDocumentBase(), "clouds.jpg"), 1);
        String tokens = getParameter("sprite");
        StringTokenizer parse = new StringTokenizer(tokens, ", ");
        String filename = parse.nextToken();
        showStatus("Sprite: " + filename);
        try {
            int spriteFrames = Integer.parseInt(parse.nextToken());
            sprite = new AnimatedSprite(getImage(getDocumentBase(), filename), spriteFrames);
            while (parse.hasMoreTokens()) {
                int frame = Integer.parseInt(parse.nextToken());
                int dx = Integer.parseInt(parse.nextToken());
                int dy = Integer.parseInt(parse.nextToken());
                sprite.addState(0,frame,1,dx,dy);
            }
        } catch (NumberFormatException e) {
        }
        sprite.x = 200;
        sprite.y = 80;
        playfield.addSprite(0, sprite);
        cycle = 0;
        changed = true;
        calls = 0;
    }

    public void paint(Graphics g)
    {
        if (changed) {
            playfield.Draw();
            output = playfield.toImage(this);
            sprite.nextState();
            cycle += 1;
            if (cycle >= 12) {
                cycle = 0;
                changed = false;
            }
        }
        calls++;
        showStatus("calls = "+calls);
        g.drawImage(output, 0, 0, this);
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public boolean mouseDown(Event e, int x, int y)
    {
        changed = !changed;
        sprite.x = x - 40;
        sprite.y = y - 60;
        repaint();
        return true;
    }
}

This page last updated: Thursday, September 26, 1996