我正在尝试使用 JUNG 创建一个网格布局图。我找不到如何使用 JUNG 来创建图表
提前致谢
我不确定 JUNG 是否使用其任何布局管理器直接支持 GridLayout。但是,可以通过将顶点添加到 StaticLayout 然后根据其在网格中的坐标更改每个顶点的位置来创建基于网格的布局。
import edu.uci.ics.jung.algorithms.layout.*;
import edu.uci.ics.jung.graph.*;
import edu.uci.ics.jung.visualization.*;
import edu.uci.ics.jung.visualization.control.*;
import java.awt.*;
import javax.swing.*;
public class JungGridLayout extends JFrame {
Graph graph;
StaticLayout layout;
VisualizationViewer vv;
public static void main(String[] args) {
JungGridLayout g = new JungGridLayout(25,5,5);
}
public JungGridLayout(int numNodes, int numRows, int numColumns) {
graph = new SparseMultigraph();
layout = new StaticLayout(graph);
//distance between the nodes
int distX=100;
int distY=100;
//idea is to add the vertices and change and the position of each vertex to a coordinate in a grid
for (int n=0;n<numNodes;n++) {
graph.addVertex(String.valueOf(n));
}
int operatingNode = 0;
for (int i=0;i<numRows;i++) {
for (int j=0;j<numColumns;j++) {
layout.setLocation(String.valueOf(operatingNode++), i*distX, j*distY);
}
}
createVisualization();
createFrame();
}
public void createFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public void createVisualization() {
vv = new VisualizationViewer(layout, new Dimension(800, 600));
//zooming and transforming
GraphZoomScrollPane zoomPane = new GraphZoomScrollPane(vv);
DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
vv.setGraphMouse(graphMouse);
this.getContentPane().add(zoomPane);
}
}