如何使用 jung 添加菜单来更改图形布局

发布于 2024-10-14 15:48:15 字数 2722 浏览 0 评论 0原文

如何添加 q 菜单栏来更改 Jung 图的布局(即:StaticLayout、SpringLayout 等)?


事实上,这就是我已经拥有的:

JFrame frame = new JFrame("JUNG2 based GraphVisualization Tool");
        // Create a graph 
        SparseMultigraph<MyNode, MyEdge> graph = new SparseMultigraph<MyNode, MyEdge>();
        // We want to give the Nodes a point where to be (for later use)
        //Map<MyNode, Point2D> vertexLocations = new HashMap<MyNode, Point2D>();
        // Also we need a Layout
        Layout<MyNode, MyEdge> layout = new StaticLayout(graph);
        layout.setSize(new Dimension(600, 600));
        // VisualizationViewer to Visualize our nodes and edges
        VisualizationViewer<MyNode, MyEdge> vv = new VisualizationViewer<MyNode, MyEdge>(layout);
        vv.setPreferredSize(new Dimension(650, 650));
        // To show the vertex and EdgeLabels
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
        // Our mouse should be usable in different modes
        EditingModalGraphMouse mouse = new EditingModalGraphMouse(vv.getRenderContext(), MyNodeFactory.getInstance(), MyEdgeFactory.getInstance());
        // Default values for new edges
        MyEdgeFactory.setDefaultCapacity(100.0);
        MyEdgeFactory.setDefaultWeight(5.0);

        // Popupmenu
        PopupNodeEdgeMenuMousePlugin nodeEdgePlugin = new PopupNodeEdgeMenuMousePlugin();
        JPopupMenu nodeMenu = new MyMouseMenus.NodeMenu();
        JPopupMenu edgeMenu = new MyMouseMenus.EdgeMenu(frame);
        nodeEdgePlugin.setNodePopup(nodeMenu);
        nodeEdgePlugin.setEdgePopup(edgeMenu);
        // The already existing popup editing plugin has to be removed
        mouse.remove(mouse.getPopupEditingPlugin());
        // And the new one has to be added
        mouse.add(nodeEdgePlugin);
        // set up the new mouse for the VisualizationViewer
        vv.setGraphMouse(mouse);

        // A JFrame to show all the stuff
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(vv);

        // To change the mouse modes, the tutorial shows a menuBar. Think it would be nice to have a toolbar here!
        JMenuBar menuBar = new JMenuBar();
        JMenu modeMenu = mouse.getModeMenu();
        modeMenu.setText("Mouse Mode");
        modeMenu.setIcon(null);
        modeMenu.setPreferredSize(new Dimension(80,20));
        menuBar.add(modeMenu);
        frame.setJMenuBar(menuBar);
        mouse.setMode(ModalGraphMouse.Mode.EDITING);
        frame.pack();
        frame.setVisible(true);

抱歉,我是 java 新手,所以如果您建议我根据我的代码做什么,那就太好了。谢谢

How can I add q menu bar to change the layout of a Jung graph (ie: StaticLayout, SpringLayout, etc)?


Infact this is what I already have:

JFrame frame = new JFrame("JUNG2 based GraphVisualization Tool");
        // Create a graph 
        SparseMultigraph<MyNode, MyEdge> graph = new SparseMultigraph<MyNode, MyEdge>();
        // We want to give the Nodes a point where to be (for later use)
        //Map<MyNode, Point2D> vertexLocations = new HashMap<MyNode, Point2D>();
        // Also we need a Layout
        Layout<MyNode, MyEdge> layout = new StaticLayout(graph);
        layout.setSize(new Dimension(600, 600));
        // VisualizationViewer to Visualize our nodes and edges
        VisualizationViewer<MyNode, MyEdge> vv = new VisualizationViewer<MyNode, MyEdge>(layout);
        vv.setPreferredSize(new Dimension(650, 650));
        // To show the vertex and EdgeLabels
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
        // Our mouse should be usable in different modes
        EditingModalGraphMouse mouse = new EditingModalGraphMouse(vv.getRenderContext(), MyNodeFactory.getInstance(), MyEdgeFactory.getInstance());
        // Default values for new edges
        MyEdgeFactory.setDefaultCapacity(100.0);
        MyEdgeFactory.setDefaultWeight(5.0);

        // Popupmenu
        PopupNodeEdgeMenuMousePlugin nodeEdgePlugin = new PopupNodeEdgeMenuMousePlugin();
        JPopupMenu nodeMenu = new MyMouseMenus.NodeMenu();
        JPopupMenu edgeMenu = new MyMouseMenus.EdgeMenu(frame);
        nodeEdgePlugin.setNodePopup(nodeMenu);
        nodeEdgePlugin.setEdgePopup(edgeMenu);
        // The already existing popup editing plugin has to be removed
        mouse.remove(mouse.getPopupEditingPlugin());
        // And the new one has to be added
        mouse.add(nodeEdgePlugin);
        // set up the new mouse for the VisualizationViewer
        vv.setGraphMouse(mouse);

        // A JFrame to show all the stuff
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(vv);

        // To change the mouse modes, the tutorial shows a menuBar. Think it would be nice to have a toolbar here!
        JMenuBar menuBar = new JMenuBar();
        JMenu modeMenu = mouse.getModeMenu();
        modeMenu.setText("Mouse Mode");
        modeMenu.setIcon(null);
        modeMenu.setPreferredSize(new Dimension(80,20));
        menuBar.add(modeMenu);
        frame.setJMenuBar(menuBar);
        mouse.setMode(ModalGraphMouse.Mode.EDITING);
        frame.pack();
        frame.setVisible(true);

Sorry I am new to java so it would be great if you suggest me what to do according to my code.Thanks

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

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

发布评论

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

评论(1

枫以 2024-10-21 15:48:16

事实上,你有一张荣格图并不会真正改变你需要做的事情。
1. 创建一个 JMenuBar 并将其附加到您需要在其上添加菜单的 JFrame。
2. 添加绑定有 Actions 或 ActionListeners 的 JMenuItems。
3. 在 GUI 中创建一个或多个方法来更改带有图形的容器的布局。 (可能需要添加/删除组件,或者可能只是完全重建 GUI 的该部分)。
4. 让 ActionListener 调用适当的方法。

The fact that you have a Jung graph doesn't really change what you need to do.
1. Create a JMenuBar and attach it to the JFrame you need to menu on.
2. Add JMenuItems that either have Actions or ActionListeners tied to them.
3. Create a method or methods in your GUI that change the layout of the container with the Graph. (Might have to add/remove components or possibly just rebuild that part of the GUI completely).
4. Have the ActionListener call the appropriate method.

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