如何在 JUNG 中创建网格布局

发布于 2024-09-14 07:45:21 字数 65 浏览 1 评论 0原文

我正在尝试使用 JUNG 创建一个网格布局的图表。 我找不到如何使用 JUNG 来创建图表

提前致谢

I'm trying to create a graph which is GRID layout by using JUNG.
and i couldn't find how, Is there anyway to create a graph by using JUNG

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-09-21 07:45:21

我不确定 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);
}
}

I am not sure whether JUNG directly supports a GridLayout using any of its layout managers. However it is possible to create a grid based layout by adding vertices to a StaticLayout and then changing the position of each vertex according to its coordinates in the grid.

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);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文