Draw2d入门系列(一、 Hello World实现)

Draw2d提供了在SWT的Canvas上展现和布局的能力,GEF通过Draw2d实现GEF的视图(GEF的视图和插件的视图时完全不同的概念)。学习Draw2d是为GEF打基础
今天晚上将从如下几个方面介绍Draw2d相关技术的实现
Draw2d简介
图形(Figure)
连线(Connection)
UML关系图实现

Draw2d简介
Draw2d是基于SWT的轻量级组件系统。Draw2d的事例由SWT组件、LightweightSystem和Draw2d的figures组成。
SWT的组件式Draw2d的容器,figures是Draw2d中的图形,LightweightSystem是SWT和Draw2d的桥梁,由于时间关系,不画关系图了,请见谅

IFigure可以是图形,也可以是容器。IFigure对象中能加入其它的IFigure对象,通过IFigre的组合生成Draw2d的图形。创建Draw2d的程序步骤如下:
(1)创建SWT的Canvas组件
(2)添加LightweightSystem实现
(3)添加IFigure实例
(4)吧IFgure实例加入到LightweightSystem中。

下面是Hello World例子的源代码
package com.heming.draw2d.demo;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * 一个简单的Draw2d实例,由Canvas、LightweightSystem和IFigure组成
 * @author 何明
 *
 */
public class HelloWorld {

	/**
	 * 主函数
	 * @param args
	 */
	public static void main(String[] args) {
		
		//新建Shell,Shell是Canvas的子类
		Shell shell = new Shell();
		
		shell.open();
		
		shell.setText("Draw2d Hello World");
		
		//添加LightweihtSystem实例
		LightweightSystem lws = new LightweightSystem(shell);
		
		//添加IFigure实例
		IFigure label = new Label("Hello World");
		
		//把IFigure添加到LightweightSystem中
		lws.setContents(label);
		
		Display display = Display.getDefault();
		
		while(!shell.isDisposed()){
			
			if(!display.readAndDispatch()){
				
				display.sleep();
				
			}
			
		}

	}

}


你可能感兴趣的:(eclipse,UML)