如何在JUNG中获取顶点坐标? [英] How to get a vertex coordinate in JUNG?

查看:102
本文介绍了如何在JUNG中获取顶点坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jung中使用EditingModalGraphMouse时是否有一种获取顶点坐标的方法? 我已经使用坐标设置器和吸气剂为顶点创建了一个类,但是我不知道如何使用其特定的坐标来设置顶点? (我有一个变压器:变压器)

Is there a way to get the vertex coordinate when using the EditingModalGraphMouse in Jung ?? 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)

推荐答案

以下内容为您提供了有关如何在Jung VisualizationViewer实例上获取笛卡尔坐标的想法...

The following gives you an idea on how to obtain the Cartesian coordinates on a Jung VisualizationViewer instance...

创建一个内部类,如下所示:

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

将插件添加到您的GraphMouse实例:

Add the plugin to your GraphMouse instance:

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

下一个修改将为您提供笛卡尔坐标,其中考虑到在Jung图布局上进行的平移:

Next modification will give you the Cartesian coordinates that take into account the translation made upon a Jung graph layout:

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 :)

已编辑

哈哈,线索已经存在,这是正确的方法...无需计算! http://sourceforge.net/projects/jung/forums /forum/252062/topic/3040266?message = 6522779

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

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

这篇关于如何在JUNG中获取顶点坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