[英]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.