"Filthyrichclients" Reading notes (ii)-let swing correctly display gif

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.