/******************************************************************************* ** ** ** @author: Thanius ** ** @version: 1.0.0 ** ** ** ** @info: Animation Handler -> put Images into an Animation/Sequence ** ** ** *******************************************************************************/ package bomberman; import java.awt.Image; import java.util.ArrayList; public class Animation { private final ArrayList frames; private int currentFrame; private long animTime, totalDuration; public Animation() { frames = new ArrayList<>(); totalDuration = 0; synchronized(this) { animTime = 0; currentFrame = 0; } } /******************************************************************************* ** ** ** M A I N F U N C T I O N S F O R A N I M A T I O N S ** ** ** *******************************************************************************/ @SuppressWarnings("unchecked") public synchronized void AddFrame(Image image, long duration) { totalDuration += duration; frames.add(new AnimFrame(image, totalDuration)); } public synchronized void Update(long elapsedTime) { if(frames.size() > 1) { animTime += elapsedTime; if(animTime >= totalDuration) { animTime %= totalDuration; currentFrame = 0; } while(animTime > GetFrame(currentFrame).endTime) { currentFrame++; } } } public synchronized Image GetImage() { if(frames.isEmpty()) return null; else return GetFrame(currentFrame).image; } private AnimFrame GetFrame(int i) { return frames.get(i); } /******************************************************************************* ** ** ** S U B - C L A S S T O P E R F O R M A N I M A T I O N ** ** ** *******************************************************************************/ public class AnimFrame { Image image; long endTime; public AnimFrame(Image image, long endTime) { this.image = image; this.endTime = endTime; } } }