user1047271
user1047271

Reputation: 431

JGraphX: How can I get a vertex by mouse coordinates? (mouseMoved method)

I have a mouseMoved(MouseEvent e) method with the coordinates e.getX() and e.getY(). Now I want to check if the mouse is over a vertex. Is there a way to do this?

I don't want to check if the cell (vertex) is selected, I only want to check if the mouse is over one vertex.

mGraph = new mxGraph();

// create vertexes ...

mGraphComponent = new mxGraphComponent(mGraph);

//mGraphComponent.getGraphControl().addMouseMotionListener(new MouseAdapter() {
mGraphComponent.getGraphControl().addMouseMotionListener(new mxMouseAdapter() {
    @Override
    public void mouseMoved(MouseEvent e)
    {
        System.out.println(Integer.toString(e.getX()) + " " +
            Integer.toString(e.getY()));

        // here I want to check if the mouse position is over a cell
        // I only want to check if the mouse is over one (or more?) cells
    }
}
);

mPanel.add(mGraphComponent);

Upvotes: 3

Views: 1891

Answers (1)

Xavier M.
Xavier M.

Reputation: 136

You can do this like so:

Object cell = mGraphComponent.getCellAt(e.getX(), e.getY(), false);

cell should be a mxCell and then you can use model.isVertex() or model.isEdge().

Upvotes: 2

Related Questions