4 frame animation using a utility timer

This here is an example of how to animate 4 seperate BufferedImages using a utility timer. Each frame is assigned to an integer (i.e. horseman frame one.png = frame0) and we use the utility timer to cycle through the images, to enable animation, with .png files instead of a .gif file, which can be useful in a number of 2d programming situations (2d game programming) for example, if you needed the last frame to pause 20 seconds more then the first 3, (like something exploded and left a dust cloud) a .gif file would work, but you would have to be messy about it and add 16 of the same frame, it just seemed cleaner overall to use a png and utility timer or 4 .pngs instead of a .gif cause I encountered a problem where it seemed like .png was cleaner then gif, or a better solution.. but it takes more time, and is not as easy. Lucky for you I have provided or I hope to provide you an example that you can use / reference or modify. I don't get mad about legal stuff because I know most of you wouldn't steal anything (or all of you) but i do like the GNU lesser agreement license, so I'm going to say this code is provided to you under the conditions and terms of the GNU lesser public license.

If you were following the last example you would know this next example contains images which you must put in you src folder in your workspace. If you dont have a java IDE or framework source JRE you may want to download the JDK (java development kit(which includes the JRE*)), then assuming if you don't have that, you need some sort of java IDE such as eclipe or netbeans. I prefer eclipse at the moment because (#1 it was my first java ide (I'm a eclipse native)) and also because it just seems better, where if I could go into detail I would say "well it just has the java packages / javadoc / .jar hyarchy right there where I know where it is already" and in netbeans i can't find the damn .jar hyarchy yet, + I have invested a lot of time in eclipse so I have set a few things up / have experience, etc. Nothing wrong with netbeans though. Most professional java devlopers use netbeans and it is more reccomended and sponsored or encouraged on oracle's website. So lets put these images on there haha or I'll give you the images for this code example and you can put it all in your IDE if you want, remember to create drawFramePackage, or rename the files.

horseman frame 1 horseman frame2 horse man frame 3 horse man frame 4

Right click on the image and click "save as" to download this image (save to your java SRC folder in your workspace!)

So those are the images used to make an animation, and here is the code used below . If you plug it in correctly you should see horseman animating in a JFrame... or you can send me an email at

package drawFramePackage;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class animated extends JPanel implements MouseListener{
	JFrame j;
	BufferedImage f1, f2, f3, f4;
	BufferedImage frames[] = {f1, f2, f3, f4};
	String bIN[] = {"horsemanFrame1.png", "horsemanFrame2.png", "horsemanFrame3.png",
	Integer whichFrame;
	Point mouseLocation;
	Rectangle rec;
	 * @param args
	 * @throws IOException 
	public static void main(String[] args) throws IOException {
		new animated();

	public animated() throws IOException{
		rec = new Rectangle(525, 10, 50, 50);
		mouseLocation = new Point();
		whichFrame = new Integer(0);
		for (int i = 0; i <= bIN.length - 1; i++){
			frames[i] = File(bIN[i]));
		j = new JFrame();
		j.setSize(new Dimension(600, 400));
		new timer();
	public void paintComponent(Graphics g){
		Graphics2D g2d = (Graphics2D) g;
		g2d.drawImage(frames[whichFrame], new AffineTransform(), this);
	class timer extends TimerTask{
		Timer t;
		public timer(){
			t = new Timer();
			t.schedule(this, 0, 120);
		public void run() {
			if (whichFrame == 3){
				whichFrame = 0;
	public void mouseClicked(MouseEvent e) {
		mouseLocation = e.getPoint();
		mouseLocation.translate(-8, -31);
		if (rec.contains(mouseLocation.x, mouseLocation.y)){
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub