我正在尝试使用JUNG创建一个GRID布局的图形。 我无法找到,无论如何使用JUNG创建图表
提前致谢
答案 0 :(得分:0)
我不确定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);
}
}