Here is how to animate icon image in JFrame using the Timer class. The code written in the actionPerformed() here is executed for every 10 milliseconds so that the icon image keeps changing. The code is simple, just take a look at it.
The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class AnimateJFrameIconImage extends JFrame
{
Timer t;
int i;
public AnimateJFrameIconImage()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
setTitle("Animated Icon Image");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
// Create a Timer to do animation
t=new Timer(10,new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
// Set icon image for JFrame
setIconImage(new ImageIcon(getClass().getResource("/icons/busy-icon"+i+".png")).getImage());
i++;
// I have only 10 icons!
if(i==10) i=0;
}
});
t.start();
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
new AnimateJFrameIconImage();
}
}
The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)