java - 将方法参数更改为对象时出错(jgrapht 库)

标签 java processing jgrapht digraphs

我正在尝试修改 Jgrapht 的函数以将点的几个坐标(int,int)作为参数。我创建一个类和一个由 (x,y) 定义的 Point 对象,并将其作为有向图的参数

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

我想做的是能够使用:

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

当我运行代码时,即使参数是由 (int, int 定义) 定义的 Point,我也会收到错误“addVertex 方法不适用于参数 (int,int)”整数)。 我应该如何继续这项工作?

我使用基于Java的Processing

最佳答案

此类问题的最佳答案是查看 the API .

addVertex() 函数采用您在通用参数中指定的 Point 参数。您不能只替换两个 int 值并期望它能够工作。相反,您必须提供一个 Point 值:

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

同样,您也必须将 Point 值传递给 DijkstraShortestPath.findPathBetween() 函数:

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

关于java - 将方法参数更改为对象时出错(jgrapht 库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31629337/

相关文章:

java - 如何显示水平 Recyclerview - 显示水平项目时遇到问题

java - 处理 - ScreenToWorld 坐标函数?

java - 如何在图中表示双向边

java - 将森林或分区映射到分割图像上

java - 添加两条边 - JGraphT

java - 将 Object[] 数组转换为 java 中的 int [] 数组?

Java-图形-导入背景图像

java - 从文件 BufferedReader 获取文本,输出看起来不错,但不等于 "true"或 "false"(文件中的内容)

javascript - 处理可汗学院 vs 处理 2

java - 计算处理中沿线的旋转角度