中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 用户界面
SWT实现图片查看
作者:未知 时间:2005-07-24 21:16 出处:JR 责编:chinaitpower
              摘要:SWT实现图片查看
此程序演示了如何在窗体上绘制图片,以及滚动条的使用方法。
代码十分简单,我就因此不做过多的解释了。
本代码在Windows XP pro, Eclipse 2.1下测试通过。
运行此程序需要进行一些有别于普通Java Application的地方,请参见我的另一篇文章,在《Eclipse Workbench之外使用SWT和JFace》。
如果文中有不妥之处,还请指正。
  1. import org.eclipse.swt.*;
  2. import org.eclipse.swt.graphics.*;
  3. import org.eclipse.swt.widgets.*;
  4. public class d {
  5. public static void main (String [] args) {
  6.   Display display = new Display ();
  7.   final Shell shell = new Shell (display,
  8.     SWT.SHELL_TRIM | SWT.NO_BACKGROUND |
  9.     SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);
  10.   Image originalImage = null;
  11.   FileDialog dialog = new FileDialog (shell, SWT.OPEN);
  12.   dialog.setText ("选择一个图片文件或者取消");
  13.   String string = dialog.open ();
  14.   if (string != null) {
  15.     originalImage = new Image (display, string);
  16.   }
  17.   if (originalImage == null) {
  18.     int width = 150, height = 200;
  19.     originalImage = new Image (display, width, height);
  20.     GC gc = new GC (originalImage);
  21.     gc.fillRectangle (0, 0, width, height);
  22.     gc.drawLine (0, 0, width, height);
  23.     gc.drawLine (0, height, width, 0);
  24.     gc.drawText ("默认图片", 10, 10);
  25.     gc.dispose ();
  26.   }
  27.   final Image image = originalImage;
  28.   final Point origin = new Point (0, 0);
  29.   final ScrollBar hBar = shell.getHorizontalBar ();
  30.   hBar.addListener (SWT.Selection, new Listener () {
  31.     public void handleEvent (Event e) {
  32.       int hSelection = hBar.getSelection ();
  33.       int destX = -hSelection - origin.x;
  34.       origin.x = -hSelection;
  35.       Rectangle rect = image.getBounds ();
  36.       shell.scroll (destX, 0, 0, 0, rect.width, rect.height, false);
  37.     }
  38.   });
  39.   final ScrollBar vBar = shell.getVerticalBar ();
  40.   vBar.addListener (SWT.Selection, new Listener () {
  41.     public void handleEvent (Event e) {
  42.       int vSelection = vBar.getSelection ();
  43.       int destY = -vSelection - origin.y;
  44.       origin.y = -vSelection;
  45.       Rectangle rect = image.getBounds ();
  46.       shell.scroll (0, destY, 0, 0, rect.width, rect.height, false);
  47.     }
  48.   });
  49.   shell.addListener (SWT.Resize,  new Listener () {
  50.     public void handleEvent (Event e) {
  51.       Rectangle rect = image.getBounds ();
  52.       Rectangle client = shell.getClientArea ();
  53.       hBar.setMaximum (rect.width);
  54.       vBar.setMaximum (rect.height);
  55.       hBar.setThumb (Math.min (rect.width, client.width));
  56.       vBar.setThumb (Math.min (rect.height, client.height));
  57.       int hPage = rect.width - client.width;
  58.       int vPage = rect.height - client.height;
  59.       int hSelection = hBar.getSelection ();
  60.       int vSelection = vBar.getSelection ();
  61.       if (hSelection >= hPage) {
  62.         if (hPage <= 0) hSelection = 0;
  63.         origin.x = -hSelection;
  64.       }
  65.       if (vSelection >= vPage) {
  66.         if (vPage <= 0) vSelection = 0;
  67.         origin.y = -vSelection;
  68.       }
  69.       shell.redraw ();
  70.     }
  71.   });
  72.   shell.addListener (SWT.Paintnew Listener () {
  73.     public void handleEvent (Event e) {
  74.       GC gc = e.gc;
  75.       gc.drawImage (image, origin.x, origin.y);
  76.       Rectangle rect = image.getBounds ();
  77.       Rectangle client = shell.getClientArea ();
  78.       int marginWidth = client.width - rect.width;
  79.       if (marginWidth > 0) {
  80.         gc.fillRectangle (rect.width, 0, marginWidth, client.height);
  81.       }
  82.       int marginHeight = client.height - rect.height;
  83.       if (marginHeight > 0) {
  84.         gc.fillRectangle (0, rect.height, client.width, marginHeight);
  85.       }
  86.     }
  87.   });
  88.   shell.setSize (200, 150);
  89.   shell.open ();
  90.   while (!shell.isDisposed ()) {
  91.     if (!display.readAndDispatch ()) display.sleep ();
  92.   }
  93.   display.dispose ();
  94. }
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有