user1640722
user1640722

Reputation: 79

Vertex in Java using JUNG library

I am trying to use the JUNG library to visualise a basic graph with vertices and edges. Using the example code on the website, I have the following:

import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import java.util.*;

public class GraphTest {

    public GraphTest(){        
        Graph g = new DirectedSparseGraph();
        Vertex v1 = (Vertex) g.addVertex(new DirectedSparseVertex());
    Vertex v2 = (Vertex) g.addVertex(new DirectedSparseVertex());
    }

However, "Vertex" is underlined in red and Netbeans is telling me it cannot find symbol. I tried importing what Netbeans suggested but to no avail, and now I believe that Vertex is actually native to Java. I've no idea where I'm going wrong but I think it's an elementary error that is escaping me.

Upvotes: 1

Views: 1824

Answers (2)

GrahamA
GrahamA

Reputation: 5923

Check the return type of addVertex the JavaDoc for DirectedSparseGraph#addVertex suggests that the function returns a boolean not a Vertex Jung 1.XX did have a method that returned an ArchetypeVertex that could be downcast to a Vertex. I think you nay be trying to get an example from version 1 working with version 2 but it's not backwards compatiable and this is causing a build error.

Upvotes: 1

henry
henry

Reputation: 6116

Jung uses generic types, it doesn't define a Vertex type.

What do you plan to put into your graph?

Assuming you want Strings for your vertex and integers for your edges, your code should look something like

Graph<String,Integer> g = new DirectedSparseGraph<String,Intger>();
g.addVertex("foo"); // return type is just a boolean indicating if the vertex was already in graph
g.addVertex("bar");

Which will give you a graph with two unconnected vertices.

Upvotes: 1

Related Questions