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

时间:2011-04-14 13:20:05

标签: java layout jung

在Jung中使用EditingModalGraphMouse时,有没有办法获取顶点坐标? 我用坐标setter和getter为顶点创建了一个类,但是我不知道如何用它的特定坐标设置顶点? (我用过变压器:变压器)

1 个答案:

答案 0 :(得分:0)

以下介绍如何在Jung VisualizationViewer实例上获取笛卡尔坐标...

按如下方式创建内部类:

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实例:

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

<强>编辑:

下一个修改将为您提供笛卡尔坐标,其中考虑了在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();
    }
}

它仍然不完美,因为它省略了比例因子,但你会得到这个想法......

您需要从屏幕坐标系计算到视图坐标系到模型坐标系,以获得模型的坐标。

上述代码中的泛型类型应更改为您自己的版本:)

<强>被修改

哈哈,线索已经存在,这是正确的方法......无需计算! 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);
                    }
                }
        );
    }