GUI边缘到节点递归路径没有找到路径的第一个索引

时间:2017-12-08 02:56:44

标签: java user-interface recursion

我编写了一个程序,允许用户添加节点并使用GUI界面连接边缘。第一个节点始终标记为" 0"。当我调用findPath方法但是它只显示缺少0的整个路径。例如,如果我想找到0-4的路径,并且它们全部以数字方式连接,则显示[1,2,3] ,4]。要在连接节点后正确读取路径,您需要点击打印邻接按钮。其他所有东西都按预期运行,但我似乎无法弄清楚为什么这不会添加第一个节点。

谢谢大家!

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

        }
    }

2 个答案:

答案 0 :(得分: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;
}

我还建议您查看Dijkstra's algorithm找到节点之间的最短路径

答案 1 :(得分: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);
    // }
    // }
}