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();
    panel.add(lbl);
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(400, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    for(j = 1; j <= 2; j++){
      SwingAnimator();
      if (j == 2)
        j = 0;
    }
  }

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

Can anyone explain this part for me? I just can't get it.. :

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

Thanks for advances~

images are changed every second

th.sleep(1000);

isn't correct and good idea use of Thread.sleep(int dealy) for Swing, instead of sleep I'd prefer to use java.swing.Timer

images are changed every second

th.sleep(1000);

isn't correct and good idea use of Thread.sleep(int dealy) for Swing, instead of sleep I'd prefer to use java.swing.Timer

Actually i get this from some tutorial website..
How about this line ?

images = new ImageIcon("images/img" + i + ".gif");

reads files images/img1.gif images/img2.gif images/img3.gif etc
and uses them to create graphical icons that can be displayed in a JLabel

this code doesn't reads any ImageIcons and I think that doesn't generated any exceptions (API) from null imageIcon, that's all for this code

That's right. If new ImageIcon fails it doesn't throw anything, it just returns null, which is really dumb.

Sorry but i just a beginner on java =(..Do you got better coding for this? And where u guys go and learn these?

Do you got better coding for this?

Not really - just check for the ImageIcon not being null

And where u guys go and learn these?

The secret is to write lots and lots of programs, then use Google and DaniWeb (in that order) when you get stuck

This article has been dead for over six months. Start a new discussion instead.