2017-12-08 1 views
0

사용자가 노드를 추가하고 GUI 인터페이스를 사용하여 노드를 연결할 수있는 프로그램을 작성했습니다. 첫 번째 노드는 항상 "0"으로 표시됩니다. 그러나 findPath 메서드를 호출하면 0이 부족한 전체 경로가 적절하게 표시됩니다. 예를 들어 0-4에서 경로를 찾으려면 모두 숫자로 연결되어 있습니다. [1,2,3,4 ]. 노드를 연결 한 후 경로를 올바르게 읽으려면 인쇄 인접성 버튼을 눌러야합니다. 다른 모든 것은 예상대로 실행되지만 이것이 첫 번째 노드를 추가하지 않는 이유를 알 수 없습니다.노드의 재귀 경로 GUI가 경로의 첫 번째 색인을 찾지 못함

고마워!

import javax.swing.JFrame;//imports JFrame package 

    public class Graph//class header 
    { 
     public static void main (String[] args)//main method 
     { 
      JFrame frame = new JFrame ("Directed Graph");//sets variable frame to a JFrame object and titles it 
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);//exits application when it is closed 
      frame.getContentPane().add (new GraphPanel());//adding GraphPanel() to the frame 
      frame.setLocationRelativeTo(null);//sets location to the center 
      frame.pack();//packs the frame, fits everything inside 
      frame.setVisible(true);//makes frame visible 
     } 
    } 

import java.util.ArrayList;//all of these are the required to get classes from other packages 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.JPanel; 

public class GraphPanel extends JPanel 
{ 
    private final int SIZE = 10; // radius of each node 
    private double alpha; // double variable to define later 
    private JButton print, create, delete, path1; //allpath;//button variables 
    private JTextField label, label1; 
    boolean mode = true;//sets boolean for entire class to true, until later when I define it as false for certain methods to be complete 

    String x1, y1; 
    String str1 = new String("Mode Chosen Is : create");//when you choose create it will print this instead 
    String str2 = new String("Mode Chosen Is : delete");//when you choose delete it will print this 

    int x, y; 
    int mode1 = 1; // int mode1 variable, will comment why i need this when it is later defined/changed 

    private Point point1 = null, point2 = null, point3 = null;//makes pointers null, i added a third pointer to put the distance from the first drawn string at the top right, but no longer needed it 
    //keeping it incase I wanted to add it again 

    private ArrayList<Point> nodeList; // Graph nodes 
    private ArrayList<Edge> edgeList; // Graph edges 

    static private int[][] a = new int[100][100]; // Graph adjacency matrix 
    static ArrayList<Integer> path = new ArrayList<Integer>(); 

    public GraphPanel() 
    { 

     nodeList = new ArrayList<Point>();//declares ArrayList<Point> 
     edgeList = new ArrayList<Edge>();//declares ArrayList<Edge> 

     GraphListener listener = new GraphListener();//assigns 'listener' to a Graphlistener object 
     addMouseListener (listener);//accepts and reads mouseListener methods to the GraphListener object 
     addMouseMotionListener (listener);//accepts and reads MouseMotionListener methods to the GraphListener object 

     label = new JTextField (1); 
     label1 = new JTextField (1); 

     //allpath = new JButton ("Find All Paths"); 
     path1 = new JButton ("Find Path");//path button 
     create = new JButton("Create");//declares 'create' as a JButton object with text on it that says 'create' 
     delete = new JButton("Delete");//declares 'delete' as a JButton object with text that says 'delete' 
     JButton print = new JButton("Print adjacency matrix");//declares 'print' as a JButton object with text that reads 'print adjacency matrix' 

     print.addActionListener (new ButtonListener());//adds an action listener to the print button from the buttonlistener object 
     create.addActionListener (new ButtonListener1());//adds an actionListener to the create button from the buttonlistener1 object 
     delete.addActionListener (new ButtonListener2());//adds an actionliseneer to the delete button from the buttonlistener2 object 
     path1.addActionListener (new ButtonListener3());//adds button that will find path of nodes 
     //allpath.addActionListener (new ButtonListener4()); 

     setBackground (Color.black);//sets backround as black 
     setPreferredSize (new Dimension(400, 300));//this is preferred size of graphpanel 
     add(print);//these next six add()'s add each button to GraphPanel 
     add(delete);// 
     add(create);// 
     //add(allpath); 
     add(path1); 
     add(label); 
     add(label1); 
    } 

    public static boolean findPath(int x, int y) //boolean method findpath that takes two ints as parameters 
    { 
     boolean found = false; 

     if (a[x][y]==1) //base case 
     { 
      //System.out.println(x + " -> " + y);//prints node -> node 
      found = true;//path is found 
      path.add(0,y); 
     } 
     else 
     { 
      int z; 
      for (z=0; !found& z<a.length; z++) 
      { 
       if (a[x][z]==1) 
       { 
        a[x][z]=2;      
        found = findPath(z,y); 

       } 
       if (found) 
       { 
        //System.out.println(x + " -> " + z); 
        path.add(0,z); 
       } 

      } 
     } 

     if (!found){    
      System.out.println("Path Does Not Exist"); 
     } 
     return found; 
    } 

    // public static ArrayList findPath1(int x, int y, ArrayList path) 
    // { 
    // System.out.println("CALL FIND: " + path); 
    // ArrayList path2 = null; 
    // if (a[x][y]==1) 
    // { 
    // ArrayList path3 = (ArrayList)path.clone(); 
    // path3.add(y); 
    // System.out.println("EXIT BASE: " + path3); 
    // path2 = path3; 
    // } 
    // else 
    // { 
    // int z; 
    // for (z=0; z<a.length; z++) 
    // if (a[x][z]==1) 
    // { 
    // ArrayList path3 = (ArrayList)path.clone(); 
    // path3.add(z); 
    // path3 = findPath1(z,y,path3); 
    // System.out.println("EXIT RECU: " + path); 
    // path2 = path3; 
    // } 
    // } 
    // return path2; 
    // } 
    // Draws the graph 
    public void paintComponent (Graphics page) 
    { 
     super.paintComponent(page);//using parent methods 

     // Draws the edge that is being dragged 
     page.setColor (Color.green);//setting color of what will be drawn to green 
     if (mode1 == 1)//if mode1 is set to 1, which is when the create button is selected 
     { 
      page.drawString(str1, 125, 75);//then this is printed 
     } 
     if (mode1 == 2)//if mode1 is set to 2, which is when the delete button is selected 
     { 
      page.drawString(str2, 125, 75);//then this is printed 
     } 
     if (point1 != null && point2 != null)//if and only if both points are not null 
     { 
      page.drawLine (point1.x, point1.y, point2.x, point2.y);//then it draws the line from between point1(x, y cords), and point2(x, y cords) 
      page.fillOval (point2.x-3, point2.y-3, 6, 6);//fills this oval by the rectangle it is specified from 
     } 
     // Draws the nodes  
     for (int i=0; i<nodeList.size(); i++) //for loop going through the nodeList ArrayList 
     { 
      page.setColor (Color.green);//color is set to green 
      page.fillOval (nodeList.get(i).x-SIZE, nodeList.get(i).y-SIZE, SIZE*2, SIZE*2);//fills oval by subtracting the SIZE from the x, y and setting the height and width as two times the SIZE 
      page.setColor (Color.black);//sets next line to black 
      page.drawString (String.valueOf(i), nodeList.get(i).x-SIZE/2, nodeList.get(i).y+SIZE/2);//writes inside the node what number index it is from the ArrayList with black text 
     } 
     // Draws the edges 
     for (int i=0; i<edgeList.size(); i++) // for loop going through the edgeList ArrayList 
     { 
      page.setColor (Color.green);//sets the next line to green 
      page.drawLine (edgeList.get(i).a.x, edgeList.get(i).a.y,edgeList.get(i).b.x, edgeList.get(i).b.y);//draws the line from x and y cords of where it starts to end 
      //page.fillOval (edgeList.get(i).b.x-3, edgeList.get(i).b.y-3, 6, 6); 

      //page.drawString (String.valueOf(point1.x*(point2.y-point3.y)+point2.x*(point3.y-point1.y)+point3.x*(point1.y-point2.y)), 5, 15); 

      alpha = Math.atan((double)(edgeList.get(i).b.y-edgeList.get(i).a.y)/(edgeList.get(i).b.x-edgeList.get(i).a.x));//alpha = b.y-a.y cords/b.x-a.x chords as a double variable 
      if (edgeList.get(i).a.x > edgeList.get(i).b.x)//if the x chord of a is greater than the x cordinate of b 
      { 
       alpha = alpha + Math.PI;//then alpha = previously defined alpha multiplied by PI 
      } 
      if (edgeList.get(i).a.x < edgeList.get(i).b.x && edgeList.get(i).a.y > edgeList.get(i).b.y)//if a.x is less than b.x and a.y is less than b.y 
      { 
       alpha = alpha + 2*Math.PI;//then alpha becomes the previously defined alpha multiplied by 2 PI 
      } 
      arrow(page,edgeList.get(i).b.x,edgeList.get(i).b.y,0,1.57-alpha);//arrow method, taking a x(edgeList.get(i).b.x, y(edgeList.get(i).b.y, length = 0 so arrow is at the 
      // very end of the drawn line subtracted by a double aplha 
     } 
    } 

