显示动画GIF : PNG和GIF « SWT 二维图形 « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » SWT 二维图形 » PNG和GIF 
18. 18. 1. 显示动画GIF
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * Image example snippet: display an animated GIF
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

public class AnimatedGIFDisplay {
  static Display display;

  static Shell shell;

  static GC shellGC;

  static Color shellBackground;

  static ImageLoader loader;

  static ImageData[] imageDataArray;

  static Thread animateThread;

  static Image image;

  static final boolean useGIFBackground = false;

  public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setSize(300300);
    shell.open();
    shellGC = new GC(shell);
    shellBackground = shell.getBackground();

    FileDialog dialog = new FileDialog(shell);
    dialog.setFilterExtensions(new String[] { "*.gif" });
    String fileName = dialog.open();
    if (fileName != null) {
      loader = new ImageLoader();
      try {
        imageDataArray = loader.load(fileName);
        if (imageDataArray.length > 1) {
          animateThread = new Thread("Animation") {
            public void run() {
              /*
               * Create an off-screen image to draw on, and fill it with the
               * shell background.
               */
              Image offScreenImage = new Image(display, loader.logicalScreenWidth,
                  loader.logicalScreenHeight);
              GC offScreenImageGC = new GC(offScreenImage);
              offScreenImageGC.setBackground(shellBackground);
              offScreenImageGC.fillRectangle(00, loader.logicalScreenWidth,
                  loader.logicalScreenHeight);

              try {
                /* Create the first image and draw it on the off-screen image. */
                int imageDataIndex = 0;
                ImageData imageData = imageDataArray[imageDataIndex];
                if (image != null && !image.isDisposed())
                  image.dispose();
                image = new Image(display, imageData);
                offScreenImageGC.drawImage(image, 00, imageData.width, imageData.height,
                    imageData.x, imageData.y, imageData.width, imageData.height);

                /*
                 * Now loop through the images, creating and drawing each one on
                 * the off-screen image before drawing it on the shell.
                 */
                int repeatCount = loader.repeatCount;
                while (loader.repeatCount == || repeatCount > 0) {
                  switch (imageData.disposalMethod) {
                  case SWT.DM_FILL_BACKGROUND:
                    /* Fill with the background color before drawing. */
                    Color bgColor = null;
                    if (useGIFBackground && loader.backgroundPixel != -1) {
                      bgColor = new Color(display, imageData.palette.getRGB(loader.backgroundPixel));
                    }
                    offScreenImageGC.setBackground(bgColor != null ? bgColor : shellBackground);
                    offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width,
                        imageData.height);
                    if (bgColor != null)
                      bgColor.dispose();
                    break;
                  case SWT.DM_FILL_PREVIOUS:
                    /* Restore the previous image before drawing. */
                    offScreenImageGC.drawImage(image, 00, imageData.width, imageData.height,
                        imageData.x, imageData.y, imageData.width, imageData.height);
                    break;
                  }

                  imageDataIndex = (imageDataIndex + 1% imageDataArray.length;
                  imageData = imageDataArray[imageDataIndex];
                  image.dispose();
                  image = new Image(display, imageData);
                  offScreenImageGC.drawImage(image, 00, imageData.width, imageData.height,
                      imageData.x, imageData.y, imageData.width, imageData.height);

                  /* Draw the off-screen image to the shell. */
                  shellGC.drawImage(offScreenImage, 00);

                  /*
                   * Sleep for the specified delay time (adding commonly-used
                   * slow-down fudge factors).
                   */
                  try {
                    int ms = imageData.delayTime * 10;
                    if (ms < 20)
                      ms += 30;
                    if (ms < 30)
                      ms += 10;
                    Thread.sleep(ms);
                  catch (InterruptedException e) {
                  }

                  /*
                   * If we have just drawn the last image, decrement the repeat
                   * count and start again.
                   */
                  if (imageDataIndex == imageDataArray.length - 1)
                    repeatCount--;
                }
              catch (SWTException ex) {
                System.out.println("There was an error animating the GIF");
              finally {
                if (offScreenImage != null && !offScreenImage.isDisposed())
                  offScreenImage.dispose();
                if (offScreenImageGC != null && !offScreenImageGC.isDisposed())
                  offScreenImageGC.dispose();
                if (image != null && !image.isDisposed())
                  image.dispose();
              }
            }
          };
          animateThread.setDaemon(true);
          animateThread.start();
        }
      catch (SWTException ex) {
        System.out.println("There was an error loading the GIF");
      }
    }

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}
18. 18. PNG和GIF
18. 18. 1. 显示动画GIF
18. 18. 2. 写动画GIF到一个文件
18. 18. 3. 写图片到PNG文件
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.