- 浏览: 130327 次
- 性别:
- 来自: Heaven
文章分类
最新评论
-
jiakechong:
借鉴学习下
java线程分析 -
右上角的爱:
哥们,为什么运行不出来啊
JAVA版 山寨QQ 附图 附源码 -
zl544434558:
Eclipse插件工程目录介绍 -
Irving_wei:
wj99 写道我也想请教一下,请问兄台QQ号多少啊?
3154 ...
JAVA版 山寨QQ 附图 附源码 -
wj99:
我也想请教一下,请问兄台QQ号多少啊?
JAVA版 山寨QQ 附图 附源码
站在巨人的肩膀上:http://blog.csdn.net/xcl119xcl/archive/2010/01/22/5224444.aspx
因为SWT的ImageLoader支持读写以上所有格式的图片,所以实现起来比较简单。主要解决了两个问题。第一个问题是播放GIF动画,通过 ImageLoader读入GIF的所有帧以及间隔时间,然后用Display.timerExec实现Timer播放。第二个问题是对图片的 Scrollbar支持以及pack支持。SWT.H_SCROLL和SWT.V_SCROLL 虽然加上了滚动条,但是不起作用,需要监听滚动条的SWT.Selection事件。另外,加上滚动条后,pack无法得到大小,不能正确的pack。需要重载computeSize。
测试程序
在对GIF图片的支持上,Swing要做的好很多,一句label.setIcon(new ImageIcon(name))就搞定GIF动画了。
因为SWT的ImageLoader支持读写以上所有格式的图片,所以实现起来比较简单。主要解决了两个问题。第一个问题是播放GIF动画,通过 ImageLoader读入GIF的所有帧以及间隔时间,然后用Display.timerExec实现Timer播放。第二个问题是对图片的 Scrollbar支持以及pack支持。SWT.H_SCROLL和SWT.V_SCROLL 虽然加上了滚动条,但是不起作用,需要监听滚动条的SWT.Selection事件。另外,加上滚动条后,pack无法得到大小,不能正确的pack。需要重载computeSize。
/** * 负责显示各种格式的图片 * * @author 喜来乐哈哈 */ 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 ScrollBar hBar; private ScrollBar vBar; private Color bg; private Display display; public ImageViewer(Composite parent) { super (parent, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL); hBar = getHorizontalBar(); vBar = getVerticalBar(); bg = getBackground(); display = getDisplay(); addListeners(); } public void setImage(ImageData imageData) { checkWidget(); stopAnimationTimer(); this .image = new Image(display, imageData); this .imageDatas = null ; this .images = null ; redraw(); } /** * @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() { hBar.addListener(SWT.Selection, new Listener() { public void handleEvent(Event arg0) { hscroll(); } }); vBar.addListener(SWT.Selection, new Listener() { public void handleEvent(Event arg0) { vscroll(); } }); addListener(SWT.Resize, new Listener() { public void handleEvent(Event e) { resize(); } }); addListener(SWT.Paint, new Listener() { public void handleEvent(Event e) { paint(e); } }); } void hscroll() { Image image = getCurrentImage(); if (image != null ) { int hSelection = hBar.getSelection(); int destX = - hSelection - origin.x; Rectangle rect = image.getBounds(); scroll(destX, 0 , 0 , 0 , rect.width, rect.height, false ); origin.x = - hSelection; } } void vscroll() { Image image = getCurrentImage(); if (image != null ) { int vSelection = vBar.getSelection(); int destY = - vSelection - origin.y; Rectangle rect = image.getBounds(); scroll( 0 , destY, 0 , 0 , rect.width, rect.height, false ); origin.y = - vSelection; } } void resize() { Image image = getCurrentImage(); if (image == null ) return ; Rectangle rect = image.getBounds(); Rectangle client = getClientArea(); hBar.setMaximum(rect.width); vBar.setMaximum(rect.height); hBar.setThumb(Math.min(rect.width, client.width)); vBar.setThumb(Math.min(rect.height, client.height)); int hPage = rect.width - client.width; int vPage = rect.height - client.height; int hSelection = hBar.getSelection(); int vSelection = vBar.getSelection(); if (hSelection >= hPage) { if (hPage <= 0 ) hSelection = 0 ; origin.x = - hSelection; } if (vSelection >= vPage) { if (vPage <= 0 ) vSelection = 0 ; origin.y = - vSelection; } redraw(); } 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 class ImageCanvasTest { public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); ImageViewer ic = new ImageViewer(shell); shell.setLayout( new FillLayout()); FileDialog dialog = new FileDialog(shell, SWT.OPEN); dialog.setText( " Open an image file or cancel " ); String string = dialog.open(); ImageLoader loader = new ImageLoader(); ImageData[] imageDatas = loader.load(string); if (imageDatas.length == 0 ) return ; else if (imageDatas.length == 1 ) { ic.setImage(imageDatas[ 0 ]); } else { ic.setImages(imageDatas, loader.repeatCount); } ic.pack(); shell.pack(); shell.open(); while ( ! shell.isDisposed()) { if ( ! display.readAndDispatch()) display.sleep(); } display.dispose(); } }
在对GIF图片的支持上,Swing要做的好很多,一句label.setIcon(new ImageIcon(name))就搞定GIF动画了。
发表评论
-
Eclipse 国际化
2012-12-17 17:31 1272Eclipse 提供了方便的国际化实现方式。 例子 ... -
CreateProcess error=87
2012-12-13 15:05 11393站在巨人的肩膀上:http ... -
sssss
2011-06-16 21:47 1140output error file to the follow ... -
在SWT中使用Swing
2011-06-16 11:26 4091在SWT出现之前,已经有众多的Swing程序和控件被开发出来, ... -
SWT 同步和异步
2011-06-15 10:52 1594站在巨人的肩膀上:http ... -
toolbarPath
2011-06-09 11:10 978· 文件: org.eclipse.ui.workbench. ... -
menubarPath
2011-06-09 11:05 785· 文件:file · "开始" 区 ... -
Eclipse插件工程目录介绍
2011-06-03 14:05 2558Eclipse插件工程目录详解 ---站在巨人 ... -
网上收集
2010-12-01 11:00 9821,使用第三方控件,在 ... -
AWT-Swing-SWT
2010-11-21 14:21 1280SWT与Sun的AWT/Swing源 ... -
可遇不可求的plugins开发程序员
2010-09-01 11:18 1596从开始的SWT学习,到现 ...
相关推荐
SWT 3.0 开始引入了 Tray,可以在系统栏放置你的程序图标了 本程序实现的功能有四: * 1. 点击窗口的最小化或关闭按钮都是隐藏窗口--任务栏里不显示,不退出程序 * 2. 窗口隐藏时,任务栏无图标,系统栏有图标;...
主要介绍了java使用swt显示图片示例,修改后就可变为图片浏览器,需要的朋友可以参考下
手写基于Swt/Jface图片工厂实现图片传输界面更新。应用于项目中的工具类,供大家学习参考。提供一种新的学习思路。
用Eclipse SWT 实现的动态背景对话框的源码,动态背景为黑客帝国字符雨效果,极为炫酷。高端大气且上档次。
SWT.zip SWT 图形化 各种jar
SWT配置 SWT配置 SWT配置 SWT配置 SWT配置 SWT配置 SWT配置 SWT配置
swt动态图表相关jar包,jfreechart实现配合相关源码
swt图形界面开发,运用的是java知识
64位windows操作系统使用的SWT库
java swt 实现的隐藏和显示任务栏图标和托盘图标的(导入swt包即可) 资源名称是:java swt 隐藏和显示任务栏图标和托盘图标(2)
swt工具文档 swt工具文档 swt工具文档 swt工具文档 swt工具文档 swt工具文档
java swt 实现的隐藏和显示任务栏图标和托盘图标的(导入swt包即可)
SWT-Internal-Video使用SWT技术对视频的常用显示功能进行了实现和封装,并提供了API。 它的接口之灵活性可以满足视频显示上的很多要求 与 情况。 SWT-Internal-Video解决的是程序与视频设备DVR/DVS之间的链接,并...
SWT examples,SWT官方例子:http://www.eclipse.org/swt/examples.php 该页面所要下载的例子 其中,swt的例子在\plugins\org.eclipse.swt.examples.source_3.7.0.v3740f.jar中
SWT插件中文教程SWT常用方法SWT插件中文教程SWT常用方法SWT插件中文教程SWT常用方法SWT插件中文教程SWT常用方法SWT插件中文教程SWT常用方法SWT插件中文教程SWT常用方法
swt背景拖动,拖动背景图片不将图片拖出窗口
swt教程 有关SWT怎么应用
swt 基础教程 swt 基础教程 swt 基础教程swt 基础教程swt 基础教程
Java socket编程使用SWT编写界面,远程升级程序.class文件,不是工程源码,工程里面还有其他的,不方便发。https://blog.csdn.net/L_e_c/article/details/110423909
下载编译的SWT库,版本号4.911,包含多个版本: org.eclipse.swt.cocoa.macosx.x86_64 org.eclipse.swt.gtk.linux.aarch64 org.eclipse.swt.gtk.linux.arm org.eclipse.swt.gtk.linux.ppc64le org.eclipse.swt.gtk....