整理了一个工具类, 用于在SWT中显示GIF图片
/**
* 用于显示GIF图片的util类
*
* @author mark.wang
*/
public class ImageViewer extends Canvas {
protected Point origin = new Point(0, 0);
protected Image image;
protected ImageData[] imageDatas;
protected Image[] images;
protected int current;
private int repeatCount;
private Runnable animationTimer;
private Color bg;
private Display display;
public ImageViewer(Composite parent) {
super(parent, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE);
bg = getBackground();
display = getDisplay();
addListeners();
}
/**
* @param repeatCount
* 0 forever
*/
public void setImages(ImageData[] imageDatas, int repeatCount) {
checkWidget();
this.image = null;
this.imageDatas = imageDatas;
this.repeatCount = repeatCount;
convertImageDatasToImages();
startAnimationTimer();
redraw();
}
@Override
public Point computeSize(int wHint, int hHint, boolean changed) {
checkWidget();
Image image = getCurrentImage();
if (image != null) {
Rectangle rect = image.getBounds();
Rectangle trim = computeTrim(0, 0, rect.width, rect.height);
return new Point(trim.width, trim.height);
}
return new Point(wHint, hHint);
}
@Override
public void dispose() {
if (image != null)
image.dispose();
if (images != null)
for (int i = 0; i < images.length; i++)
images[i].dispose();
super.dispose();
}
protected void paint(Event e) {
Image image = getCurrentImage();
if (image == null)
return;
GC gc = e.gc;
gc.drawImage(image, origin.x, origin.y);
gc.setBackground(bg);
Rectangle rect = image.getBounds();
Rectangle client = getClientArea();
int marginWidth = client.width - rect.width;
if (marginWidth > 0) {
gc.fillRectangle(rect.width, 0, marginWidth, client.height);
}
int marginHeight = client.height - rect.height;
if (marginHeight > 0) {
gc.fillRectangle(0, rect.height, client.width, marginHeight);
}
}
void addListeners() {
addListener(SWT.Paint, new Listener() {
public void handleEvent(Event e) {
paint(e);
}
});
}
void convertImageDatasToImages() {
images = new Image[imageDatas.length];
// Step 1: Determine the size of the resulting images.
int width = imageDatas[0].width;
int height = imageDatas[0].height;
// Step 2: Construct each image.
int transition = SWT.DM_FILL_BACKGROUND;
for (int i = 0; i < imageDatas.length; i++) {
ImageData id = imageDatas[i];
images[i] = new Image(display, width, height);
GC gc = new GC(images[i]);
// Do the transition from the previous image.
switch (transition) {
case SWT.DM_FILL_NONE:
case SWT.DM_UNSPECIFIED:
// Start from last image.
gc.drawImage(images[i - 1], 0, 0);
break;
case SWT.DM_FILL_PREVIOUS:
// Start from second last image.
gc.drawImage(images[i - 2], 0, 0);
break;
default:
// DM_FILL_BACKGROUND or anything else,
// just fill with default background.
gc.setBackground(bg);
gc.fillRectangle(0, 0, width, height);
break;
}
// Draw the current image and clean up.
Image img = new Image(display, id);
gc.drawImage(img, 0, 0, id.width, id.height, id.x, id.y, id.width,
id.height);
img.dispose();
gc.dispose();
// Compute the next transition.
// Special case: Can't do DM_FILL_PREVIOUS on the
// second image since there is no "second last"
// image to use.
transition = id.disposalMethod;
if (i == 0 && transition == SWT.DM_FILL_PREVIOUS)
transition = SWT.DM_FILL_NONE;
}
}
Image getCurrentImage() {
if (image != null)
return image;
if (images == null)
return null;
return images[current];
}
void startAnimationTimer() {
if (images == null || images.length < 2)
return;
final int delay = imageDatas[current].delayTime * 10;
display.timerExec(delay, animationTimer = new Runnable() {
public void run() {
if (isDisposed())
return;
current = (current + 1) % images.length;
redraw();
if (current + 1 == images.length && repeatCount != 0
&& --repeatCount <= 0)
return;
display.timerExec(delay, this);
}
});
}
void stopAnimationTimer() {
if (animationTimer != null)
display.timerExec(-1, animationTimer);
}
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
CopyOfImageViewer ic = new CopyOfImageViewer(shell);
shell.setLayout(new FillLayout());
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setText(" Open an gif image file or cancel, only gif ");
String string = dialog.open();
ImageLoader loader = new ImageLoader();
ImageData[] imageDatas = loader.load(string);
ic.setImages(imageDatas, loader.repeatCount);
ic.pack();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
分享到:
相关推荐
swt工具文档 swt工具文档 swt工具文档 swt工具文档 swt工具文档 swt工具文档
工具类实现了分页,虽然还有些欠缺,如在搜索栏和导出菜单未考虑分页的情况
java SWT简单小工具java SWT简单小工具java SWT简单小工具java SWT简单小工具
手写基于Swt/Jface图片工厂实现图片传输界面更新。应用于项目中的工具类,供大家学习参考。提供一种新的学习思路。
用Eclipse SWT 实现的动态背景对话框的源码,动态背景为黑客帝国字符雨效果,极为炫酷。高端大气且上档次。
SWT 3.0 开始引入了 Tray,可以在系统栏放置你的程序图标了 本程序实现的功能有四: * 1. 点击窗口的最小化或关闭按钮都是隐藏窗口--任务栏里不显示,不退出程序 * 2. 窗口隐藏时,任务栏无图标,系统栏有图标;...
主要介绍了java使用swt显示图片示例,修改后就可变为图片浏览器,需要的朋友可以参考下
swt link类用法
SWT配置 SWT配置 SWT配置 SWT配置 SWT配置 SWT配置 SWT配置 SWT配置
swt文件编码转换工具;附件中包括源码和执行文件;双击FileCharSet_fat.jar即可执行(前提条件要有java环境)。
DJNativeSwing.jar,DJNativeSwing-SWT.jar,swt-win-x64.jar(另提供swt-windows.jar32位下载) 自带pom.xml maven文件---亲测都可使用
64位windows操作系统使用的SWT库
SWT实现图片列表预览, 封装了一个用于展示图片的类,根据构造方法可以设置显示的行数及列数。根据设置的宽高进行自适应,可以实现滚动条,选择,查看,全选,反选,图片大小自适应等其他操作。
swt背景拖动,拖动背景图片不将图片拖出窗口
java swt 实现的隐藏和显示任务栏图标和托盘图标的(导入swt包即可) 资源名称是:java swt 隐藏和显示任务栏图标和托盘图标(2)
SWT表格管理类(包括表头排序,隔行颜色,表格宽度自适应)
swt教程 有关SWT怎么应用
这是我找到的最全的SWT示例,即使在官方网站上也非常难找,示例包括以下内容:SWT模式窗体,SWT浏览器,SWT树,SWT工具条,SWT画图工具,SWT图形,SWT各种布局,SWT剪贴板,SWT快捷键,SWT菜单,SWT文本编辑器,SWT...
SWT插件中文教程SWT常用方法SWT插件中文教程SWT常用方法SWT插件中文教程SWT常用方法SWT插件中文教程SWT常用方法SWT插件中文教程SWT常用方法SWT插件中文教程SWT常用方法
SWT examples,SWT官方例子:http://www.eclipse.org/swt/examples.php 该页面所要下载的例子 其中,swt的例子在\plugins\org.eclipse.swt.examples.source_3.7.0.v3740f.jar中