Java Tutorial/SWT 2D Graphics/PNG GIF

Материал из Java эксперт
Перейти к: навигация, поиск

Display an animated GIF

   <source lang="java">

/*******************************************************************************

* 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(300, 300);
   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(0, 0, 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, 0, 0, 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 == 0 || 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, 0, 0, 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, 0, 0, imageData.width, imageData.height,
                     imageData.x, imageData.y, imageData.width, imageData.height);
                 /* Draw the off-screen image to the shell. */
                 shellGC.drawImage(offScreenImage, 0, 0);
                 /*
                  * 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();
 }

}</source>





Write an animated GIF to a file

   <source lang="java">

/*******************************************************************************

* 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(0xff, 0xff, 0xff);
   RGB redRGB = new RGB(0xff, 0, 0);
   RGB greenRGB = new RGB(0, 0xff, 0);
   RGB blueRGB = new RGB(0, 0, 0xff);
   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, 0, 0, 101, 55, SWT.DM_FILL_NONE, 40);
   data[1] = newFrame("S", red, true, 0, 0, 30, 55, SWT.DM_FILL_NONE, 40);
   data[2] = newFrame("W", green, true, 28, 0, 39, 55, SWT.DM_FILL_NONE, 40);
   data[3] = newFrame("T", blue, true, 69, 0, 32, 55, 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(0, 0, width, height);
   gc.setForeground(color);
   gc.setFont(font);
   gc.drawString(letter, 5, 5);
   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;
 }

}</source>





Write an Image to a PNG file

   <source lang="java">

/*******************************************************************************

* Copyright (c) 2000, 2006 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 Image to a PNG file.
* 
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/

import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.widgets.*; public class PNGFileWriteImage {

 public static void main(String[] args) {
   Display display = new Display();
   Font font = new Font(display, "Comic Sans MS", 24, SWT.BOLD);
   Image image = new Image(display, 87, 48);
   GC gc = new GC(image);
   gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
   gc.fillRectangle(image.getBounds());
   gc.setFont(font);
   gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
   gc.drawString("S", 3, 0);
   gc.setForeground(display.getSystemColor(SWT.COLOR_GREEN));
   gc.drawString("W", 25, 0);
   gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
   gc.drawString("T", 62, 0);
   gc.dispose();
   ImageLoader loader = new ImageLoader();
   loader.data = new ImageData[] {image.getImageData()};
   loader.save("swt.png", SWT.IMAGE_PNG);
   image.dispose();
   font.dispose();
   display.dispose();
 }

}</source>