[Jung] Finding Vertices on a Graph in Jung

Finding the vertex or edge at a specific point, or the set of vertices in a certain area, requires the graph’s pick support. The support is obtainable from VisualizationViewer‘s getPickSupport method. The support is actually an implementation of GraphElementAccessor and contains the following methods:

    V getVertex(Layout<V,E> layout, double x, double y);

    Collection<V> getVertices(Layout<V,E> layout, Shape rectangle);

    E getEdge(Layout<V,E> layout, double x, double y);

The layout can also be obtained from the VisualizationViewer’s getGraphLayout method.

The “rectangle” argument to getVertices can be a bit misleading, as it can work on any defined Shape. To illustrate this, I wrote a short demostrative application that allows you to place vertices using the mouse. When launched, the application displays a frame containing a box and a circle, that are defined by the following shapes:

    /**
     * Defines the square zone.
     */
    private static final Rectangle square = 
        new Rectangle(100, 100, 100, 100);

    /**
     * Defines the circle zone.
     */
    private static final Ellipse2D circle = 
        new Ellipse2D.Double(125, 125, 50, 50);

Every three seconds, a TimerTask is triggered that outputs the vertices in the square and circle.

    private void showVerticies(Shape area, 
        String type) {

        GraphElementAccessor<V, E> pickSupport = 
            targetViewer.getPickSupport();
        Collection<V> vertices = 
            pickSupport.getVertices(targetViewer
            .getGraphLayout(), area);
    
        System.out.print("Vertices in " + 
            type + ": ");
        for (V vertex : vertices) {
            System.out.print(
                vertex.toString() + " ");
        }
        System.out.println("");
   }

In case you missed the link earlier, you can download the source code for the application here.

Advertisements

8 Responses to [Jung] Finding Vertices on a Graph in Jung

  1. David says:

    Thanks a lot for your information. Very useful :). I would like to ask you a question. I need to select a particular vertex and highlight this vertex only visually. How can I achieve this in Jung?

    Thanks

    • David says:

      Sorry for not explaining exactly how. I need to search for a vertex in the graph. For ex: the vertex “abc”. When found I need to just highlight it automatically for the user, via code not mouse.

      • kahgoh says:

        Hi,

        I assume you want to “highlight” it by “selecting” (or “picking”) the vertex? Assuming you know which vertex you want to pick programatically, you know which vertex you want to highlight, you can call getPickedVertexState on the VisisualizationViewer (vv in my examples). This will give you a PickedState object where you can call pick to make the vertex selected.

        For example, in my example, I could make vertex 1 selected by adding the following line:

        vv.getPickedVertexState().pick(Integer.valueOf(1), true);
        

        Change true to false if the vertex should not be selected.

  2. David says:

    Yes that worked, however I have a problem a small problem. Basically I am creating 4 graphs (vertex and edges as strings) in 4 tabs within a tabbed pane. I need to get the tabbed pane where the vertex is found. So for example I have a vertex ‘Person’ in the graph residing in tab 3. When ‘Person’ is found, I need to get Tab 3 focussed.

    I am doing the following:-

    for(int comp=0;comp&lt;jTabbedPane1.getTabCount();comp++) 
    {
    
    JInternalFrame internalFrame= (JInternalFrame)jTabbedPane1.getComponentAt(comp);
    JRootPane pane=(JRootPane) internalFrame.getComponent(0);
    JPanel container=(JPanel) pane.getContentPane();
    VisualizationViewer viewer=
    (VisualizationViewer )container.getComponent(0);
     
    viewer.getPickedVertexState().pick(getConcept, true);
                
    if(viewer.getPickedVertexState().isPicked(getConcept))
            jTabbedPane1.setSelectedIndex(comp); 
    
    }
    

    However the if statement is always ‘true’. I am surely missing something, or maybe using the wrong method. Thanks a lot for your help

    • kahgoh says:

      Hi,

      You should be checking whether the vertex exists before you set its picked state in the graph. For some reason, setPicked does not perform this check for you. Something like this

      ...
      if (/* ... insert check condition here ... */) {
         viewer.getPickedVertexState().pick(getConcept, true);
      
         // Now you know that vertex was in the current tab value.
         jTabbedPane1.setSelectedIndex(comp);
      
         // break; ??? (if you the vertex is in only one tab / graph)
      }
      ...
      

      Lines 5 to 8 are just additional changes you may consider, but is not really part of the check that you need. As for the check condition, if you have the Graph you can call its containsVertex method to find out if the vertex exists within the Graph. If not, you can get the Graph from the VisualizationView with the following:

      viewer.getModel().getGraphLayout().getGraph()
      
  3. David says:

    Thanks a lot, it worked perfectly :D:D

  4. Hemachandran says:

    Hi,
    If i want to find which vertex has been selected by the user from a graph which is already defined with edges and vertices what should i do.. please do help..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: