SWT 绘图(GC类)
1 、点击鼠标左键在shell里画线。
public static void main(String[] args)
... {
Display display = new Display();
final Shell shell = new Shell(display);
Listener listener = new Listener() ...{
int lastX = 0, lastY = 0;
public void handleEvent(Event event)
...{
switch (event.type)
...{
case SWT.MouseMove :
if ((event.stateMask & SWT.BUTTON1) == 0)
break; // 判断是否为鼠标左键,如果不是跳出
GC gc = new GC(shell);
gc.drawLine(lastX, lastY, event.x, event.y);
gc.dispose();
// FALL THROUGH
case SWT.MouseDown :
lastX = event.x;
lastY = event.y;
break;
}
}
};
shell.addListener(SWT.MouseDown, listener);
shell.addListener(SWT.MouseMove, listener);
shell.open();
while (!shell.isDisposed())
...{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
2 、在弹出窗口中显示表的当时图像状态。
public static void main(String[] args) ... {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Widget");
//建立一个简单的表
final Table table = new Table(shell, SWT.MULTI);
table.setLinesVisible(true);
table.setBounds(10, 10, 100, 100);
for (int i = 0; i < 9; i++) ...{
new TableItem(table, SWT.NONE).setText("item" + i);
}
//建立捕捉图像的按钮
Button button = new Button(shell, SWT.PUSH);
button.setText("Capture");
button.pack();
button.setLocation(10, 140);
button.addListener(SWT.Selection, new Listener() ...{
public void handleEvent(Event event) ...{
Point tableSize = table.getSize(); //获取表的大小
GC gc = new GC(table); //建立表的GC对象
final Image image =
new Image(display, tableSize.x, tableSize.y); //建立表大小的图像image
gc.copyArea(image, 0, 0); //利用表的GC对象把表的图像复制到image中
gc.dispose();
//建立一个弹出面板Shell对象popup
Shell popup = new Shell(shell);
popup.setText("Image");
popup.addListener(SWT.Close, new Listener() ...{
public void handleEvent(Event e) ...{
image.dispose();
}
});
//在popup上建立画布对象canvas
Canvas canvas = new Canvas(popup, SWT.NONE);
canvas.setBounds(10, 10, tableSize.x+10, tableSize.y+10);
canvas.addPaintListener(new PaintListener() ...{
public void paintControl(PaintEvent e) ...{
e.gc.drawImage(image, 0, 0); //在画布上绘出表的图像image
}
});
popup.pack();
popup.open();
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) ...{
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
3 、获取整个窗口的图像并显示。
public static void main(String[] args) ... {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Capture");
button.addListener(SWT.Selection, new Listener() ...{
public void handleEvent(Event event) ...{
/**//* Take the screen shot */
GC gc = new GC(display);
final Image image = new Image(display, display.getBounds());
gc.copyArea(image, 0, 0);
gc.dispose();
Shell popup = new Shell(shell, SWT.SHELL_TRIM);
popup.setLayout(new FillLayout());
popup.setText("Image");
popup.setBounds(50, 50, 200, 200);
popup.addListener(SWT.Close, new Listener() ...{
public void handleEvent(Event e) ...{
image.dispose();
}
});
ScrolledComposite sc = new ScrolledComposite (popup, SWT.V_SCROLL | SWT.H_SCROLL);
Canvas canvas = new Canvas(sc, SWT.NONE);
sc.setContent(canvas);
canvas.setBounds(display.getBounds ());
canvas.addPaintListener(new PaintListener() ...{
public void paintControl(PaintEvent e) ...{
e.gc.drawImage(image, 0, 0);
}
});
popup.open();
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) ...{
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
4 、使用transform、alpha和paths混合技术绘图。注意:必须在项目中import“swt - gdip - win32 - 3139 .dll”。
public static void main(String[] args)