简体   繁体   English

如何在JUNG中获得顶点坐标?

[英]How to get a vertex coordinate in JUNG?

Is there a way to get the vertex coordinate when using the EditingModalGraphMouse in Jung ?? 在Jung中使用EditingModalGraphMouse时,有没有一种方法可以获取顶点坐标? I've made a class for the vertex with the coordinate setter and getter but i don't kown how to set the vertex with its specific coordinate ? 我已经使用坐标设置器和吸气剂为顶点创建了一个类,但是我不知道如何使用其特定的坐标来设置顶点? (i've user a transformer : Transformer) (我有一个变压器:变压器)

The following gives you an idea on how to obtain the Cartesian coordinates on a Jung VisualizationViewer instance... 以下内容为您提供了有关如何在Jung VisualizationViewer实例上获取笛卡尔坐标的想法...

Create an inner class as follows: 创建一个内部类,如下所示:

protected class MyGraphMousePlugin extends TranslatingGraphMousePlugin implements MouseListener {

    @Override
    public void mouseMoved(MouseEvent e) {
        final VisualizationViewer<GeoLocationData.Station,GeoLocationData.Link> vv =
        (VisualizationViewer<GeoLocationData.Station,GeoLocationData.Link>)e.getSource();
        Point2D p = e.getPoint();//vv.getRenderContext().getBasicTransformer().inverseViewTransform(e.getPoint());
        GraphElementAccessor<GeoLocationData.Station,GeoLocationData.Link> pickSupport = vv.getPickSupport();
        if(pickSupport != null) {               
            vv.setToolTipText ("<html>x: "+p.getX()+"<br>y: "+p.getY());
        }
    }

    public MyGraphMousePlugin(int modifiers) {
        super(modifiers);
        // TODO Auto-generated constructor stub
    }

    public MyGraphMousePlugin() {
        super();
    }
}

Add the plugin to your GraphMouse instance: 将插件添加到您的GraphMouse实例:

graphMouse = new DefaultModalGraphMouse<Object, Object>();
vv.setGraphMouse(graphMouse);
vv.addKeyListener(graphMouse.getModeKeyListener());
graphMouse.add(new MyGraphMousePlugin());

Edited: 编辑:

Next modification will give you the Cartesian coordinates that take into account the translation made upon a Jung graph layout: 接下来的修改将为您提供笛卡尔坐标,其中考虑到在Jung图布局上进行的平移:

protected class MyGraphMousePlugin extends TranslatingGraphMousePlugin implements MouseListener {

    @Override
    public void mouseMoved(final MouseEvent e) {

        SwingUtilities.invokeLater(
                new Runnable() {
                    public void run() {
                        final VisualizationViewer<GeoLocationData.Station,GeoLocationData.Link> vv =
                            (VisualizationViewer<GeoLocationData.Station,GeoLocationData.Link>)e.getSource();
                        Point2D p = e.getPoint();//vv.getRenderContext().getBasicTransformer().inverseViewTransform(e.getPoint());
                        GraphElementAccessor<GeoLocationData.Station,GeoLocationData.Link> pickSupport = vv.getPickSupport();
                        if(pickSupport != null) {

                            AffineTransform lat =
                                vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).getTransform();
                            //AffineTransform vat =
                            //  vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW).getTransform();
                            //AffineTransform at = new AffineTransform();

                            double x = p.getX() - lat.getTranslateX(); //;
                            double y = p.getY() - lat.getTranslateY(); //;

                            vv.setToolTipText ("<html>x: "+x+"<br>y: "+y);
                        }

                    }
                }
        );
    }

    public MyGraphMousePlugin(int modifiers) {
        super(modifiers);
        // TODO Auto-generated constructor stub
    }

    public MyGraphMousePlugin() {
        super();
    }
}

It is still not perfect since it omits the scale factor, but you will get the idea... 由于它省略了比例因子,因此它仍然不是完美的,但是您会明白的。

You need to calculate from the screen coordinate system to the view coordinate system to the model coordinate system to get the model's coordinates. 您需要计算从屏幕坐标系到视图坐标系再到模型坐标系,以获取模型的坐标。

The generic types in the above codes should be changed to your own version :) 上面代码中的泛型类型应更改为您自己的版本:)

Edited 已编辑

Haha, the clue is already there and it is the correct way...no need to calculate! 哈哈,线索已经存在,这是正确的方法……无需计算! http://sourceforge.net/projects/jung/forums/forum/252062/topic/3040266?message=6522779 http://sourceforge.net/projects/jung/forums/forum/252062/topic/3040266?message=6522779

    @Override
    public void mouseMoved(final MouseEvent e) {

        SwingUtilities.invokeLater(
                new Runnable() {
                    public void run() {
                        final VisualizationViewer<GeoLocationData.Station,GeoLocationData.Link> vv =
                            (VisualizationViewer<GeoLocationData.Station,GeoLocationData.Link>)e.getSource();
                        Point2D p = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(e.getPoint());

                        double x = p.getX();
                        double y = p.getY();

                        vv.setToolTipText ("<html>x: "+(int)x+"<br>y: "+(int)y);
                    }
                }
        );
    }

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

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