`
- 浏览:
599884 次
- 性别:
- 来自:
厦门
-
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)
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
在SWT中,自定义控件通常继承自`org.eclipse.swt.widgets.Control`类或其子类,如`Composite`。`Control`类是所有SWT可视对象的基类,而`Composite`则可以作为一个容器,包含其他控件。 ### 3. 绘制控件 自定义控件...
在SWT(Standard Widget Toolkit)中,2D绘图主要依赖于Graphics Context(GC)以及相关的绘图操作。本文将详细介绍如何使用SWT进行2D绘图,包括在图像和控件上绘制图形,以及利用剪切(Clipping)功能实现特定的...
10. **绘图与图像处理**:SWT的`GC`类提供低级别的绘图功能,包括绘制线条、形状、文本和位图,而`Image`类则用于加载和显示图像。 综上所述,SWT是一个强大的GUI工具包,结合JFace和其他Eclipse相关组件,为Java...
Canvas提供了直接在窗口上绘制的机会,开发者可以利用Java的Graphics2D API或者SWT的GC对象进行低级别的绘图操作。字符雨的动画效果可能通过定时器来实现,每隔一定时间间隔更新屏幕上的字符位置。 JFace的Dialog类...
- 图形绘制:利用GC对象在Canvas组件上进行自定义绘图。 8. SWT的最佳实践 - 模块化设计:利用RCP的插件体系,将功能划分为独立模块。 - 性能优化:避免不必要的组件刷新,合理使用布局管理器。 - 错误处理:...
SWT的`GC`(Graphics Context)类允许开发者进行复杂的图形绘制,包括直线、曲线、文本、图像等。`Image`类则用于加载和显示图像,可以支持各种常见的图片格式。这些特性使得SWT可以用于创建图表、绘图工具等视觉...
这通常通过`PaintEvent`的`gc`(Graphics Context)对象完成,允许在`Canvas`上进行绘图。 5. **保存和分享**:用户完成编辑后,需要将最终图像保存到本地,可以使用`ImageData`从`Image`导出数据,然后通过`...
SWT支持低级的图形和绘图操作,如绘制线条、曲线、填充区域等,这些功能由`GC`类提供。开发者可以通过`Canvas`组件创建自定义的画布,并在其上进行图形绘制。 **5. 实用工具类** SWT API中还包括许多实用工具类,...
- 图形绘制(GC):可以通过Graphics Context(GC)进行自定义绘图,实现复杂的图形效果。 - 文件对话框(FileDialog):提供打开、保存等文件操作的原生对话框。 - 表格(Table)和树(Tree)控件:支持多列、...
`Canvas`类是SWT中的画布,可以使用其`gc`对象进行绘图操作。在俄罗斯方块中,每个方块的绘制都涉及到`gc.drawImage()`方法,通过调整坐标和尺寸来移动和旋转方块。为了实现平滑的动画效果,通常需要在每次更新之间...
7. **绘图和图像**: 使用GC(Graphics Context)进行绘图操作,加载和显示图像。 8. **国际化和本地化**: 实现SWT应用程序的多语言支持。 通过上述学习,开发者可以熟练掌握SWT,创建出高效、美观且符合用户习惯的...
5. **绘图和定制组件**:SWT允许开发者进行低级的像素级绘图,以及创建自定义的组件。这部分将涵盖Canvas类的使用,以及如何利用GC(Graphics Context)对象进行绘制。 6. **数据绑定**:SWT支持数据绑定,使得UI...
`org.eclipse.swt.graphics.GC`类封装了一系列的API,允许开发者进行线条绘制、图形绘制、文本渲染、图像处理以及填充等基本的图形操作。GC对象是与特定的Drawable对象(如控件或图像)关联的,它可以理解为一个描绘...
5. 图形绘制:可能涉及使用GC类进行基本的绘图操作,如线条、形状和文本。 6. 数据绑定:可能讲解如何使用SWT/JFace的数据绑定机制来实现视图和模型之间的同步。 7. 菜单和工具栏:创建和管理菜单、子菜单以及工具栏...
- `PaintEvent`对象包含了绘制时的重要信息,如`gc`(绘图上下文)、`x`、`y`等。 - `x`、`y`坐标代表了重绘区域的左上角位置。 ### 二、键盘事件响应 **概念理解**: - **事件处理**:为了使控件具备更好的交互性,...
GC类是图形上下文的核心,通过它我们可以执行几乎所有的绘图操作。 #### 三、在图像上绘图 在图像上绘图涉及到使用GC对象对Image对象进行操作。下面是一个简单的示例,演示如何在图像上绘制两条线: ```java ...
SWT提供了强大的绘图能力,通过`GC`(Graphics Context)对象,开发者可以直接在窗口或控件上绘制自定义图形。这包括直线、曲线、文字、图像等,为创建富有创意的界面提供了可能。 ### 7. 文件对话框与资源管理 ...
`org.eclipse.swt.graphics` 包提供了图形绘制功能,如 Color 和 GC(Graphics Context),用于在窗口上进行绘图操作。`org.eclipse.swt.events` 包含了各种事件及监听器的定义,比如点击事件、键盘事件等,用于响应...
`addPaintListener()` 方法用于监听控件的绘画事件,`PaintEvent` 包含了画笔对象 `gc`,通过它可以进行绘图操作。`dispose()` 方法用于释放资源,防止内存泄漏。 `CountNumbers.java` 示例没有给出具体代码,但...