简体   繁体   English

JUNG图顶点上的鼠标事件

[英]Mouse events on vertex of JUNG graph

I have constructed graphs with JUNG but I'm not sure how to add mouse action listeners to the vertices of the graph. 我用JUNG构建了图形,但我不确定如何将鼠标动作监听器添加到图形的顶点。

Below is a sample section of my code that I think is the most relevant for the problem. 下面是我认为与问题最相关的代码示例部分。

layout = new FRLayout2<GraphNode, GraphEdge>(graph);
visualizationViewer = new VisualizationViewer<GraphNode, GraphEdge>(layout, new Dimension(1000, 700));

visualizationViewer.getModel().getRelaxer().setSleepTime(500);
visualizationViewer.setGraphMouse(new DefaultModalGraphMouse<GraphNode, String>());
        visualizationViewer.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);
visualizationViewer.setForeground(Color.black);

In particular, I want to detect single and double clicks on the vertices of a graph and act on them. 特别是,我想检测图形顶点的单击和双击并对它们进行操作。

Note: I have seen a couple of old SO links, but none of the answers seem to be helping me. 注意:我看过几个旧的SO链接,但没有一个答案似乎对我有所帮助。 If someone can just give me the pseudocode or point me to the right direction, it would be really helpful. 如果有人可以给我伪代码或指向正确的方向,那将非常有帮助。

I think a full example will serve you and others better than just modifying your code. 我认为一个完整的例子将比为您修改代码更好地为您和其他人服务。 The relevant part here is, assuming you already have a VisualizationViewer , to use this snippet: 假设您已经有VisualizationViewer ,这里的相关部分是使用此代码段:

visualizationViewer.addGraphMouseListener(new GraphMouseListener() {...});

Besides handling click events, this also allows you to add press and release events. 除了处理点击事件外,还允许您添加新闻和发布事件。

This standalone class creates a simple graph and -- when a vertex is clicked on -- prints on stdout what vertex was clicked. 这个独立类创建一个简单的图形 - 当点击顶点时 - 在stdout上打印点击的顶点。

import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph; 
import edu.uci.ics.jung.graph.SparseGraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.GraphMouseListener;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;

public class METest {

    public static void main(String[] args) {
        //Create a test graph
        Graph graph = new SparseGraph<String, String>();
        graph.addVertex("a");
        graph.addVertex("b");
        graph.addVertex("c");
        graph.addVertex("d");
        graph.addEdge("a->b", "a", "b");
        graph.addEdge("a->c", "a", "c");
        graph.addEdge("b->c", "b", "c");
        graph.addEdge("c->d", "c", "d");
        //Metrics
        visualize(graph);
    }

    public static void visualize(Graph graph) {
        //Layout graph.
        Layout layout = new CircleLayout(graph);
        layout.setSize(new Dimension(500, 500)); // sets the initial size of the space
        VisualizationViewer server = new VisualizationViewer(layout);
        server.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        //Here you add the listener
        server.addGraphMouseListener(new GraphMouseListener() {

            @Override
            public void graphClicked(Object v, MouseEvent me) {
                if (me.getButton() == MouseEvent.BUTTON1 && me.getClickCount() == 2) {
                    System.out.println("Double clicked "+ v);
                }
                me.consume();
            }

            @Override
            public void graphPressed(Object v, MouseEvent me) {
            }

            @Override
            public void graphReleased(Object v, MouseEvent me) {
            }
        });
        //Show the frame
        JFrame frame = new JFrame("Simple Graph View");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(server);
        frame.pack();
        frame.setVisible(true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM