Draw2d可以通过图形和连线表示图的关系,本节将用实例介绍如何通过图形和连线绘制UML的关系图
表关系实例中药包苦熬表和列的UML图形(Figure),其中表和列又包含属性和方法子图形。
表和列之间要建立一个以对多的关系连线
package com.heming.table.editor.figure;
import org.eclipse.draw2d.AbstractBorder;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Insets;
/**
* 方法和属性的图形
*
* @author 何明
*
*/
public class CompartmentFigure extends Figure {
public CompartmentFigure() {
ToolbarLayout layout = new ToolbarLayout();
layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
layout.setStretchMinorAxis(false);
// 设置子图形的间距
layout.setSpacing(2);
// 设置布局管理器
setLayoutManager(layout);
// 设置图形的边框
setBorder(new CompartmentFigureBorder());
}
// 图形的边框类
public class CompartmentFigureBorder extends AbstractBorder {
public Insets getInsets(IFigure figure) {
return new Insets(1, 0, 0, 0);
}
// 重画图形的边框
public void paint(IFigure figure, Graphics graphics, Insets insets) {
graphics.drawLine(getPaintRectangle(figure, insets).getTopLeft(),
tempRect.getTopRight());
}
}
}
package com.heming.table.editor.figure;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.swt.graphics.Color;
/**
* UMLClassFigure是UML Class的图形,每个UMLClassFigure包含两个CompartmentFigure
* @author 何明
*
*/
public class UMLClassFigure extends Figure {
public static Color classColor = new Color(null,255,255,206);
//属性图形
private CompartmentFigure attributeFigure = new CompartmentFigure();
//子方法图形
private CompartmentFigure methodFigure = new CompartmentFigure();
public UMLClassFigure(Label name){
ToolbarLayout layout = new ToolbarLayout();
//设置布局管理器
setLayoutManager(layout);
//设置图形的边框
setBorder(new LineBorder(ColorConstants.black,1));
//设置背景色
setBackgroundColor(classColor);
//设置图形是否透明
setOpaque(true);
//添加名字的标签
add(name);
//添加属性的图形
add(attributeFigure);
//添加方法的图形
add(methodFigure);
}
/**
* 得到属性图形
* @return
*/
public CompartmentFigure getAttributesCompartment(){
return attributeFigure;
}
/**
* 得到子方法图形
* @return
*/
public CompartmentFigure getMethodsCompartment(){
return methodFigure;
}
}
package com.heming.table.editor.figure;
import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionEndpointLocator;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* 连线及测试类
*
* @author 何明
*
*/
public class UMLClassFigureTest {
public static void main(String args[]) {
Display d = new Display();
final Shell shell = new Shell(d);
shell.setSize(400, 400);
shell.setText("UMLClassFigure Test");
LightweightSystem lws = new LightweightSystem(shell);
// 新建底层的图形
Figure contents = new Figure();
// 新建底层图的布局方式为XYLaout
XYLayout contentsLayout = new XYLayout();
contents.setLayoutManager(contentsLayout);
Font classFont = new Font(null, "Arial", 12, SWT.BOLD);
// 添加表的现实文本及显示的图标
Label classLabel1 = new Label("Table", new Image(d,
UMLClassFigureTest.class.getResourceAsStream("class_obj.gif")));
classLabel1.setFont(classFont);
Label classLabel2 = new Label("Column", new Image(d,
UMLClassFigureTest.class.getResourceAsStream("class_obj.gif")));
classLabel2.setFont(classFont);
// 新建表和列的图形
final UMLClassFigure classFigure = new UMLClassFigure(classLabel1);
final UMLClassFigure classFigure2 = new UMLClassFigure(classLabel2);
// 添加属性的显示文本及显示图标
Label attribute1 = new Label("columns:Column[]", new Image(d,
UMLClassFigure.class
.getResourceAsStream("field_private_obj.gif")));
Label attribute2 = new Label("rows:Row[]", new Image(d,
UMLClassFigure.class
.getResourceAsStream("field_private_obj.gif")));
Label attribute3 = new Label("columnID:int", new Image(d,
UMLClassFigure.class
.getResourceAsStream("field_private_obj.gif")));
Label attribute4 = new Label("items:List", new Image(d,
UMLClassFigure.class
.getResourceAsStream("field_private_obj.gif")));
// 添加方法和属性的标签
classFigure.getAttributesCompartment().add(attribute1);
classFigure.getAttributesCompartment().add(attribute2);
classFigure2.getAttributesCompartment().add(attribute3);
classFigure2.getAttributesCompartment().add(attribute4);
Label method1 = new Label("getColumns():Column[]", new Image(d,
UMLClassFigure.class.getResourceAsStream("methpub_obj.gif")));
Label method2 = new Label("getRows:Row[]", new Image(d,
UMLClassFigure.class.getResourceAsStream("methpub_obj.gif")));
Label method3 = new Label("getColumnID():int", new Image(d,
UMLClassFigure.class.getResourceAsStream("methpub_obj.gif")));
Label method4 = new Label("getItems():List]", new Image(d,
UMLClassFigure.class.getResourceAsStream("methpub_obj.gif")));
classFigure.getMethodsCompartment().add(method1);
classFigure.getMethodsCompartment().add(method2);
classFigure2.getMethodsCompartment().add(method3);
classFigure2.getMethodsCompartment().add(method4);
contentsLayout
.setConstraint(classFigure, new Rectangle(10, 10, -1, -1));
contentsLayout.setConstraint(classFigure2, new Rectangle(200, 200, -1,
-1));
// 新建连线
PolylineConnection c = new PolylineConnection();
// 添加图形的锚点
ChopboxAnchor sourceAnchor = new ChopboxAnchor(classFigure);
ChopboxAnchor targetAnchor = new ChopboxAnchor(classFigure2);
c.setSourceAnchor(sourceAnchor);
c.setTargetAnchor(targetAnchor);
// 添加连线的装饰器
PolygonDecoration decoration = new PolygonDecoration();
PointList decorationPointList = new PointList();
decorationPointList.addPoint(0, 0);
decorationPointList.addPoint(-2, 2);
decorationPointList.addPoint(-4, 0);
decorationPointList.addPoint(-2, -2);
decoration.setTemplate(decorationPointList);
c.setSourceDecoration(decoration);
// 添加连线的Locator
ConnectionEndpointLocator targetEndpointLocator = new ConnectionEndpointLocator(
c, true);
targetEndpointLocator.setVDistance(15);
Label targetMultiplicityLabel = new Label("1..*");
c.add(targetMultiplicityLabel, targetEndpointLocator);
// 添加连线到Locator
ConnectionEndpointLocator sourceEndpointLocator = new ConnectionEndpointLocator(
c, false);
sourceEndpointLocator.setVDistance(15);
Label sourceMultiplicityLabel = new Label("1");
c.add(sourceMultiplicityLabel, sourceEndpointLocator);
// 添加连线到Locator
ConnectionEndpointLocator relationshipLocator = new ConnectionEndpointLocator(
c, true);
relationshipLocator.setVDistance(10);
relationshipLocator.setVDistance(-20);
Label relationshipLabel = new Label("contains");
c.add(relationshipLabel, relationshipLocator);
// 把表、列和连线(即关系)添加到底层图形上
contents.add(classFigure);
contents.add(classFigure2);
contents.add(c);
lws.setContents(contents);
shell.open();
while (!shell.isDisposed())
while (!d.readAndDispatch())
d.sleep();
}
}
分享到:
相关推荐
eclipse draw2d实例大全 org.eclipse.draw2d.examples.cg org.eclipse.draw2d.examples.connections org.eclipse.draw2d.examples.graph ...org.eclipse.draw2d.examples.uml org.eclipse.draw2d.examples.zoom
GEF/Draw2D入门例子 主要是Draw2d的几个入门的例子和入门的书籍 对eclipse中图形的绘画讲解
网上的Draw2D的参考资料实在是太少了,对于新手来说太不友好了,所以,我总结了一份只有10来页的PPT做为新手的快速入门教程。
NULL 博文链接:https://xmind.iteye.com/blog/819649
GEF Draw2D 使用例子,可以直接运行的
Draw2d js图形库,画各种流程图,非常强大的javascript类图库。
源码实现用gef 和draw2d画类图和连线,代码相对来说简单易懂,易于扩展
如果您想以图形形式描绘将展示的数据,那么Draw2D是一个好工具。可以使用Draw2D编写自己的用来绘制图形的Java代码,这有助于您将精力集中于缩放代码和绘制代码上,把其他与绘制相关的工作留给Draw2D和SWT。您还可以...
draw2d 绘制图形教程 draw2d 绘制图形教程draw2d 绘制图形教程 draw2d 绘制图形教程
Draw2d API Draw2d是一个宿主在SWT Composite控件中的轻量级的构件(widge)系统。一个Draw2d应用程序由一个 SWT Composite控件, 一个轻量级系统, 以及其内容(figures)组成。
eclipse Draw2d Programmer Guide
swt总结draw2d绘图
Draw2D:eclipse Draw2D documents and samples.
GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子GEF的例子
Draw2d教程,网上的,我收集了一下。 写的很详细
Draw2D教程,介绍了Draw2D的基本知识,对学习Draw2D有所帮助
org.eclipse.draw2d_3.1.0.jarorg.eclipse.draw2d_3.1.0.jarorg.eclipse.draw2d_3.1.0.jarorg.eclipse.draw2d_3.1.0.jar
使用Draw2d做的流程图工具
draw2d_Demo_code
eclipse draw2d 示例代码 码很全的。。。。