用SWT设计一个动画,Gui线程可以根据后台的事件来显示不同画面。网上看到的例子一般都是以GUI界面作为主线程然后新建一个后台线程来处理数据。但是我需要将后台的程序作为主线程,请问应该怎么做?

解决方案 »

  1.   

    请问怎么用java进行数据采集?
      

  2.   

    把代码贴出来:
    一个Grid类,在运行过程中调用GridGui来显示动画、这个Grid是作为系统中公共资源由其他的类访问和修改的
    Grid类代码如下:
    public class Grid {
    private static Grid grid;
    static int size = examples.garbageCleaner.Map.size;
    private static int mapGrid[][] = new int[size][size];
    //garbage 类型
    final public static int NOGARBAGE = 0;
    final public static int GLASSGARBAGE = 1; 
    final public static int PAPERGARBAGE = 2; public Display display = Display.getDefault();
    private GridGui gridGui = new GridGui(display) ;

    private Grid(){
    for(int i=0;i<size;i++){
    for(int j=0;j<size;j++){
    mapGrid[i][j] = 0;
    }
    }
    new Thread(){
    public void run(){
    gridGui.open();
    }
    }.start();
    return;
    }

    /**
         * Class method to access the Grid instance of the class.
         */
    public static Grid getGridInstance(){
    if(grid == null){
    grid = new Grid();
    return grid;
    }
    return grid;
    }
    public void addGarbage(int x, int y, int garbage){
    gridGui.addGarbage(x, y, garbage);
    }
    }
    ublic class GridGui extends Shell{

    static private GridGui gridGui;
    private Grid grid;
    private Display display;
    GridLayout gridLayout = new GridLayout();
    static int size = examples.garbageCleaner.Map.size;
    final public static int NOGARBAGE = 0;
    final public static int GLASSGARBAGE = 1; 
    final public static int PAPERGARBAGE = 2;
    Image image;


    private Canvas[][] canvas_Array = new Canvas[size][size]; public GridGui(Display display) {
    gridGui = this;
    System.out.println("get grid instance");
    grid = Grid.getGridInstance();
    this.display = display;
    this.setSize(580, 600);
    setText("Garbage Clean");
    gridLayout.horizontalSpacing = 0;
    gridLayout.verticalSpacing = 0;
    gridLayout.numColumns = size;
    gridLayout.marginWidth = 0;
    gridLayout.marginHeight = 0;
    gridLayout.horizontalSpacing = 0;
    setLayout(gridLayout);
    setBackgroundImage(backgroundImage);
    for(int i=0;i<size;i++){
    for(int j=0;j<size;j++){
    canvas_Array[i][j] = new Canvas(this, SWT.NONE);
    canvas_Array[i][j].setSize(28, 28);
    canvas_Array[i][j].setBackgroundImage(backgroundImage);
    canvas_Array[i][j].setRedraw(true);
    canvas_Array[i][j].addPaintListener(new PaintListener(){
    public void paintControl(PaintEvent event) {
    //将图像显示在canvas上
    if(image!=null){
    System.out.println("draw");
    event.gc.drawImage(image, 0, 0);
    }
    }
    });
    }
    }// end for cycle
    layout();
    System.out.println("GridGui constructed");
    }

    public void open(){
    // display = Display.getDefault();
    // open();
    // layout();
    System.out.println("open");
    while (!this.isDisposed()) {
    if (!grid.display.readAndDispatch()){
    grid.display.sleep();
    }
            }
    }

    /**
     * Create contents of the window
     */
    protected void createContents() {
    } public void addGarbage(final int x, final int y, final int garbage){
    if(display != null){
    display.asyncExec(new Runnable() {
            public void run() {
             if(garbage == GLASSGARBAGE){
    image = glassImage;
    canvas_Array[x][y].redraw();
    }else if(garbage == PAPERGARBAGE){
    image = paperImage;
    canvas_Array[x][y].redraw();
    }
            }
    }); 
    }
    }

    protected void checkSubclass() {
    // Disable the check that prevents subclassing of SWT components
    }}
    运行时总是报错,谁能帮我看一下?
    GridGui代码如下: