animation for sprites

Hi,
I’m a new member in the java game’s world and my intent is to write my first game: a “shooting game”.
I wanna give an animation to the enemy sprites, I’ve seen the “Animation.java” class in the “New Riders Developing Games In Java” document and I would like to know if is good or if there are best ways to build animation.

This is the code:


import java.awt.Image;
import java.util.ArrayList;

/**
    The Animation class manages a series of images (frames) and
    the amount of time to display each frame.
*/
public class Animation {

    private ArrayList frames;
    private int currFrameIndex;
    private long animTime;
    private long totalDuration;


    /**
        Creates a new, empty Animation.
    */
    public Animation() {
        frames = new ArrayList();
        totalDuration = 0;
        start();
    }


    /**
        Adds an image to the animation with the specified
        duration (time to display the image).
    */
    public synchronized void addFrame(Image image,
        long duration)
    {
        totalDuration += duration;
        frames.add(new AnimFrame(image, totalDuration));
    }


    /**
        Starts this animation over from the beginning.
    */
    public synchronized void start() {
        animTime = 0;
        currFrameIndex = 0;
    }


    /**
        Updates this animation's current image (frame), if
        necessary.
    */
    public synchronized void update(long elapsedTime) {
        if (frames.size() > 1) {
            animTime += elapsedTime;

            if (animTime >= totalDuration) {
                animTime = animTime % totalDuration;
                currFrameIndex = 0;
            }

            while (animTime > getFrame(currFrameIndex).endTime) {
                currFrameIndex++;
            }
        }
    }

    /**
        Gets this Animation's current image. Returns null if this
        animation has no images.
    */
    public synchronized Image getImage() {
        if (frames.size() == 0) {
            return null;
        }
        else {
            return getFrame(currFrameIndex).image;
        }
    }


    private AnimFrame getFrame(int i) {
        return (AnimFrame)frames.get(i);
    }


    private class AnimFrame {

        Image image;
        long endTime;

        public AnimFrame(Image image, long endTime) {
            this.image = image;
            this.endTime = endTime;
        }
    }
}

its a rather common way and good to use. but for my taste theres no real reason to create a class
(AnimFrame) for only holding two attributes.
but i would keep a two class system if you plan to realize some advanced features:
i usually need several different ways of doing an animation, e.g. backwards, looped-ones or
whatever. so i have an AnimationStrip class holding the images and an Animator class doing the
counting stuff for advancing the frames

Im not really sure what this class is for. All it seems to do is return an image based on a time,
it does nothing to actually draw the image that I can see.

A class per image seems a waste to me, too. All you need is an array of images and an array of update times in your sprite class. But I dont see much harm in it either if it makes it easier for you to personally organize.

shrug

It doesnt seem to address the more interesting and bigger issues-- which is how you are organizing frame rendering. Are you doing entire frame or dirty rectangle? Are you using active or passive rendering? Are you page flipping? Are you using VolatileImage for your sprite data and if so what are you doing abotu loss of the volatile buffer?

hmmm, i really think such a container for an animation sequence is a good idea. although there arent much ideas in mesina’s code implemented yet,
you could imagine lots of it. because he mentioned to be new to java gaming i assume he will use perhpas double buffering (which hasnt to be part of
this class) and only adds single frames to the arraylist. so the class takes control over the indexing stuff, perhaps even with my idea of several indexing
strategies. this can really be a pain to implement in the sprite class itself. moreover its a good place to watch, when the animation really ended, dependend on
which animation strategy you chose and to notify the owner.
e.g. a glowing star is a set of few frames which show a point getting brighter. the strategy could be to animate it once forward and then once backward,
after that something happens (stopping, removing, changing the animation, whatever).