---
title: "line线"
author: "java4evero"
date: "2011-12-20T16:04:42Z"
tags: []
slug: "line"
draft: false
lastmod: "2011-12-20T16:04:42Z"
id: 1317803
url: "https://java4evero.iteye.com/blog/1317803"
---

1.Connection接口：draw2d里面的线必须实现这个接口，这个接口里面有一个PointList，这个list里面放跟这个线相关的所有ConnectionAnchor。ConnectionAnchor是一个个瞄点，线是必须穿过这些瞄点的。

?

2.Polyline：这个相当于线的实现类，一个Figure，是在Shape的基础上发展出来的。这个类里面控制线的宽度大小形状还有颜色之类的，是对线的外观的定义。尽管Polyline是一个线，但它只是一个线的实现，一般使用的不是它，而是它的子类。

?

3.PolylineConnection是我们经常使用的线，它继承自Polyline同时实现了Connection接口。同时它里面有两个RotatableDecoration对象，用于描述线的两头的形状。默认有两个实现：PolylineDecoration虚心箭头，PolygonDecoration实心箭头，其实就是一个Figure自己想画随便画成啥样。

?

?


```java
public static void createLine(IFigure panel,IFigure node1,IFigure node2){
        PolylineConnection conn = new PolylineConnection(); 
        
        // 设置连线起点的锚点  
        conn.setSourceAnchor(new ChopboxAnchor(node1));  
        // 设置连线目标的锚点  
        conn.setTargetAnchor(new ChopboxAnchor(node2));  
        // 设置连线目标的装饰器，箭头 
        conn.setTargetDecoration(new PolylineDecoration()); 
        
        panel.add(conn);
    }
```

?

?

?

4.在线上面加label：

在线上面加label其实就是在线上面加一个Figure，Figure本身你画什么并不重要。

?





```java
        Label label = new Label("Midpoint");
        label.setOpaque(true);
        label.setBackgroundColor(ColorConstants.buttonLightest);
        label.setBorder(new LineBorder());
        // 添加连线的Locator
        conn.add(label, new MidpointLocator(conn, 0));
```

?
5.<span style="white-space: pre;">Anchor锚点：线两端的点，定位连线的起始和终结点。</span>

?

6.<span style="white-space: pre;">Router路由：有些路由是直接在线上使用的，有些路由必须在父容器中使用。</span>

?





```java
conn.setConnectionRouter(new ManhattanConnectionRouter());
```

?