简体   繁体   English

使用鼠标事件检索节点/边缘

[英]Retrieving node/edge with mouse events

I'm following the official GraphStream tutorial , and as the title suggest - I'm trying to get node's by clicking on it.我正在关注官方 GraphStream 教程,正如标题所暗示的那样 - 我正在尝试通过单击它来获取节点。

this is my code so far:到目前为止,这是我的代码:

import org.graphstream.graph.*;
import org.graphstream.graph.implementations.*;
    
public static void main(String args[]) {
    Graph graph = new MultiGraph("Tutorial 1");
    graph.setStrict(false);
    graph.setAutoCreate( true );

    graph.addNode("A").setAttribute("xy", 1, 1);
    graph.addNode("B").setAttribute("xy", 5, 5);
    graph.addNode("C").setAttribute("xy", 1, 8);

    graph.addEdge("AB", "A", "B");
    graph.addEdge("BC", "B", "C");
    graph.addEdge("CA", "C", "A");

    Viewer viewer = graph.display();
    viewer.disableAutoLayout();
}

Is there an efficient way to do it?有没有有效的方法来做到这一点?

So here is the solution I have found:所以这是我找到的解决方案:

First I've written a new MouseManager to override the default one, and I've used the function findNodeOrSpriteAt(int x, int y) to "catch" the clicked node:首先,我编写了一个新的MouseManager来覆盖默认的 MouseManager,并且我使用 function findNodeOrSpriteAt(int x, int y)来“捕捉”被点击的节点:

public class CustomMouseManager implements MouseManager {

    protected View view;
    protected GraphicGraph graph;

    @Override
    public void init(GraphicGraph graph, View view) {
        this.graph = graph;
        this.view = view;
        view.addMouseListener(this);
        view.addMouseMotionListener(this);
    }

    @Override
    public void release() {
        view.removeMouseListener(this);
        view.removeMouseMotionListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();

        GraphicElement node = view.findNodeOrSpriteAt(x, y);

        if(node != null){
            System.out.println("Node " + node.getId() + ": (" + x + "," + y + ")");
        }
    }
    // here you should implement the rest of the MouseManager's methods (mouseDragged, mouseReleased, etc.)

After that, I've added the new custom MouseManager to my Viewer with setMouseManager()之后,我使用setMouseManager()将新的自定义 MouseManager 添加到我的Viewer

public static void main(String args[]) {
    Graph graph = new MultiGraph("Tutorial 1");
    graph.setStrict(false);
    graph.setAutoCreate( true );

    graph.addNode("A").setAttribute("xy", 1, 1);
    graph.addNode("B").setAttribute("xy", 5, 5);
    graph.addNode("C").setAttribute("xy", 1, 8);

    graph.addEdge("AB", "A", "B");
    graph.addEdge("BC", "B", "C");
    graph.addEdge("CA", "C", "A");

    Viewer viewer = graph.display();
    viewer.disableAutoLayout();
    viewer.getDefaultView().setMouseManager(new MyMouseManager());
}

this code works for nodes but I'm still not sure what is the correct way to get an edge by clicking on it.此代码适用于节点,但我仍然不确定通过单击获得边缘的正确方法是什么。

However, a naive solution might be to get the mouse-click's coordinates and then iterate the nodes and check if those coordinates are between 2 nodes .然而,一个天真的解决方案可能是获取鼠标点击的坐标,然后迭代节点并检查这些坐标是否在 2 个节点之间

another (faster) solution is - attaching sprites to the edges:另一个(更快的)解决方案是 - 将精灵附加到边缘:

Sprite s1;
s1.attachToEdge("AB");

By doing this, one can retrieve the edge's sprite with the function findNodeOrSpriteAt(int x, int y) that I've used to retrieve nodes.通过这样做,可以使用我用来检索节点的 function findNodeOrSpriteAt(int x, int y)检索边的精灵。

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

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