Display an animated GIF : PNG GIF « SWT 2D Graphics « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » SWT 2D Graphics » PNG GIF 
18. 18. 1. Display an animated 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. Display an animated GIF
18. 18. 2. Write an animated GIF to a file
18. 18. 3. Write an Image to a PNG file
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.