Write an animated GIF to a file : 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. 2. Write an animated GIF to a file
/*******************************************************************************
 * Copyright (c) 2000, 2005 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;
/*
 * Write an animated GIF to a file.
 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
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.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

public class AnimatedGIFWriteFile {
  static Display display;

  static Color white, red, green, blue;

  static PaletteData palette;

  static Font font;

  public static void main(String[] args) {
    display = new Display();
    RGB whiteRGB = new RGB(0xff0xff0xff);
    RGB redRGB = new RGB(0xff00);
    RGB greenRGB = new RGB(00xff0);
    RGB blueRGB = new RGB(000xff);
    palette = new PaletteData(new RGB[] { whiteRGB, // 0
        redRGB, // 1
        greenRGB, // 2
        blueRGB })// 3
    white = new Color(display, whiteRGB);
    red = new Color(display, redRGB);
    green = new Color(display, greenRGB);
    blue = new Color(display, blueRGB);
    font = new Font(display, "Comic Sans MS"24, SWT.BOLD);

    ImageData[] data = new ImageData[4];
    data[0= newFrame("", white, false, 0010155, SWT.DM_FILL_NONE, 40);
    data[1= newFrame("S", red, true, 003055, SWT.DM_FILL_NONE, 40);
    data[2= newFrame("W", green, true, 2803955, SWT.DM_FILL_NONE, 40);
    data[3= newFrame("T", blue, true, 6903255, SWT.DM_FILL_BACKGROUND, 200);

    ImageLoader loader = new ImageLoader();
    loader.data = data;
    loader.backgroundPixel = 0;
    loader.logicalScreenHeight = data[0].height;
    loader.logicalScreenWidth = data[0].width;
    loader.repeatCount = 0// run forever
    loader.save("swt.gif", SWT.IMAGE_GIF);

    white.dispose();
    red.dispose();
    green.dispose();
    blue.dispose();
    font.dispose();
    display.dispose();

  }

  private static ImageData newFrame(String letter, Color color, boolean transparent, int x, int y,
      int width, int height, int disposalMethod, int delayTime) {
    ImageData temp = new ImageData(width, height, 2, palette)// 4-color
                                                                // palette has
                                                                // depth 2
    Image image = new Image(display, temp);
    GC gc = new GC(image);
    gc.setBackground(white);
    gc.fillRectangle(00, width, height);
    gc.setForeground(color);
    gc.setFont(font);
    gc.drawString(letter, 55);
    gc.dispose();
    ImageData frame = image.getImageData();
    if (transparent)
      frame.transparentPixel = 0// white
    image.dispose();
    frame.x = x;
    frame.y = y;
    frame.disposalMethod = disposalMethod;
    frame.delayTime = delayTime;
    return frame;
  }
}
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.