2011-12-07 102 views
2

我在處理JUNG圖上的挑選事件時遇到了一些麻煩。我一直在使用GraphMouseListener接口來監聽頂點的點擊,但是現在我需要添加對拾取邊的支持,而且我似乎無法找到在文檔上執行的方法。JUNG-邊緣採摘(鼠標事件)

任何指針?

謝謝

+0

參考'GraphEditorDemo.java'例或其它實施例中如在我的訊息[http://stackoverflow.com/questions/8226624/how-do-i-use-jung2提到-in-a-mvc-gui](http://stackoverflow.com/questions/8226624/how-do-i-use-jung2-in-a-mvc-gui) –

+0

感謝您的回覆。我怎樣才能覆蓋點擊邊緣?我沒有找到任何示例顯示如何做到這一點。 – pnsilva

+0

即使在GraphEditorDemo.java中,也有一些拾取邊緣的例子,但您需要將鼠標模式從鼠標模式列表更改爲拾取模式。但是,如果你的願望是有一個採摘行爲ala Photoshop,那麼它不像一個。它只是讓你選擇一個邊緣,並聽取它的事件。 –

回答

1

通過添加項偵聽到頂點和邊的拍攝狀態解決它:

visualizationViewer.getPickedEdgeState().addItemListener(new EdgePickListener()); 
+1

是'EdgePickListener'你創建的類嗎? – Ankit

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); 

}