package sourcecode;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
/**
* @author lejuo
*
*/
public class MainWindows {
protected Shell shell;
protected Display display = Display.getDefault();
protected Image image = null;
protected ImageData imageData = null;
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
MainWindows window = new MainWindows();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window
*/
public void open() {
display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
/**
* @author:lejuo
* @date&time:Aug 15, 2007 9:09:55 AM
* @Instruction:
*/
private void loadImageToShell(){
//加载图片
image = ImageLoad.getInstance().getImage(display);
//region代表区域,以下代码要求构造区域
Region region = new Region();
imageData = image.getImageData();
//区分image是α还是β像素
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);
}
/**
* Create contents of the window
*/
/**
* @author:lejuo
* @date&time:Aug 15, 2007 9:10:34 AM
* @Instruction:
*/
protected void createContents() {
shell = new Shell(display, SWT.NO_TRIM);//没有边框造型
shell.setSize(500, 375);
shell.setText("演示程序");
loadImageToShell();
Listener listen = new Listener() {
int startX, startY;
public void handleEvent(Event e) {
//添加esc按钮事件
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) {
//将不规则图片设为窗体背景,这样就完成了不规则窗体的绘制
e.gc.drawImage(image, imageData.x, imageData.y);
}
}
};
shell.addListener(SWT.KeyDown, listen);
shell.addListener(SWT.MouseDown, listen);
shell.addListener(SWT.MouseMove, listen);
shell.addListener(SWT.Paint, listen);
}
}