    // arrow method to call when making an arrow 
    private void arrow(Graphics page, int x, int y, int len, double alpha)//arrow method, taking a x, y, length and double parameter 
    { 
     page.setColor (Color.green);//sets arrow to green 
     int x1 = x+(int)(len*Math.sin(alpha));//x1 is set to x plus length *sin of alpha 
     int y1 = y+(int)(len*Math.cos(alpha));//y1 is set to y plus the length * cosin of alpha 
     page.drawLine (x, y, x1, y1);//drawa the x and y, and then previously defined x1, and y1 
     page.drawLine (x1, y1, x1+(int)(20*Math.sin(alpha+2.5)), y1+(int)(20*Math.cos(alpha+2.5)));//arithmatic to draw one side of the line for the arrow 
     page.drawLine (x1, y1, x1+(int)(20*Math.sin(alpha+3.7)), y1+(int)(20*Math.cos(alpha+3.7)));//arithmatic to draw the corresponding line for the arrow to complete the arrow 
    } 

    // The listener for mouse events. 
    private class GraphListener implements MouseListener, MouseMotionListener//GraphListener which implements MouseListener and MouseMotionListener, meaning it takes methods from both of those classes 
    { 
     public void mouseClicked (MouseEvent event)//when mouse is clicked and released 
     { 
      if (mode == true)//if the mode is true 
      { 
       nodeList.add(event.getPoint());//the current point to the nodeList 
       repaint();//whenever you change the look of a componenet you call this method 
      } 
      if (mode == false)//if boolean mode is false 
      { 
       for (int i=0; i<nodeList.size(); i++)//for loop to go through the nodeList ArrayList 
       { 
        if (distance(nodeList.get(i), event.getPoint()) < SIZE)//if the distance between a specific node and the pointer is less than the radius of the node (SIZE) 
         nodeList.remove(i);//then remove that node 
       } 
       for (int i=0; i<edgeList.size(); i++) 
       { 
        if (distance(edgeList.get(i).a, event.getPoint())+distance(event.getPoint(), edgeList.get(i).b)-distance(edgeList.get(i).a, edgeList.get(i).b) < SIZE) 
        //if the (distance between starting point of that edgelist index and the current point) plus (the distance between the current point and ending point of that edgeList index) 
        //subtracted by the (distance of the starting and ending point of that edgeList index)(AB+BC-AC) is all less than the size 
         edgeList.remove(i);//then remove that index 
       } 
      }    
     } 

     public void mousePressed (MouseEvent event)//when the mouse is pressed down 
     { 
      if (mode == true)//if the mode is true 
      { 
       point1 = event.getPoint();//you start that point 
      } 
     } 

     public void mouseDragged (MouseEvent event)//when the mouse is dragged while pressed down 
     { 
      if (mode == true)//if the mode is true 
      { 
       point2 = event.getPoint();//then you get the second point 
       repaint();//must use this method because you are making a change to the component 
      } 
     } 

     public void mouseReleased (MouseEvent event)//when the mouse is released after being pressed (not to be confused with a click) 
     { 
      if (mode == true)//if the mode is true 
      { 
       point2 = event.getPoint();//then you set point2 to where that point landed 
       if (point1.x != point2.x && point1.y != point2.y)//if the points are not in the same spots 
       { 
        edgeList.add(new Edge(point1,point2));//then you add a new edgeList element to the ArrayList, by definition of Edge() object 
        repaint();//must use this method because changes to component have been made 
       } 
      } 

     } 

