스니펫:GIF 애니메이션 표시하기

위클립스
이동: 둘러보기, 찾기
스니펫 분류
스니펫 (3)
스니펫/JDT (1)
스니펫/PDE (3)
스니펫/SWT (7)

[편집] 결과

Java-duke.gif

[편집] 소스

import java.io.IOException;
import java.net.URL;
 
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
 
public class GIFAnimation {
   public static void main(String[] args) throws IOException {
      URL url = new URL("http://eclipse.or.kr/images/a/ab/Java-duke.gif");
      ImageLoader loader = new ImageLoader();
      final ImageData[] frames = loader.load(url.openStream());
 
      final Display display = Display.getDefault();
      Shell shell = new Shell(display, SWT.SHELL_TRIM);
      shell.setLayout(new FillLayout());
 
      final Label label = new Label(shell, SWT.NORMAL);
      label.setImage(new Image(display, frames[0]));
      shell.pack();
 
      Thread animation = new Thread() {
         int currentFrame = 0;
         boolean isDisposed = false;
 
         @Override
         public void run() {
            while (!isDisposed) {
               try {
                  sleep(frames[currentFrame].delayTime * 10);
               } catch (InterruptedException e) {
                  e.printStackTrace();
               }
 
               currentFrame = (currentFrame + 1) % frames.length;
               if (display.isDisposed()) {
                  return;
               }
               display.asyncExec(new Runnable() {
                  @Override
                  public void run() {
                     try {
                        Image newImage = new Image(display, frames[currentFrame]);
                        label.getImage().dispose();
                        label.setImage(newImage);
                     } catch (SWTException e) {
                        isDisposed = true;
                     }
 
                  }
               });
            }
 
         }
      };
      animation.start();
 
      shell.open();
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
 
      display.dispose();
   }
}
개인 도구
이름공간
변수
행위
포탈
탐색
도움
도구모음