使用SWT制作不规则窗体(已更新)

来源:互联网 发布:五星宏辉分析软件 编辑:程序博客网 时间:2024/06/05 05:57

原文:http://blog.csdn.net/javenwong/article/details/1674949

 

(注: 不好意思, 前几天发表完自己也没看, 原来没有发上来, 由于是第一次用CSDN, 没玩好,报歉!!!!)

    最近需要做一个桌面应用, 需要使用到这种不规则窗体的技术, 而在Java领域写Windows桌面应用, SWT是一个很不错的选择, 因为使用JNI, 界面不仅漂亮, 速度更比awt/swing快! 我把搜集的关于SWT制作不规则窗体的东西整理了一下, 并写了一个测试代码, 供大家有需要时使用!

下面是程序运行后的样图:

程序用到的样图:

不知道为什么, 上传源码压缩包总是不成功, 只好贴上来, 汗!~~

把下面的代码粘贴到Eclipse中, 按下 Ctrl+Shift+F 格式化即可!

===========================源码区===============================

package window;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

/**
* Title: JwWindow
* Description: 使用SWT制作不规则窗体
* Copyright (c) 2007 Javen Wong
* Create Date: 2007.6.29
* @author: Javen Wong, javenwong@hotmail.com
* @version: 1.0
*/
public class JwWindow {

protected Shell shell;
protected Display display = Display.getDefault();
protected Image image = null;
protected ImageData imageData = null;
protected boolean currentImageFlag;
protected boolean useImage;

/**
  * Launch the application
  * @param args
  */
public static void main(String[] args) {
  try {
   JwWindow window = new JwWindow();
   window.open();
  } catch (Exception e) {
   e.printStackTrace();
  }
}

/**
  * Open the window
  */
public void open() {
  createContents();
  shell.open();
  shell.layout();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }
}

/**
  * Create contents of the window
  */
protected void createContents() {
  shell = new Shell(display, SWT.NO_TRIM);
  shell.setSize(500, 375);
  shell.setText("SWT不规则窗体");
  createIrregularlyShell();
  Listener l = new Listener() {
   int startX, startY;

   public void handleEvent(Event e) {
    if (e.type == SWT.KeyDown && e.character == SWT.ESC) {
     shell.dispose();
    }
    if (e.type == SWT.MouseDown && e.button == 1) {
     startX = e.x;
     startY = e.y;
    }
    if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
     Point p = shell.toDisplay(e.x, e.y);
     p.x -= startX;
     p.y -= startY;
     shell.setLocation(p);
    }
    if (e.type == SWT.Paint) {
     if (useImage) e.gc.drawImage(image, imageData.x, imageData.y);
    }
   }
  };
  shell.addListener(SWT.KeyDown, l);
  shell.addListener(SWT.MouseDown, l);
  shell.addListener(SWT.MouseMove, l);
  shell.addListener(SWT.Paint, l);

  final Button button = new Button(shell, SWT.NONE);
  button.addSelectionListener(new SelectionAdapter() {
   public void widgetSelected(final SelectionEvent e) {
    shell.dispose();
   }
  });
  button.setText("退出");
  button.setBounds(185, 105, 80, 25);

  final Button button_1 = new Button(shell, SWT.NONE);
  button_1.addSelectionListener(new SelectionAdapter() {
   public void widgetSelected(final SelectionEvent e) {
    shell.setMinimized(true);
    createIrregularlyShell();
    shell.setMinimized(false);
   }
  });
  button_1.setText("切换样式");
  button_1.setBounds(80, 105, 80, 25);

  final Button button_2 = new Button(shell, SWT.NONE);
  button_2.addSelectionListener(new SelectionAdapter() {
   public void widgetSelected(final SelectionEvent e) {
    shell.setMinimized(true);
   }
  });
  button_2.setText("最小化");
  button_2.setBounds(80, 64, 80, 25);

  final Button button_3 = new Button(shell, SWT.NONE);
  button_3.addSelectionListener(new SelectionAdapter() {
   public void widgetSelected(final SelectionEvent e) {
    shell.setMinimized(true);
    createAnnularShell();
    shell.setMinimized(false);
   }
  });
  button_3.setText("环形窗体");
  button_3.setBounds(185, 65, 80, 25);
  //
}

protected void createIrregularlyShell(){
  long l1 = System.currentTimeMillis();//////////////////////////////////////
 
  if (currentImageFlag){
   image = new Image(display, new ImageData("C://TEMP//shell_1.png"));
  }else {
   image = new Image(display, new ImageData("C://TEMP//shell_2.png"));
  }
  currentImageFlag = !currentImageFlag;
  useImage = true;
 
  Region region = new Region();
  imageData = image.getImageData();
  if (imageData.alphaData != null) {
   for (int y = 0; y < imageData.height; y++) {
    for (int x = 0; x < imageData.width; x++) {
     if (imageData.getAlpha(x, y) == 255) {
      region.add(imageData.x + x,imageData.y + y,1,1);
     }
    }
   }
  } else {
   ImageData mask = imageData.getTransparencyMask();
   for (int y = 0; y < mask.height; y++) {
    for (int x = 0; x < mask.width; x++) {
     if (mask.getPixel(x, y) != 0) {
      region.add(imageData.x + x,imageData.y + y,1,1);
     }
    }
   }
  }
  shell.setRegion(region);
  shell.setSize(imageData.x + imageData.width, imageData.y
    + imageData.height);
 
  System.out.println("切换Shell样式耗时:" +
    ((System.currentTimeMillis() - l1)/1000D) + "秒");///////////////////////////
}

protected void createAnnularShell(){
  long l1 = System.currentTimeMillis();//////////////////////////////////////
 
  useImage = false;
 
  shell.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
  Region region = new Region();
  region.add(createCircle(200, 200, 200));
  region.subtract(createCircle(80, 200, 200));
  shell.setRegion(region);
  Rectangle regionBounds = region.getBounds();
  shell.setSize(regionBounds.width, regionBounds.height);
 
  System.out.println("制作环形Shell耗时:" +
    ((System.currentTimeMillis() - l1)/1000D) + "秒");///////////////////////////
}

protected int[] createCircle(int radius, int centerX, int centerY) {
  int[] points = new int[720 * 2];
  for (int i = 0; i < 720; i++) {
   int theX = centerX + (int) (radius * Math.cos(Math.toRadians(i/2)));
   int theY = centerY + (int) (radius * Math.sin(Math.toRadians(i/2)));
   points[i * 2] = theX;
   points[i * 2 + 1] = theY;
  }
  return points;
}
}

===========================源码区===============================

我在源码中展示了如何使用不规则图片制作不规则窗体 以及 使用环形制作不规则窗体!

使用说明: 在Eclipse中新建java源文件,将上面的代码粘贴进去, Run As -> SWT Application...

 

原创粉丝点击