我在处理JUNG图表上的选择事件时遇到了一些麻烦。我一直在使用GraphMouseListener接口来监听顶点的点击,但现在我需要添加对边缘的支持,而我似乎无法在文档中找到一种方法。
任何指针?
谢谢
答案 0 :(得分:1)
通过将项侦听器添加到顶点和边的拾取状态来解决它:
visualizationViewer.getPickedEdgeState().addItemListener(new EdgePickListener());
答案 1 :(得分:0)
您还可以创建自己的MouseEdgeListenerTranslator:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.visualization.VisualizationViewer;
public class MouseEdgeListenerTranslator<V,E> extends MouseAdapter {
private VisualizationViewer<V,E> vv;
private GraphEdgeMouseListener<E> geml;
public MouseEdgeListenerTranslator(GraphEdgeMouseListener<E> geml, VisualizationViewer<V,E> vv){
this.geml = geml;
this.vv = vv;
}
private E getEdge(Point2D point){
Point2D p = point;
GraphElementAccessor<V,E> pickSupport = vv.getPickSupport();
Layout<V,E> layout = vv.getGraphLayout();
E e = null;
if(pickSupport != null){
e = pickSupport.getEdge(layout, p.getX(), p.getY());
}
return e;
}
public void mouseClicked(MouseEvent me){
E e = getEdge(me.getPoint());
if(e != null){
geml.graphClicked(e, me);
}
}
public void mousePressed(MouseEvent me){
E e = getEdge(me.getPoint());
if(e != null){
geml.graphPressed(e, me);
}
}
public void mouseReleased(MouseEvent me){
E e = getEdge(me.getPoint());
if(e != null){
geml.graphReleased(e, me);
}
}
}
然后在GraphEdgeMouseListener中将它添加到您自己的VisualizationViewer子类中:
import edu.uci.ics.jung.visualization.VisualizationModel;
import edu.uci.ics.jung.visualization.VisualizationViewer;
public class MyVisualizationViewer<V,E> extends VisualizationViewer<V,E> {
public MyVisualizationViewer(VisualizationModel<V,E> visualizationModel, Dimension size){
super(visualizationModel, size);
}
public void addGraphEdgeMouseListener(GraphEdgeMouseListener<E> geml){
addMouseListener(new MouseEdgeListenerTranslator<V,E>(geml, this));
}
}
和
import java.awt.event.MouseEvent;
public interface GraphEdgeMouseListener<E> {
void graphClicked(E e, MouseEvent me);
void graphPressed(E e, MouseEvent me);
void graphReleased(E e, MouseEvent me);
}