Kfir Ettinger
Kfir Ettinger

Reputation: 632

Translate MouseEvent coordinates to GraphStream's element coordinates

tl;dr how to get closest GraphicElement coordinates given MouseEvent coordinates?

I created a big graph that represents the road map of a certain region using Graphstream and I'm trying to find the closest node to an occuring MouseEvent.

My problem is that the MouseEvent coordinates are relative to the frame resulotion while my GraphicElement's coordinates are longitude and latitude of real location.

For example, when clicking on a Node and get it using MouseManager with the following code:

    @Override
    public void mouseReleased(MouseEvent e) {
        super.mouseReleased(e);
        GraphicElement graphicElement = view.findNodeOrSpriteAt(e.getX(), e.getY());
        if(graphicElement!= null){
            System.out.println( "node "+graphicElement.getLabel()+ ", xy ("+graphicElement.getX() +","+ graphicElement.getY()+").");
            System.out.println( "MouseEvent xy ("+e.getX() +","+e.getY()+").");
        }
    }

the output will be:

node 2, xy (34.7811627,32.0528169).
MouseEvent xy (281,489).

Is there a GraphStream built-in method or someother way I can translate the frame's coordinates to my coordinates?

Upvotes: 1

Views: 75

Answers (1)

Kfir Ettinger
Kfir Ettinger

Reputation: 632

After a lot of reading and debuging while trying to understand how view.findNodeOrSpriteAt(); is working I've found a solution.

The Camera class (view.getCamera( )) have methods to do exactly what I was looking for.
transformGuToPx(double x, double y, double z) -> coordinates to pixels.
transformPxToGu(double x, double y) -> pixels to coordinates.

So, using them I can convert the MouseEvent coordinates:

view.getCamera().transformPxToGu(mouseEvent.getX(), mouseEvent.getY());

Upvotes: 2

Related Questions