Using GIF images to make simple animations is a common rendering technique, and swing supports GIF image formatting and can be animated automatically.
The simplest way to put a GIF picture on a swing component is to invoke the JButton or JLabel seticon (icon icon) method.
Another method is to rewrite the paintcomponent (Graphics g) or Paint (Graphics g) method. For example
public class ShowGifPanel extends JPanel{
ImageIcon image = new ImageIcon("/root/opt/loading.gif");
@Override
public void paint(Graphics g) {
g.drawImage(image.getImage(), 0, 0, this);
}
}
The above method renders the following 3 gif.
But the fact is: do not attempt to achieve the desired effect through such a simple process. If you do this, you'll soon find that the GIF refresh rate is often very fast, and it looks like the GIF frame refreshes quickly, or should be said too quickly.
Swing also provides a means of setting up a similar set of GIFs, which are presented through a round-robin display, as illustrated by the following figure.
This can be rendered, but you must provide multiple GIFs for an animation. To occupy space and to the art of the burden is unfavorable.
If you use SWT rendering Gif,eclipse provides a solution.
Http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet141.java?view=co
The basic principle is to be the gif of the various frames of the display, if you run the program loading.gif, look very quickly, you can modify the 131th to 137th line between the code to adjust the refresh rate. In this way SWT will be able to achieve the perfect processing gif.
Unfortunately, porting SWT's way to swing does not work very well. It takes a little extra work to achieve a perfect deal with GIF in swing.