I’ve been reading through a book by David Brackeen recently, this is a modified version of his Animation class that I edited. Feel free to improve further ^^
You would still have to type out that monster Graphics2D.drawImage(5 000 000 arguments); statement when drawing the image though, but it removes a lot of the strip animation code from your rendering loop.
To draw the image you would do something like this
protected Animation mainChar = new Animation("/images/spritesheet.png", 50, 100, 1, 3, 100, 0, 2);
g2d.drawImage(mainChar.getImage(), mCharPosX, mCharPosY,
mCharPosX+mainChar.getFrameWidth(),
mCharPosY+mainChar.getFrameHeight(),
mainChar.getFrameX(),
mainChar.getFrameY(),
mainChar.getFrameX()+mainChar.getFrameWidth(),
mainChar.getFrameY()+mainChar.getFrameHeight(),
null);
/**
* @param String stripname - the name of the strip image
* @param Integer framewidth - the width of an individual frame in pixels
* @param Integer frameheight - the height of an individual frame in pixels
* @param Integer rows - the number of rows the strip has
* @param Integer columns - the number of columns the strip has
* @param Long frametime - how long each frame will be shown
* @param Integer startpos - This is which frame in the strip image the animation will start from
* @param Integer endpos - You guessed it! The frame on the strip image the animation will end at and loop back to the startpos
*
* @version 0.005
*
*Date created: 2011-06-13
*
*Date updated: 2011-06-14
*/
public class Animation {
private int frameWidth, frameHeight;
private int rows, cols;
private int totFrames;
private int currFrameIndex;
protected int frameX, frameY;
private long animTime;
private long totalDuration;
private long frameTime;
private int startPos;
private int endPos;
protected boolean moving;
private int updateCount;
protected Image stripImage;
//Creates a new empty Animation
public Animation(String stripname, int framewidth, int frameheight, int row, int col, long frametime, int startpos, int endpos){
frameWidth = framewidth;
frameHeight = frameheight;
rows = row;
cols = col;
totFrames = cols*rows;
totalDuration = 0;
frameTime = frametime;
stripImage = loadImage(stripname);
startPos = startpos;
endPos = endpos;
start();
}
private Image loadImage(String fileName){
return new ImageIcon(getClass().getResource(fileName)).getImage();
}
/**
* Starts the animation over from the beginning
*/
public synchronized void start(){
animTime = 0;
currFrameIndex = startPos;
}
/**
* Updates this animation's current image(frame), if necessary
*/
public synchronized void update(long elapsedTime){
updateCount++;
if(totFrames > 1){
animTime += elapsedTime;
totalDuration += frameTime;
if(updateCount >= frameTime/20){
if(currFrameIndex >= endPos+1){
currFrameIndex = startPos;
}
frameX = (currFrameIndex % cols) * frameWidth;
frameY = (currFrameIndex / cols) * frameHeight;
if(moving){
currFrameIndex++;
}
updateCount = 0;
}
}
}
//Gets the image lol
public synchronized int getIndex(){
if(totFrames == 0){
return 0;
}else{
return currFrameIndex;
}
}
public Image getImage(){
return stripImage;
}
public void setMoving(boolean b){ moving = b; }
public synchronized int getFrameX(){ return frameX; }
public synchronized int getFrameY(){ return frameY; }
public synchronized int getFrameWidth(){ return frameWidth; }
public synchronized int getFrameHeight(){return frameHeight; }
}
Hope this will help some of you ^^
Edit: Whoops, forgot to update the params