     public void mouseMoved (MouseEvent event)// if the mouse is moved, without it being clicked (not to be confused by mouseDragged) 
     {   
      point3 = event.getPoint();//point three is set to be where the mouse is at the current time(used this to find the distance of original edge drawn, keeping here incase I want to add) 
      repaint();//must use this method because changes were made to component 

     } 

     // Empty definitions for unused event methods. 
     public void mouseEntered (MouseEvent event){}//when the mouse enters a component  

     public void mouseExited (MouseEvent event) {}//when the mouse exits a component 

     private int distance(Point p1, Point p2) //private distance formula but inside of this class so i was able to use it for determining how to erase nodes and edges 
     { 
      return (int)Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));//square root of x cordinants of first point minus second point times its self plus the y coordinates 
      //of first point minus second point times itself 
     } 
    } 
    // Represents the graph edges 
    private class Edge //defines how edges are made 
    { 
     Point a, b; 

     public Edge(Point a, Point b) 
     { 
      this.a = a; 
      this.b = b; 
     } 
    } 

    private class ButtonListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 

      if (event.getSource()== create) 
      { 
       mode = true; 
       mode1 = 1; 
      } 
      if (event.getSource()==delete) 
      { 
       mode = !true; 
       mode1 = 2; 

      } 

      // Initializes graph adjacency matrix 
      for (int i=0; i<nodeList.size(); i++) 
       for (int j=0; j<nodeList.size(); j++) a[i][j]=0; 

      // Includes the edges in the graph adjacency matrix 
      for (int i=0; i<edgeList.size(); i++) 
      { 
       for (int j=0; j<nodeList.size(); j++) 
        if (distance(nodeList.get(j),edgeList.get(i).a)<=SIZE+3) 
         for (int k=0; k<nodeList.size(); k++) 
          if (distance(nodeList.get(k),edgeList.get(i).b)<=SIZE+3) 
          { 
           System.out.println(j+"->"+k); 
           a[j][k]=1; 
          } 
      } 

      if (event.getSource()==print) 
      { 

       // Prints the graph adjacency matrix 
       for (int i=0; i<nodeList.size(); i++) 
       { 
        for (int j=0; j<nodeList.size(); j++) 
         System.out.print(a[i][j]+"\t"); 
        System.out.println(); 
       } 
      } 
     } 
     // Euclidean distance function  
     private int distance(Point p1, Point p2) 
     { 
      return (int)Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); 
     } 
    } 

    private class ButtonListener1 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     {    
      if (event.getSource()== create)//if create button is selected 
      { 
       mode = true;//then the mode is set to true 
       mode1 = 1;//and mode1 is set to 1 
      }     
     }   
    } 

    private class ButtonListener2 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     {    
      if (event.getSource()== delete)//if create button is selected 
      { 
       mode = !true;//then the mode is set to true 
       mode1 = 2;//and mode1 is set to 2, which would change the string that is output 
      }     
     }   
    } 

    private class ButtonListener3 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 
      x1 = label.getText(); 
      y1 = label1.getText(); 
      x = Integer.parseInt(x1); 
      y = Integer.parseInt(y1); 

      System.out.println("Path from " +x+" to " +y); 
      System.out.println(findPath(x,y)); 
      System.out.println(path); 

     } 
    } 

답변

0

경로에 첫 번째 노드를 절대 추가하지 마십시오. 경로가 존재하는지 확인할 때 당신은 그것을 할 수 있습니다 :

if (!found){    
    System.out.println("Path Does Not Exist"); 
} else { 
    path.add(0, x); 
} 

을 그리고 당신은이 블록을 제거해야합니다

if (found) { 
    //System.out.println(x + " -> " + z); 
    path.add(0,z); 
} 

그래서 완전한 방법은 또한

