问题
JGraphX的早期版本(1.x)在mxHierarchicalLayout类中提供了属性layoutFromSinks。这是不再可用的,而且我已经看过很长时间了。
布局差异的问题最好在图片中说明:
旧的JGraphX版本,启用了接收器的布局,产生了这个:
较新的JGraph版本(我试过的最新版本是3.4.1.2)产生了这个:
正如您所看到的,旧版本产生了更清晰的结果。
问题
这种分层布局是否也可以在最新版本的JGraphX中使用,您需要应用哪些设置才能实现相同的布局?
代码
以下是产生上述布局的示例代码
import java.awt.BorderLayout;
import javax.swing.JFrame;
import com.mxgraph.layout.mxIGraphLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxICell;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
public class HierarchyLayoutExample extends JFrame {
mxICell a,b,c,d,e,f,g,h;
public HierarchyLayoutExample() {
final mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
a = (mxICell) graph.insertVertex(parent, null, "a", 0, 0, 80, 30);
b = (mxICell) graph.insertVertex(parent, null, "b", 0, 0, 80, 30);
c = (mxICell) graph.insertVertex(parent, null, "c", 0, 0, 80, 30);
d = (mxICell) graph.insertVertex(parent, null, "d", 0, 0, 80, 30);
g = (mxICell) graph.insertVertex(parent, null, "g", 0, 0, 80, 30);
h = (mxICell) graph.insertVertex(parent, null, "h", 0, 0, 80, 30);
e = (mxICell) graph.insertVertex(parent, null, "e", 0, 0, 80, 30);
graph.insertEdge(parent, null, "", a, b);
graph.insertEdge(parent, null, "", a, c);
graph.insertEdge(parent, null, "", c, d);
graph.insertEdge(parent, null, "", e, d);
graph.insertEdge(parent, null, "", g, a);
graph.insertEdge(parent, null, "", b, d);
graph.insertEdge(parent, null, "", h, e);
} finally {
graph.getModel().endUpdate();
}
// define layout
mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
// layout.setLayoutFromSinks(true); // <== not available anymore
layout.execute(graph.getDefaultParent());
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(graphComponent, BorderLayout.CENTER);
}
public static void main(String[] args) {
HierarchyLayoutExample frame = new HierarchyLayoutExample();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(460, 400);
frame.setVisible(true);
}
}