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

n7taea2i  于 2023-02-15  发布在  Java
关注(0)|答案(2)|浏览(98)

我正在跟踪official GraphStream tutorial,正如标题所示-我试图通过单击它来获得节点。
这是我的代码:

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

有没有有效的方法来做这件事?

4xrmg8kj

4xrmg8kj1#

所以这是我找到的解决方案:
首先,我编写了一个新的MouseManager来覆盖默认值,并使用函数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.)

之后,我使用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());
}

这段代码适用于节点,但我仍然不确定点击它来得到边的正确方法。
然而,一个简单的解决方案可能是获取鼠标单击的坐标,然后迭代节点和check if those coordinates are between 2 nodes
另一个(更快的)解决方案是-将X1 E1 F1 X附加到边缘:

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

通过这样做,可以使用我用来检索节点的函数findNodeOrSpriteAt(int x, int y)检索边的sprite。

bwleehnv

bwleehnv2#

对于节点,只需按照官方文档中的方法实现ViewerListener
https://graphstream-project.org/doc/Tutorials/Graph-Visualisation/
在OS X上,您可能希望使用-Dsun.java2d.uiScale=100%运行,否则坐标可能会出错
对于悬停事件,您需要添加viewer.getDefaultView().enableMouseOptions();

public class Clicks implements ViewerListener {
    protected boolean loop = true;

    public static void main(String args[]) {
        System.setProperty("org.graphstream.ui", "swing");
        new Clicks();
    }
    public Clicks() {
        Graph graph = new SingleGraph("Clicks");
        Viewer viewer = graph.display();

        viewer.setCloseFramePolicy(Viewer.CloseFramePolicy.HIDE_ONLY);

        ViewerPipe fromViewer = viewer.newViewerPipe();
        fromViewer.addViewerListener(this);
        fromViewer.addSink(graph);
    
        while(loop) {
            fromViewer.pump();
        }
    }

    public void viewClosed(String id) {
        loop = false;
    }

    public void buttonPushed(String id) {
        System.out.println("Button pushed on node "+id);
    }

    public void buttonReleased(String id) {
        System.out.println("Button released on node "+id);
    }

    public void mouseOver(String id) {
        System.out.println("Need the Mouse Options to be activated");
    }

    public void mouseLeft(String id) {
        System.out.println("Need the Mouse Options to be activated");
    }
}

相关问题