public static boolean findPath(int x, int y) //boolean method findpath that takes two ints as parameters 
{ 
    boolean found = false; 

    if (a[x][y]==1) //base case 
    { 
     //System.out.println(x + " -> " + y);//prints node -> node 
     found = true;//path is found 
     path.add(0,y); 
    } 
    else 
    { 
     int z; 
     for (z=0; !found& z<a.length; z++) 
     { 
      if (a[x][z]==1) 
      { 
       a[x][z]=2; 
       found = findPath(z,y); 

      } 
      /*if (found) 
      { 
       //System.out.println(x + " -> " + z); 
       path.add(0,z); 
      }*/ 

     } 
    } 

    if (!found){ 
     System.out.println("Path Does Not Exist"); 
    } else { 
     path.add(0,x); 
    } 
    return found; 
} 

내가 좋아하는 모양을 노드 간의 최단 경로를 찾는 중 을 (를) 찾아 볼 것을 권합니다.

+0

감사합니다 남자하지만 난 그것을 시도하고 나에게 좀 더 많은 문제를 준, 트릭은 당신보다 더 클릭하면 있도록 배열을 취소 한 후 findPath를 호출 한 후 buttonlistener 클래스에 추가하는 것이 었습니다 일단 동일한 경로를 추가하고 거대한 배열을 만드는 것을 계속하지 않으면 – alwaysquestioning

+0

글쎄, 이것도 해결책이다. 하지만 궁금 해서요, 제 코드의 문제점은 무엇입니까? –

0

나는 그것을 발견 할 수있었습니다. , 물론 내가 놓친 가장 작은 실수였습니다. 다른 누구에게도 동일한 문제가 발생했을 때 도움이되기를 바랍니다. 주된 차이점은 buttonlistener3 클래스에 있습니다. 그러나이 모든 것을 다시 추가하여 누군가를 도와줍니다.

import java.util.ArrayList;//all of these are the required to get classes from other packages 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.JPanel; 

public class GraphPanel extends JPanel 
{ 
    private final int SIZE = 10; // radius of each node 
    private double alpha; // double variable to define later 
    private JButton print, create, delete, path1; //allpath;//button variables 
    private JTextField label, label1; 
    boolean mode = true;//sets boolean for entire class to true, until later when I define it as false for certain methods to be complete 

    String x1, y1; 
    String str1 = new String("Mode Chosen Is : create");//when you choose create it will print this instead 
    String str2 = new String("Mode Chosen Is : delete");//when you choose delete it will print this 

    int x, y; 
    int mode1 = 1; // int mode1 variable, will comment why i need this when it is later defined/changed 

    private Point point1 = null, point2 = null, point3 = null;//makes pointers null, i added a third pointer to put the distance from the first drawn string at the top right, but no longer needed it 
    //keeping it incase I wanted to add it again 

    private ArrayList<Point> nodeList; // Graph nodes 
    private ArrayList<Edge> edgeList; // Graph edges 

    static private int[][] a = new int[100][100]; // Graph adjacency matrix 
    static ArrayList<Integer> path = new ArrayList<Integer>(); 

    public GraphPanel() 
    { 

     nodeList = new ArrayList<Point>();//declares ArrayList<Point> 
     edgeList = new ArrayList<Edge>();//declares ArrayList<Edge> 

     GraphListener listener = new GraphListener();//assigns 'listener' to a Graphlistener object 
     addMouseListener (listener);//accepts and reads mouseListener methods to the GraphListener object 
     addMouseMotionListener (listener);//accepts and reads MouseMotionListener methods to the GraphListener object 

     label = new JTextField (1); 
     label1 = new JTextField (1); 

     //allpath = new JButton ("Find All Paths"); 
     path1 = new JButton ("Find Path");//path button 
     create = new JButton("Create");//declares 'create' as a JButton object with text on it that says 'create' 
     delete = new JButton("Delete");//declares 'delete' as a JButton object with text that says 'delete' 
     JButton print = new JButton("Print adjacency matrix");//declares 'print' as a JButton object with text that reads 'print adjacency matrix' 

     print.addActionListener (new ButtonListener());//adds an action listener to the print button from the buttonlistener object 
     create.addActionListener (new ButtonListener1());//adds an actionListener to the create button from the buttonlistener1 object 
     delete.addActionListener (new ButtonListener2());//adds an actionliseneer to the delete button from the buttonlistener2 object 
     path1.addActionListener (new ButtonListener3());//adds button that will find path of nodes 
     //allpath.addActionListener (new ButtonListener4()); 

     setBackground (Color.black);//sets backround as black 
     setPreferredSize (new Dimension(400, 300));//this is preferred size of graphpanel 
     add(print);//these next six add()'s add each button to GraphPanel 
     add(delete);// 
     add(create);// 
     //add(allpath); 
     add(path1); 
     add(label); 
     add(label1); 
    } 

    public static boolean findPath(int x, int y) //boolean method findpath that takes two ints as parameters 
    { 
     boolean found = false; 

     if (a[x][y]==1) //base case 
     { 
      //System.out.println(x + " -> " + y);//prints node -> node 
      found = true;//path is found 
      path.add(0,y); 
     } 
     else 
     { 
      int z; 
      for (z=0; !found& z<a.length; z++) 
      { 
       if (a[x][z]==1) 
       { 
        a[x][z]=2;    
        found = findPath(z,y); 

       } 
       if (found) 
       { 
        //System.out.println(x + " -> " + z); 
        path.add(0,z); 
       } 

      } 
     } 
     return found; 
    } 

    // public static ArrayList findPath1(int x, int y, ArrayList path) 
    // { 
    // System.out.println("CALL FIND: " + path); 
    // ArrayList path2 = null; 
    // if (a[x][y]==1) 
    // { 
    // ArrayList path3 = (ArrayList)path.clone(); 
    // path3.add(y); 
    // System.out.println("EXIT BASE: " + path3); 
    // path2 = path3; 
    // } 
    // else 
    // { 
    // int z; 
    // for (z=0; z<a.length; z++) 
    // if (a[x][z]==1) 
    // { 
    // ArrayList path3 = (ArrayList)path.clone(); 
    // path3.add(z); 
    // path3 = findPath1(z,y,path3); 
    // System.out.println("EXIT RECU: " + path); 
    // path2 = path3; 
    // } 
    // } 
    // return path2; 
    // } 
    // Draws the graph 
    public void paintComponent (Graphics page) 
    { 
     super.paintComponent(page);//using parent methods 

     // Draws the edge that is being dragged 
     page.setColor (Color.green);//setting color of what will be drawn to green 
     if (mode1 == 1)//if mode1 is set to 1, which is when the create button is selected 
     { 
      page.drawString(str1, 125, 75);//then this is printed 
     } 
     if (mode1 == 2)//if mode1 is set to 2, which is when the delete button is selected 
     { 
      page.drawString(str2, 125, 75);//then this is printed 
     } 
     if (point1 != null && point2 != null)//if and only if both points are not null 
     { 
      page.drawLine (point1.x, point1.y, point2.x, point2.y);//then it draws the line from between point1(x, y cords), and point2(x, y cords) 
      page.fillOval (point2.x-3, point2.y-3, 6, 6);//fills this oval by the rectangle it is specified from 
     } 
     // Draws the nodes  
     for (int i=0; i<nodeList.size(); i++) //for loop going through the nodeList ArrayList 
     { 
      page.setColor (Color.green);//color is set to green 
      page.fillOval (nodeList.get(i).x-SIZE, nodeList.get(i).y-SIZE, SIZE*2, SIZE*2);//fills oval by subtracting the SIZE from the x, y and setting the height and width as two times the SIZE 
      page.setColor (Color.black);//sets next line to black 
      page.drawString (String.valueOf(i), nodeList.get(i).x-SIZE/2, nodeList.get(i).y+SIZE/2);//writes inside the node what number index it is from the ArrayList with black text 
     } 
     // Draws the edges 
     for (int i=0; i<edgeList.size(); i++) // for loop going through the edgeList ArrayList 
     { 
      page.setColor (Color.green);//sets the next line to green 
      page.drawLine (edgeList.get(i).a.x, edgeList.get(i).a.y,edgeList.get(i).b.x, edgeList.get(i).b.y);//draws the line from x and y cords of where it starts to end 
      //page.fillOval (edgeList.get(i).b.x-3, edgeList.get(i).b.y-3, 6, 6); 

      //page.drawString (String.valueOf(point1.x*(point2.y-point3.y)+point2.x*(point3.y-point1.y)+point3.x*(point1.y-point2.y)), 5, 15); 

      alpha = Math.atan((double)(edgeList.get(i).b.y-edgeList.get(i).a.y)/(edgeList.get(i).b.x-edgeList.get(i).a.x));//alpha = b.y-a.y cords/b.x-a.x chords as a double variable 
      if (edgeList.get(i).a.x > edgeList.get(i).b.x)//if the x chord of a is greater than the x cordinate of b 
      { 
       alpha = alpha + Math.PI;//then alpha = previously defined alpha multiplied by PI 
      } 
      if (edgeList.get(i).a.x < edgeList.get(i).b.x && edgeList.get(i).a.y > edgeList.get(i).b.y)//if a.x is less than b.x and a.y is less than b.y 
      { 
       alpha = alpha + 2*Math.PI;//then alpha becomes the previously defined alpha multiplied by 2 PI 
      } 
      arrow(page,edgeList.get(i).b.x,edgeList.get(i).b.y,0,1.57-alpha);//arrow method, taking a x(edgeList.get(i).b.x, y(edgeList.get(i).b.y, length = 0 so arrow is at the 
      // very end of the drawn line subtracted by a double aplha 
     } 
    } 

    // arrow method to call when making an arrow 
    private void arrow(Graphics page, int x, int y, int len, double alpha)//arrow method, taking a x, y, length and double parameter 
    { 
     page.setColor (Color.green);//sets arrow to green 
     int x1 = x+(int)(len*Math.sin(alpha));//x1 is set to x plus length *sin of alpha 
     int y1 = y+(int)(len*Math.cos(alpha));//y1 is set to y plus the length * cosin of alpha 
     page.drawLine (x, y, x1, y1);//drawa the x and y, and then previously defined x1, and y1 
     page.drawLine (x1, y1, x1+(int)(20*Math.sin(alpha+2.5)), y1+(int)(20*Math.cos(alpha+2.5)));//arithmatic to draw one side of the line for the arrow 
     page.drawLine (x1, y1, x1+(int)(20*Math.sin(alpha+3.7)), y1+(int)(20*Math.cos(alpha+3.7)));//arithmatic to draw the corresponding line for the arrow to complete the arrow 
    } 

    // The listener for mouse events. 
    private class GraphListener implements MouseListener, MouseMotionListener//GraphListener which implements MouseListener and MouseMotionListener, meaning it takes methods from both of those classes 
    { 
     public void mouseClicked (MouseEvent event)//when mouse is clicked and released 
     { 
      if (mode == true)//if the mode is true 
      { 
       nodeList.add(event.getPoint());//the current point to the nodeList 
       repaint();//whenever you change the look of a componenet you call this method 
      } 
      if (mode == false)//if boolean mode is false 
      { 
       for (int i=0; i<nodeList.size(); i++)//for loop to go through the nodeList ArrayList 
       { 
        if (distance(nodeList.get(i), event.getPoint()) < SIZE)//if the distance between a specific node and the pointer is less than the radius of the node (SIZE) 
         nodeList.remove(i);//then remove that node 
       } 
       for (int i=0; i<edgeList.size(); i++) 
       { 
        if (distance(edgeList.get(i).a, event.getPoint())+distance(event.getPoint(), edgeList.get(i).b)-distance(edgeList.get(i).a, edgeList.get(i).b) < SIZE) 
        //if the (distance between starting point of that edgelist index and the current point) plus (the distance between the current point and ending point of that edgeList index) 
        //subtracted by the (distance of the starting and ending point of that edgeList index)(AB+BC-AC) is all less than the size 
         edgeList.remove(i);//then remove that index 
       } 
      }    
     } 

     public void mousePressed (MouseEvent event)//when the mouse is pressed down 
     { 
      if (mode == true)//if the mode is true 
      { 
       point1 = event.getPoint();//you start that point 
      } 
     } 

     public void mouseDragged (MouseEvent event)//when the mouse is dragged while pressed down 
     { 
      if (mode == true)//if the mode is true 
      { 
       point2 = event.getPoint();//then you get the second point 
       repaint();//must use this method because you are making a change to the component 
      } 
     } 

     public void mouseReleased (MouseEvent event)//when the mouse is released after being pressed (not to be confused with a click) 
     { 
      if (mode == true)//if the mode is true 
      { 
       point2 = event.getPoint();//then you set point2 to where that point landed 
       if (point1.x != point2.x && point1.y != point2.y)//if the points are not in the same spots 
       { 
        edgeList.add(new Edge(point1,point2));//then you add a new edgeList element to the ArrayList, by definition of Edge() object 
        repaint();//must use this method because changes to component have been made 
       } 
      } 

     } 

     public void mouseMoved (MouseEvent event)// if the mouse is moved, without it being clicked (not to be confused by mouseDragged) 
     {   
      point3 = event.getPoint();//point three is set to be where the mouse is at the current time(used this to find the distance of original edge drawn, keeping here incase I want to add) 
      repaint();//must use this method because changes were made to component 

     } 

     // Empty definitions for unused event methods. 
     public void mouseEntered (MouseEvent event){}//when the mouse enters a component  

     public void mouseExited (MouseEvent event) {}//when the mouse exits a component 

     private int distance(Point p1, Point p2) //private distance formula but inside of this class so i was able to use it for determining how to erase nodes and edges 
     { 
      return (int)Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));//square root of x cordinants of first point minus second point times its self plus the y coordinates 
      //of first point minus second point times itself 
     } 
    } 
    // Represents the graph edges 
    private class Edge //defines how edges are made 
    { 
     Point a, b; 

     public Edge(Point a, Point b) 
     { 
      this.a = a; 
      this.b = b; 
     } 
    } 

    private class ButtonListener implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 

      if (event.getSource()== create) 
      { 
       mode = true; 
       mode1 = 1; 
      } 
      if (event.getSource()==delete) 
      { 
       mode = !true; 
       mode1 = 2; 

      } 

      // Initializes graph adjacency matrix 
      for (int i=0; i<nodeList.size(); i++) 
       for (int j=0; j<nodeList.size(); j++) a[i][j]=0; 

      // Includes the edges in the graph adjacency matrix 
      for (int i=0; i<edgeList.size(); i++) 
      { 
       for (int j=0; j<nodeList.size(); j++) 
        if (distance(nodeList.get(j),edgeList.get(i).a)<=SIZE+3) 
         for (int k=0; k<nodeList.size(); k++) 
          if (distance(nodeList.get(k),edgeList.get(i).b)<=SIZE+3) 
          { 
           System.out.println(j+"->"+k); 

           a[j][k]=1; 
          } 
      } 

      if (event.getSource()==print) 
      { 

       // Prints the graph adjacency matrix 
       for (int i=0; i<nodeList.size(); i++) 
       { 
        for (int j=0; j<nodeList.size(); j++) 
         System.out.print(a[i][j]+"\t"); 
        System.out.println(); 
       } 
      } 
     } 
     // Euclidean distance function  
     private int distance(Point p1, Point p2) 
     { 
      return (int)Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); 
     } 
    } 

    private class ButtonListener1 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     {    
      if (event.getSource()== create)//if create button is selected 
      { 
       mode = true;//then the mode is set to true 
       mode1 = 1;//and mode1 is set to 1 
      }     
     }   
    } 

    private class ButtonListener2 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     {    
      if (event.getSource()== delete)//if create button is selected 
      { 
       mode = !true;//then the mode is set to true 
       mode1 = 2;//and mode1 is set to 2, which would change the string that is output 
      }     
     }   
    } 

    private class ButtonListener3 implements ActionListener 
    { 
     public void actionPerformed (ActionEvent event) 
     { 
      x1 = label.getText(); 
      y1 = label1.getText(); 
      x = Integer.parseInt(x1); 
      y = Integer.parseInt(y1); 
      System.out.println("Path from " +x+" to " +y); 
      System.out.println(findPath(x,y)); 
      path.add(0,x); 
      System.out.println(path); 
      path.clear(); 
     } 
    } 

    // private class ButtonListener4 implements ActionListener 
    // { 
    // public void actionPerformed (ActionEvent event) 
    // { 
    // x1 = label.getText(); 
    // y1 = label1.getText(); 
    // x = Integer.parseInt(x1); 
    // y = Integer.parseInt(y1); 
    // System.out.println("Path from " +x+" to " +y); 
    // System.out.println(findPath1(x,y,path)); 
    // System.out.println(path); 
    // } 
    // } 
} 
관련 문제