不幸的是删除了MouseMotionListener
正如 @trashgod 所建议的那样,这不起作用,因为发生了一些 World Wind 特定行为:删除gov.nasa.worldwind.awt.AWTInputHandler
导致其他MouseMotionListener
s 停止接收事件通知。
禁用地球仪拖动并仍然在另一个中接收事件MouseMotionListener
需要采取以下步骤:
获取 World Wind 的参考AWTInputHandler
:
AWTInputHandler wwHandler = null;
// get World Wind's AWTInputHandler class:
for (MouseMotionListener l : ww.getMouseMotionListeners()) {
if(l instanceof AWTInputHandler) {
wwHandler = (AWTInputHandler)l;
break;
}
}
创建一个MouseMotionListener
它消耗事件:
public class MyMouseMotionListener implements MouseMotionListener {
@Override
public void mouseDragged(MouseEvent e) {
// consume the event so the globe position does not change
e.consume();
if (e.getSource() instanceof WorldWindowGLCanvas) {
// get the position of the mouse
final WorldWindowGLCanvas canvas = ((WorldWindowGLCanvas) e.getSource());
final Position p = canvas.getCurrentPosition();
// do something with the position here
}
}
@Override
public void mouseMoved(MouseEvent e) {
e.consume();
}
}
添加鼠标移动监听器AWTInputHandler
:
if(wwHandler != null) {
wwHandler.addMouseMotionListener(new MyMouseMotionListener());
} else {
// I don't think this should happen unless the AWTInputHandler
// is explicitly removed by client code
logger.error("Couldn't find AWTInputHandler");
}
也就是说,我不知道为什么WorldWindowGLCanvas
正在使用Component.addMouseMotionListener()
而不是AWTInputHandler.addMouseMotionListener()
.