Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 87829 Details for
Bug 196864
Toolbar should support animated gifs
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Snippet source code for animated GIFs in ToolBar
AnimatedToolbarSnippet.java (text/x-java), 5.69 KB, created by
Mark Powell
on 2008-01-24 22:14:36 EST
(
hide
)
Description:
Snippet source code for animated GIFs in ToolBar
Filename:
MIME Type:
Creator:
Mark Powell
Created:
2008-01-24 22:14:36 EST
Size:
5.69 KB
patch
obsolete
>package org.eclipse.swt.snippets; >import java.io.File; > >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; >import org.eclipse.swt.widgets.ToolBar; >import org.eclipse.swt.widgets.ToolItem; > >public class AnimatedToolbarSnippet { > > static Display display; > static Shell shell; > static GC shellGC; > static Color shellBackground; > static ImageLoader[] loader; > static ImageData[][] imageDataArray; > static Thread animateThread[]; > static Image[][] image; > private static ToolItem item[]; > static final boolean useGIFBackground = false; > > public static void main (String [] args) { > display = new Display(); > Shell shell = new Shell (display); > shellBackground = shell.getBackground(); > FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI); > dialog.setFilterExtensions(new String[] {"*.gif"}); > String filename = dialog.open(); > String filenames[] = dialog.getFileNames(); > int numToolBarItems = filenames.length; > if (numToolBarItems > 0) { > try { > loadAllImages(new File(filename).getParent(), filenames); > } catch (SWTException e) { > System.err.println("There was an error loading an image."); > e.printStackTrace(); > } > ToolBar toolBar = new ToolBar (shell, SWT.FLAT | SWT.BORDER); > item = new ToolItem[numToolBarItems]; > for (int i = 0; i < numToolBarItems; i++) { > item[i] = new ToolItem (toolBar, SWT.DROP_DOWN); > item[i].setImage(image[i][0]); > } > toolBar.pack (); > shell.open (); > > startAnimationThreads(); > > while (!shell.isDisposed()) { > if (!display.readAndDispatch ()) display.sleep (); > } > > for (int i = 0; i < numToolBarItems; i++) { > for (int j = 0; j < image[i].length; j++) { > image[i][j].dispose(); > } > } > display.dispose (); > } > } > > private static void loadAllImages(String directory, String[] filenames) throws SWTException { > int numItems = filenames.length; > loader = new ImageLoader[numItems]; > imageDataArray = new ImageData[numItems][]; > image = new Image[numItems][]; > for (int i = 0; i < numItems; i++) { > loader[i] = new ImageLoader(); > int fullWidth = loader[i].logicalScreenWidth; > int fullHeight = loader[i].logicalScreenHeight; > imageDataArray[i] = loader[i].load(directory + File.separator + filenames[i]); > int numFramesOfAnimation = imageDataArray[i].length; > image[i] = new Image[numFramesOfAnimation]; > for (int j = 0; j < numFramesOfAnimation; j++) { > if (j == 0) { > //for the first frame of animation, just draw the first frame > image[i][j] = new Image(display, imageDataArray[i][j]); > fullWidth = imageDataArray[i][j].width; > fullHeight = imageDataArray[i][j].height; > } > else { > //after the first frame of animation, draw the background or previous frame first, then the new image data > image[i][j] = new Image(display, fullWidth, fullHeight); > GC gc = new GC(image[i][j]); > gc.setBackground(shellBackground); > gc.fillRectangle(0, 0, fullWidth, fullHeight); > ImageData imageData = imageDataArray[i][j]; > switch (imageData.disposalMethod) { > case SWT.DM_FILL_BACKGROUND: > /* Fill with the background color before drawing. */ > Color bgColor = null; > if (useGIFBackground && loader[i].backgroundPixel != -1) { > bgColor = new Color(display, imageData.palette.getRGB(loader[i].backgroundPixel)); > } > gc.setBackground(bgColor != null ? bgColor : shellBackground); > gc.fillRectangle(imageData.x, imageData.y, imageData.width, imageData.height); > if (bgColor != null) bgColor.dispose(); > break; > default: > /* Restore the previous image before drawing. */ > gc.drawImage( > image[i][j-1], > 0, > 0, > fullWidth, > fullHeight, > 0, > 0, > fullWidth, > fullHeight); > break; > } > Image newFrame = new Image(display, imageData); > gc.drawImage(newFrame, > 0, > 0, > imageData.width, > imageData.height, > imageData.x, > imageData.y, > imageData.width, > imageData.height); > newFrame.dispose(); > gc.dispose(); > } > } > } > } > > private static void startAnimationThreads() { > animateThread = new Thread[image.length]; > for (int ii = 0; ii < image.length; ii++) { > final int i = ii; > animateThread[i] = new Thread("Animation "+i) { > int imageDataIndex = 0; > public void run() { > try { > int repeatCount = loader[i].repeatCount; > while (loader[i].repeatCount == 0 || repeatCount > 0) { > imageDataIndex = (imageDataIndex + 1) % imageDataArray[i].length; > if (!display.isDisposed()) { > display.asyncExec(new Runnable() { > public void run() { > if (!item[i].isDisposed()) > item[i].setImage(image[i][imageDataIndex]); > } > }); > } > > /* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */ > try { > int ms = imageDataArray[i][imageDataIndex].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[i].length - 1) repeatCount--; > } > } catch (SWTException ex) { > System.out.println("There was an error animating the GIF"); > ex.printStackTrace(); > } > } > }; > animateThread[i].setDaemon(true); > animateThread[i].start(); > } > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 196864
: 87829 |
87830
|
87831
|
87832