Q:

How do I make an animation run just once in java?

So I am using the framework from a book on how to program games in java and it has an animation class like so:
package com.vincent.framework.animation;

import java.awt.Graphics;

public class Animation {

private Frame[] frames;
private double[] frameEndTimes;
private int currentFrameIndex = 0;

private double totalDuration;
private double currentTime;

public Animation(Frame... frames) {
    this.frames = frames;
    frameEndTimes = new double[frames.length];

    for (int i = 0; i < frames.length; i++) {
        Frame f = frames[i];
        totalDuration += f.getDuration();
        frameEndTimes[i] = totalDuration;
    }
}

public synchronized void update(float increment) {
    currentTime += increment;

    if (currentTime > totalDuration) {
        wrapAnimation();
    }

    while (currentTime > frameEndTimes[currentFrameIndex]) {
        currentFrameIndex++;
    }

}

private synchronized void wrapAnimation() {
    currentFrameIndex = 0;
    currentTime %= totalDuration;
}

public synchronized void render(Graphics g, int x, int y) {
    g.drawImage(frames[currentFrameIndex].getImage(), x, y, null);
}

public synchronized void render(Graphics g, int x, int y, int width, int height) {
    g.drawImage(frames[currentFrameIndex].getImage(), x, y, width, height, null);
}

}

That is fine if you want the animation to repeat over and over, but how can I make an animation stop when it gets to the last frame? I have tried having an if statement to check if it has reached the last stage, but it would skip frames for some reason. Thanks for any help!

A:

Getting rid of the line currentFrameIndex = 0 in the method wrapAnimation() ended up working. It turns out the problem was in another class where update() was being called. The animation timer was starting too early in some cases causing it to skip frames.

