Topic: 关于SWT的画图程序的问题?

  Print this page

1.关于SWT的画图程序的问题? Copy to clipboard
Posted by: howjay
Posted on: 2005-03-02 22:23

不是程序使用 swt 绘制静态图,是想做一个画图程序。网上我找到了是使用 awt/swing 的例子。

我尝试了一下实现鼠标画线的功能,使用 Canvas 类型作为画布。有 2 个问题:

1。我使用 canvas.addMouseListener() 和 canvas.addMouseMoveListener(),在 mouseMove() 函数里面画线的话,resize 窗口图像会消失。我知道在 canvas.addPaintListener() 函数添加的画图代码在 resize 窗口时图像不会消失,那我应该怎么做呢?

2。下载下来的例子中是在 mouseDragged() 中实现画图功能,随鼠标动画出的线很连续,为什么我在 mouseMove() 中使用 drawPoint() 或者 drawLine() 画出的线都断断续续的,鼠标移动越快效果越不好?

我网上看到的资料都没有关于用户动态画图这一块的。若有这方面的资料还请指点,多谢~

2.Re:关于SWT的画图程序的问题? [Re: howjay] Copy to clipboard
Posted by: cnfree
Posted on: 2005-03-02 23:03

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/*
* Created on 2005-3-2
*/

/**
* @author cnfree
*/
public class DrawLine {
private int x = -1,y = -1;
private GC gc = null;

private MouseTrackListener trackListener = new MouseTrackListener(){
public void mouseEnter(MouseEvent e) {
if(gc==null || gc.isDisposed()){
gc = new GC(canvas);
}
}
public void mouseExit(MouseEvent e) {
if(!gc.isDisposed())gc.dispose();
}
public void mouseHover(MouseEvent e) {
}

};

private MouseMoveListener moveListener = new MouseMoveListener(){
public void mouseMove(MouseEvent e) {
if(x<0 || y<0){
gc.drawLine(e.x,e.y,e.x,e.y);
}else{
gc.drawLine(e.x,e.y,x,y);
}
x=e.x;
y=e.y;
}
};

private Display display;
private Canvas canvas;
public DrawLine(){
display = new Display();
Shell shell = new Shell(display, SWT.TITLE | SWT.CLOSE | SWT.RESIZE);
shell.setLayout(new FillLayout());
canvas = new Canvas(shell,SWT.NONE);
canvas.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
canvas.addMouseTrackListener(trackListener);
canvas.addMouseMoveListener(moveListener);
shell.layout(true);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

public static void main(String[] args) {
DrawLine drawLine = new DrawLine();
}
}

3.Re:关于SWT的画图程序的问题? [Re: howjay] Copy to clipboard
Posted by: howjay
Posted on: 2005-03-02 23:45

看出来了,第二个问题的关键在于


if(x<0 || y<0){
gc.drawLine(e.x,e.y,e.x,e.y);
}else{
gc.drawLine(e.x,e.y,x,y);
}
x=e.x;
y=e.y;


我放到我的程序里面就好了。其实我下载的那个画图程序也是从之前的点画到现在的点,我没有看清楚,以为是 drawLine(e.x, e.y, e.x, e.y) 了。

那第一个问题呢,版主的程序里面并没有使用 addPaintListener() 啊,为什么 resize 窗口之后图像还是在的哦?

4.Re:关于SWT的画图程序的问题? [Re: howjay] Copy to clipboard
Posted by: howjay
Posted on: 2005-03-03 11:02

了解了,如果程序里面没有 canvas.addPaintListener() 的话 resize 就没有问题,如果有的话那么只有这个函数里的画图语句才会在 resize 之后仍然有效

5.Re:关于SWT的画图程序的问题? [Re: howjay] Copy to clipboard
Posted by: hrliuhai
Posted on: 2005-03-07 11:45

不知道使用gef会有没有帮助呢?

6.Re:关于SWT的画图程序的问题? [Re: howjay] Copy to clipboard
Posted by: howjay
Posted on: 2005-03-07 22:08

GEF我正在学习,可是基本上都是基于Eclipse的Editor模式的程序

我要做的是Standalone的,GEF的Standalone程序官方说不可能,但是有人做出来了,不过是2。1的,我没有试过

7.Re:关于SWT的画图程序的问题? [Re: howjay] Copy to clipboard
Posted by: cnfree
Posted on: 2005-03-08 10:54

如果为了实现某一个局部功能,需要1M以上的jar包,我宁愿不用


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923