为java应用程序创建图形

时间:2015-02-16 11:42:37

标签: java graph

enter code here我正在构建一个实现A *算法的应用程序来计算两个房间之间的路线。我正在尝试创建一个算法可以操作的图形,我不确定这是否是正确的方法。这是我到目前为止所做的:

Vect2.java:

package myalgorithm;

public class Vect2 {

    private int x;
    private int y;


    public Vect2(int x,int y ){

        this.x = x;
        this.y = y;

        }

} 

Node.java

    package myalgorithm;




    public class Node {

        Node parent;
        Vect2 vector;
        public int x;
        public int y;

        public double f;
        public double g;
        public double h;


        public Node( int x,int y,Node parent, double g,double h){

            this.x = x;
            this.y = y;
            this.parent= parent;
            this.g = g;
            this.h = h;
            this.f= this.g + this.h;

        }
    }

NodeGraph.java




 package myalgorithm;
    import java.util.Arrays;

    public class NodeGraph {

        Node a = new Node(88,623,null,0,0);
        Node b = new Node(727,627,null,0,0);
        Node c = new Node(723,93,null,0,0);
        Node d = new Node(90,92,null,0,0);
        Node e = new Node(94,349,null,0,0);
        Node f = new Node(397,358,null,0,0);
        Node g = new Node(722,339,null,0,0);
        Node[] arr = new Node[7];


    public NodeGraph init(){


        arr[0]= a;
        arr[0]= b;
        arr[0]= c;
        arr[0]= d;
        arr[0]= e;
        arr[0]= f;
        arr[0]= g;

        return this;
    }
    public void createMatrx(){
    boolean[][] matrix = new boolean[7][];
    for (int i=0; i<matrix.length; i++) matrix[i] = new boolean[7];

    int nodeA = Arrays.binarySearch(arr, a);
    int nodeB = Arrays.binarySearch(arr, b);
    int nodeC = Arrays.binarySearch(arr, c);
    int nodeD = Arrays.binarySearch(arr, d);
    int nodeE = Arrays.binarySearch(arr, e);
    int nodeF = Arrays.binarySearch(arr, f);
    int nodeG = Arrays.binarySearch(arr, g);

    matrix[nodeA][nodeB] = true;
    matrix[nodeA][nodeE] = true;
    matrix[nodeB][nodeA] = true;
    matrix[nodeB][nodeG] = true;
    // A is connected to D
    matrix[nodeC][nodeG] = true;
    matrix[nodeC][nodeD] = true;
    // B is connected to D
    matrix[nodeD][nodeC] = true;
    matrix[nodeD][nodeE] = true;
    matrix[nodeE][nodeD] = true;
    // C is connected to D
    matrix[nodeE][nodeF] = true;
    matrix[nodeE][nodeA] = true;
    matrix[nodeF][nodeE] = true;
    matrix[nodeE][nodeG] = true;

    matrix[nodeG][nodeF] = true;
    matrix[nodeE][nodeB] = true;
    matrix[nodeE][nodeC] = true;

    matrix[nodeD][nodeC] = true;
    }
    }

任何有关改进的建议都将非常感谢。谢谢!

1 个答案:

答案 0 :(得分:1)

不完全明白你在那里做了什么,但有些事情让我感到震惊:

  

arr [0] = a;           arr [0] = b;           arr [0] = c;           arr [0] = d;           arr [0] = e;           arr [0] = f;           arr [0] = g;

所有人都被分配到arr [0] 您可能希望将arr的创建替换为:

Node[] arr = new Node[]{a,b,c,d,e,f,g};

这也是:

boolean[][] matrix = new boolean[7][];
for (int i=0; i<matrix.length; i++) matrix[i] = new boolean[7];

你可以这样做:

    boolean[][] matrix = new boolean[7][7];