看这样一段代码:
1
protected JGraph createGraph() {
2
JGraph graph = new MyGraph(new MyModel());
3
graph.getGraphLayoutCache().setFactory(new DefaultCellViewFactory() {
4![]()
5
// Override Superclass Method to Return Custom EdgeView
6
protected EdgeView createEdgeView(Object cell) {
7![]()
8
// Return Custom EdgeView
9
return new EdgeView(cell) {
10![]()
11
/**
12
* Returns a cell handle for the view.
13
* 这里是返回一个Handle(对这个线条事件的执行器)
14
*/
15
public CellHandle getHandle(GraphContext context) {
16
17
return new MyEdgeHandle(this, context);
18
}
19![]()
20
};
21
}
22
});
23
return graph;
24
}
其中 setFactory是用来设置一个工厂类对象的,而这里的工厂类对象继承了DefaultCellViewFactory,这里覆盖了EdgeView,作用是产生一个自定义的EdgeView,来渲染特定的线形效果。
protected JGraph createGraph() {2
JGraph graph = new MyGraph(new MyModel());3
graph.getGraphLayoutCache().setFactory(new DefaultCellViewFactory() {4

5
// Override Superclass Method to Return Custom EdgeView6
protected EdgeView createEdgeView(Object cell) {7

8
// Return Custom EdgeView9
return new EdgeView(cell) {10

11
/**12
* Returns a cell handle for the view.13
* 这里是返回一个Handle(对这个线条事件的执行器)14
*/15
public CellHandle getHandle(GraphContext context) {16
17
return new MyEdgeHandle(this, context);18
}19

20
};21
}22
});23
return graph;24
}而 其中返回的EdgeView是被覆盖了getHandle方法的类,这个handle是用来处理这个线接受的事件。
看下述的Handle的代码:
public static class MyEdgeHandle extends EdgeView.EdgeHandle {
/**
* @param edge
* @param ctx
*/
public MyEdgeHandle(EdgeView edge, GraphContext ctx) {
super(edge, ctx);
}
//这个方法是用来添加一个方法,判断是否要在线条上添加一个断点,终于试出来了,不爽,看来做事还是要静心下来才能做
// Override Superclass Method
public boolean isAddPointEvent(MouseEvent event) {
// Points are Added using Shift-Click
return event.isShiftDown();
//return false;
}
//这个方法是用来添加一个方法,判断是否要在线条消除一个断点。
// Override Superclass Method
public boolean isRemovePointEvent(MouseEvent event) {
// Points are Removed using Shift-Click
return event.isShiftDown();
}
}


graph.getGraphLayoutCache().setFactory(
浙公网安备 33010602011771号