The following gives you an idea on how to obtain the Cartesian coordinates on a Jung VisualizationViewer instance...
创建内部班级如下:
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 = new DefaultModalGraphMouse<Object, Object>();
vv.setGraphMouse(graphMouse);
vv.addKeyListener(graphMouse.getModeKeyListener());
graphMouse.add(new MyGraphMousePlugin());
http://www.ohchr.org。
接下来的修改将给你考虑到Jung图表版译文的Cartesian坐标:
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();
}
}
这还不是完美的,因为它忽略了比额表因素,但你会提出想法。
你们需要从屏幕协调系统计算,以便把系统协调到模型协调系统,以获得模型坐标。
上述法典中的通用型号应当改为您自己的版本:
<><>Edited>
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);
}
}
);
}