2011-04-14 87 views
0

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

回答

0

下面为您提供关于如何获得笛卡尔在荣格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()); 

编辑:

下一页修改会给你考虑到在一个荣格图形布局所做的翻译笛卡尔坐标:

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); 
        } 
       } 
     ); 
    } 
+0

感谢很多:) 我与editingGraphMousePlugin试了一下还它完美的作品:) 其实我在想,是有可能得到的蒙山添加editingmodalgraphMouse顶点时的座标(i VE已经设置setter和getter在顶点工厂) – Jihath 2011-04-15 10:24:28

+0

我的目标是有一个包含x和y的graphml文件现在我可以得到它的名称和颜色,但没有找到将x信息提供给setter的方法 – Jihath 2011-04-15 10:32:19

+0

@eee你的实现在工具提示中显示了这些坐标,但是有什么方法可以将顶点的位置返回到其他代码? – CajunLuke 2012-07-27 23:31:19