Search code examples
javaprocessingjgraphtdigraphs

Error when changing method parameter to an object (jgrapht library)


I'm trying to modify the Jgrapht's functions to take as parameters a point's couple of coordinates (int, int). I create a class and a Point object defined by (x,y) and put it as the directedGraph's parameter

    public class Point {

      public int x;
      public int y;

      public Point(int x, int y) 
      {
        this.x = x;
        this.y = y;
      }
      @Override
        public String toString() {
        return ("[x="+x+" y="+y+"]");
      }
    }
  // --------------------------------------------------------------

    public class DirectedGraphDemo {
      public  void graph(String args[]) {

        //Create the directed graph
        public DirectedGraph<Point, DefaultEdge> directedGraph = new DefaultDirectedGraph<Point, DefaultEdge>(DefaultEdge.class);

        // constructs a directed graph with the specified vertices and edges

        directedGraph.addVertex(1,2);
        directedGraph.addVertex(1,3);
        directedGraph.addVertex(1,4);

        directedGraph.addEdge((1,2),(1,3));
        directedGraph.addEdge((1,3),(1,4));
        directedGraph.addEdge((1,4),(1,2));

        // computes all the strongly connected components of the directed graph
        StrongConnectivityInspector sci =
          new StrongConnectivityInspector(directedGraph);
        List stronglyConnectedSubgraphs = sci.stronglyConnectedSubgraphs();

        // prints the strongly connected components
        System.out.println("Strongly connected components:");
        for (int i = 0; i < stronglyConnectedSubgraphs.size (); i++) {
          System.out.println(stronglyConnectedSubgraphs.get(i));
        }
        System.out.println();

        // Prints the shortest path from vertex i to vertex c. This certainly
        // exists for our particular directed graph.
        System.out.println("Shortest path from (1,2) to (1,3):");
        List path =
          DijkstraShortestPath.findPathBetween(directedGraph, (1,2),(1,3);
        System.out.println(path + "\n");

        // Prints the shortest path from vertex c to vertex i. This path does
        // NOT exist for our particular directed graph. Hence the path is
        // empty and the variable "path"; must be null.
        System.out.println("Shortest path from (1,2) to (1,3):");
        path = DijkstraShortestPath.findPathBetween(directedGraph, (1,2), (1,3));
        System.out.println(path);
      }
    }

What I'm trying to do is to be able to use:

directedGraph.addVertex(x1,y1);
directedGraph.addVertex(x2,y2);
directedGraph.addEdge((x1,y1), (x2,y2));
path = DijkstraShortestPath.findPathBetween(directedGraph,(x1,y1),(x2,y2));

When I run the code, I get the error "the method addVertex is not applicable for the argument (int,int)" even when the parameter is Point, which is defined by (int,int). How should I proceed to make this work ?

I use Processing which is based on Java


Solution

  • Questions like these are best answered by looking at the API.

    The addVertex() function takes a Point parameter, as you specified in your generic arguments. You can't just substitute in two int values and expect it to work. Instead, you have to supply a Point value:

    directedGraph.addVertex(new Point(1,2));
    

    Similarly, you have to pass in Point values to the DijkstraShortestPath.findPathBetween() function as well:

    path = DijkstraShortestPath.findPathBetween(directedGraph, new Point(x1,y1),new Point(x2,y2));