# [Jung] Finding Vertices on a Graph in Jung

5 February, 2010 8 Comments

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.

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

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.

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

getPickedVertexStateon theVisisualizationViewer(vvin my examples). This will give you aPickedStateobject where you can callpickto make the vertex selected.For example, in my example, I could make vertex 1 selected by adding the following line:

Change

truetofalseif the vertex should not be selected.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:-

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

Hi,

You should be checking whether the vertex exists before you set its picked state in the graph. For some reason,

setPickeddoes not perform this check for you. Something like thisLines 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

Graphyou can call itscontainsVertexmethod to find out if the vertex exists within theGraph. If not, you can get theGraphfrom theVisualizationViewwith the following:Thanks a lot, it worked perfectly :D:D

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..

Hi,

Use BasicVisualizationServer‘s pickedEdgeState or pickedVertexState. They will provide a PickedState object. Call getPicked on them to get the selected edges or vertices. Following the above examples, in my post: