Animated JLabel by showing a list of images in Java

Description

The following code shows how to animated JLabel by showing a list of images.

Example


 /* ww w.jav a  2s  . c o  m*/
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class AnimatedLabel extends JLabel implements Runnable {
  protected Icon[] icons;

  protected int index = 0;

  protected boolean isRunning;

  public AnimatedLabel(String gifName, int numGifs) {
    icons = new Icon[numGifs];
    for (int i = 0; i < numGifs; i++)
      icons[i] = new ImageIcon(gifName + i + ".gif");
    setIcon(icons[0]);

    Thread tr = new Thread(this);
    tr.setPriority(Thread.MAX_PRIORITY);
    tr.start();
  }

  public void setRunning(boolean r) {
    isRunning = r;
  }

  public boolean getRunning() {
    return isRunning;
  }

  public void run() {
    while (true) {
      if (isRunning) {
        index++;
        if (index >= icons.length)
          index = 0;
        setIcon(icons[index]);
        Graphics g = getGraphics();
        icons[index].paintIcon(this, g, 0, 0);
      } else {
        if (index > 0) {
          index = 0;
          setIcon(icons[0]);
        }
      }
      try {
        Thread.sleep(500);
      } catch (Exception ex) {
      }
    }
  }
}




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer