Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Animating Images in Java Application

This section shows you how to create an animation with multiple images.



Animating Images in Java Application


This section shows you how to create an animation with multiple images. You can see how animation has been implemented in the following program or example. The given program implements the animation using more than one images. Image are given below:

First image of swing animation

Second image of swing animation

In this program, images are displayed one by one from the list of the images. Images are added to the label on the frame or window. This program shows the image one by one after 1000 milliseconds or 1 second. The time delaying is maintained by thread. 

Code Description:

One method has been used to show images on the frame one by one during the 1000 milliseconds. This collection of images makes the application as an animation.

sleep(int time):
This is the method of the Thread class which is used to sleep the program for the specified time. The time to sleep is passed through the
sleep() method of the Thread class as parameter.

Here is the code of the program:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingAnimation{
  Thread th;
  ImageIcon images;
  JFrame frame;
  JLabel lbl;
  int i = 0;
  int j;

  public static void main(String[] args){
  SwingAnimation sa = new SwingAnimation();

  public SwingAnimation(){
  frame = new JFrame("Animation Frame");
  th = new Thread();
  lbl = new JLabel();
  Panel panel = new Panel();
  frame.add(panel, BorderLayout.CENTER);
  for(j = 1; j <= 2; j++){
  if (j == 2)
  j = 0;

  public void SwingAnimator(){
  for(i = 1; i <= 5; i++){
  images = new ImageIcon("images/img" + i + ".gif");
  catch(InterruptedException e){}

Download this example.


Share on Google+Share on Google+

Animating Images in Java Application

Posted on: April 14, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Animating Images in Java Application   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 3, 2011

which folder do I put the image on, I tried putting below the code but I am not getting the image, but the code runds without the message
October 21, 2011
about JPanel

pls tell me about the concept of JPanel, why we are using that even frame also does the same job, why we are using getContentPane method the below codings are executed similarly but wat will be the exact use of this and which situation we had to use. . . . . . frame.getContentPane.add(panel,BorderLayout.CENTER); is same for frame.add(panel,BorderLayout.CENTER);
February 1, 2012
Image saving

please mention how to save the image and where??
August 5, 2012

images = new ImageIcon("images/img" + i + ".gif"); i cant understand this line..please explain..then what is the name of the first image and what is the name of the second image.? then assume that i've four images named as 123.jpg,124.jpg, 125.jpg, 126.jpg. How the above program converted to animate the four images?