3

我已经用 JUNG 构建了图形,但我不确定如何将鼠标动作侦听器添加到图形的顶点。

下面是我认为与问题最相关的代码示例部分。

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);

特别是,我想检测对图形顶点的单击和双击并对其进行操作。

注意:我看过几个旧的 SO 链接,但似乎没有一个答案对我有帮助。如果有人可以给我伪代码或指出正确的方向,那将非常有帮助。

4

1 回答 1

5

我认为一个完整的示例将比仅仅修改您的代码更好地为您和其他人服务。这里的相关部分是,假设你已经有一个VisualizationViewer, 来使用这个片段:

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

除了处理点击事件外,这还允许您添加新闻和发布事件。

这个独立的类创建了一个简单的图形——当一个顶点被点击时——在标准输出上打印哪个顶点被点击了。

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);
    }
}
于 2014-02-26T22:30:32.843 回答