GEF:使用Draw2D画流程图-(上)

简介:

GEF(Graphical Editing Framework)介绍中已经对Draw2D进行了一些概要介绍,本篇从一个流程图的编写来学习Draw2D的是GEF的基础。

练习要求

做一个图下图所示流程图,流程图中的各个图例可以移动,每个不同类型的图例也不一样。 源码下载:flowchart-Draw2D.zip

基础概念

 

 

图例Figure

这里支持三种图例,图例从ActivityFigure继承下来。主要就是画图还有定义连接点FixedAnchor,下面先看看代码,代码都比较简单

  • 开始、结束图例
    01 public class TerminatorFigure extends ActivityFigure {
    02     FixedAnchor inAnchor, outAnchor;
    03   
    04     public TerminatorFigure() {
    05         inAnchor = new FixedAnchor(this);
    06         inAnchor.place = new Point(10);
    07         targetAnchors.put("in_term", inAnchor);
    08         outAnchor = new FixedAnchor(this);
    09         outAnchor.place = new Point(12);
    10         sourceAnchors.put("out_term", outAnchor);
    11     }
    12   
    13     public void paintFigure(Graphics g) {
    14         Rectangle r = bounds;
    15         g.drawArc(r.x + r.width / 8, r.y, r.width / 4, r.height - 190180);
    16         g.drawLine(r.x + r.width / 4, r.y, r.x + 3 * r.width / 4, r.y);
    17         g.drawLine(r.x + r.width / 4, r.y + r.height - 1,
    18                 r.x + 3 * r.width / 4, r.y + r.height - 1);
    19         g.drawArc(r.x + 5 * r.width / 8, r.y, r.width / 4, r.height - 1270,
    20                 180);
    21         g.drawText(message, r.x + 3 * r.width / 8, r.y + r.height / 8);
    22     }
    23 }
  • 分支图例
    01 public class DecisionFigure extends ActivityFigure {
    02     FixedAnchor inAnchor, yesAnchor, noAnchor;
    03   
    04     public DecisionFigure() {
    05         inAnchor = new FixedAnchor(this);
    06         inAnchor.place = new Point(10);
    07         targetAnchors.put("in_dec", inAnchor);
    08         noAnchor = new FixedAnchor(this);
    09         noAnchor.place = new Point(21);
    10         sourceAnchors.put("no", noAnchor);
    11         yesAnchor = new FixedAnchor(this);
    12         yesAnchor.place = new Point(12);
    13         sourceAnchors.put("yes", yesAnchor);
    14     }
    15   
    16     public void paintFigure(Graphics g) {
    17         Rectangle r = bounds;
    18         PointList pl = new PointList(4);
    19         pl.addPoint(r.x + r.width / 2, r.y);
    20         pl.addPoint(r.x, r.y + r.height / 2);
    21         pl.addPoint(r.x + r.width / 2, r.y + r.height - 1);
    22         pl.addPoint(r.x + r.width, r.y + r.height / 2);
    23         g.drawPolygon(pl);
    24         g.drawText(message, r.x + r.width / 4 5, r.y + 3 * r.height / 8);
    25         g.drawText("N", r.x + 7 * r.width / 8, r.y + 3 * r.height / 8);
    26         g.drawText("Y", r.x + r.width / 2 2, r.y + 3 * r.height / 4);
    27     }
    28 }
  • 流程图例
    01 public class ProcessFigure extends ActivityFigure {
    02     FixedAnchor inAnchor, outAnchor;
    03   
    04     public ProcessFigure() {
    05         inAnchor = new FixedAnchor(this);
    06         inAnchor.place = new Point(10);
    07         targetAnchors.put("in_proc", inAnchor);
    08         outAnchor = new FixedAnchor(this);
    09         outAnchor.place = new Point(12);
    10         sourceAnchors.put("out_proc", outAnchor);
    11     }
    12   
    13     public void paintFigure(Graphics g) {
    14         Rectangle r = bounds;
    15         g.drawText(message, r.x + r.width / 4, r.y + r.height / 4);
    16         g.drawRectangle(r.x, r.y, r.width - 1, r.height - 1);
    17     }
    18 }
  • FixedAnchor:连接画线时会根据place来调用getLocation确定连接终点的位置
    01 public class FixedAnchor extends AbstractConnectionAnchor
    02 {
    03   Point place;
    04   public FixedAnchor(IFigure owner)
    05   {
    06     super(owner);
    07   }
    08     
    09   public Point getLocation(Point loc)
    10   {
    11     Rectangle r = getOwner().getBounds();
    12     int x = r.x + place.x * r.width/2;
    13     int y = r.y + place.y * r.height/2;
    14     Point p = new PrecisionPoint(x,y);
    15     getOwner().translateToAbsolute(p);
    16     return p;
    17   }
    18 }
  • ActivityFigure:主要处理连接点的代码
     


 本文转自 陈本峰 51CTO博客,原文链接:http://blog.51cto.com/zhoujg/516874 ,如需转载请自行联系原作者


相关文章
|
存储 JSON NoSQL
【MongoDB】MongoDB的数据存储格式
【4月更文挑战第1天】【MongoDB】MongoDB的数据存储格式
|
人工智能 JSON 数据可视化
yolov8+动物+姿态识别(训练教程+代码)
yolov8+动物+姿态识别(训练教程+代码)
|
Android开发 开发者
 一键在线获取APP公钥、包名、签名及备案信息方法介绍
本文介绍了一款在线APP解析工具,可以一键获取APP的公钥、包名、签名等基础信息,同时提供了详细的操作步骤和使用示例,帮助开发者更便捷地进行APP备案信息的获取。
|
机器学习/深度学习 分布式计算 并行计算
推荐一些机器学习系统MLSys中的值得研究的方向
MLsys不能算是一种方向,而是一种思路。比如对于system研究者来说,可以把ML作为我们开发的系统要适配的一种benchmark,就像transaction对于数据库、某种文件场景对于File System的意义一样。这样一想可做的空间就宽广多了
1388 0
|
JSON fastjson Java
java fastJson 转JSON 两个转义
【2月更文挑战第14天】
1384 2
|
XML Java 应用服务中间件
如何断点调试Tomcat源码
如何断点调试Tomcat源码 Tomcat作为一个老牌的一个Web容器框架,用途十分的广泛。无论是为了学习其框架的整体设计还是为了碰到问题更好的解决,作为程序员我们都应该对于Tomcat有一定的了解。
1287 0
|
域名解析 缓存 开发框架
HTTP报错状态码详解
HTTP报错状态码详解
1365 0
|
程序员 架构师 开发者
代码平台Github半年发布125项更新,私库无限免费开启(附步骤)
Github 调整了两项政策(开放程序越来越大啦!):** **1、用户可免费创建无限空间私有代码库(GitHub Free)** 从官宣之日起,程序员们可以免费使用 Github 私有仓库,但是每个私库最多有 3 个免费协作者,如果需要加入多名协作者,每月支付7刀~ **2、打通企业产品云仓库与本地化仓库(GitHub Enterprise)** 企业可以通过 GitHub Connect 连接,打通云仓库、本地化仓库,功能更加灵活、强大有么有。
2201 0
|
物联网 应用服务中间件 nginx
Nginx转发多个二级域名及前后端分离
一、直接上代码(以下配置方案为亲在广州某物联网公司做架构时的配置) /etc/nginx/conf.d/ssl.conf 配置文件: 其中sink-node1与web-node1为内部服务器的hostname。
5052 0

热门文章

最新文章