簡體   English   中英

如何在JUNG中獲得頂點坐標?

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

在Jung中使用EditingModalGraphMouse時,有沒有一種方法可以獲取頂點坐標? 我已經使用坐標設置器和吸氣劑為頂點創建了一個類,但是我不知道如何使用其特定的坐標來設置頂點? (我有一個變壓器:變壓器)

以下內容為您提供了有關如何在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);
                    }
                }
        );
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM