Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LK IF-1 J Q1
- // graphische Benutzeroberfläche zum ADT Graph.
- // Rg, 13.3.2013
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class GUI extends JFrame implements ActionListener
- {
- private JTextField tfAnzeige;
- private boolean gefunden;
- private List list;
- private String[] knoten = {"Eins", "Zwei", "Drei", "Vier"} ;
- private int[][] adjazenzMatrix = { {0,1,0,1}, {1,0,0,1}, {0,0,0,1}, {1,1,1,0}} ;
- // 0 1 0 1
- // 1 0 0 1
- // 0 0 0 1
- // 1 1 1 0
- private Graph graph = new Graph();
- public GUI()
- {
- super.getContentPane().setLayout(null);
- super.getContentPane().setBackground(Color.lightGray);
- //Textfelder
- tfAnzeige = new JTextField (20);
- tfAnzeige.setBounds (250, 100, 150, 30);
- super.getContentPane().add (tfAnzeige);
- //Dialog-Buttons
- JButton button1 = new JButton("Test1");
- button1.setBounds (50, 30, 150, 25);
- button1.setBackground (Color.yellow);
- button1.addActionListener(this);
- this.getContentPane().add(button1);
- JButton button2 = new JButton("Suche");
- button2.setBounds (50, 60, 150, 25);
- button2.setBackground (Color.yellow);
- button2.addActionListener(this);
- this.getContentPane().add(button2);
- JButton button3 = new JButton("Test3");
- button3.setBounds (50, 90, 150, 25);
- button3.setBackground (Color.yellow);
- button3.addActionListener(this);
- this.getContentPane().add(button3);
- JButton button4 = new JButton("Test4");
- button4.setBounds (50, 120, 150, 25);
- button4.setBackground (Color.yellow);
- button4.addActionListener(this);
- this.getContentPane().add(button4);
- //Window-Listener
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent event){
- setVisible(false);
- dispose();
- System.exit(0);
- }
- });
- }
- public Graph matrixToGraph(int[][] matrix, String[] knoten)
- {
- Graph ausgabeGraph = new Graph();
- int n = matrix.length;
- for (int i=0; i<n; i++) // Einfuegen der Knoten in den Graph
- {
- GraphNode aktNode = new GraphNode(knoten[i]);
- ausgabeGraph.addNode(aktNode);
- }
- for (int i=0; i<n; i++)
- {
- GraphNode aktNode01 = ausgabeGraph.getNode(knoten[i]);
- for(int j=0; j<n; j++)
- {
- GraphNode aktNode02 = ausgabeGraph.getNode(knoten[j]);
- if(matrix[i][j]==1)
- {
- ausgabeGraph.addEdge(aktNode01, aktNode02, 100);
- }
- }
- }
- return ausgabeGraph;
- }
- public List nachbarListe(Graph graph, String knoten, String[] alleKnoten)
- {
- List ausgabeListe = new List();
- GraphNode temp = graph.getNode(knoten);
- int n = alleKnoten.length;
- for(int i=0; i<n; i++)
- {
- if(graph.hasEdge(graph.getNode(knoten), graph.getNode(alleKnoten[i])))
- {
- ausgabeListe.append(alleKnoten[i]);
- }
- }
- return ausgabeListe;
- }
- public void sucheWege(Graph graph, String startknoten, String endknoten)
- {
- GraphNode temp = new GraphNode(startknoten);
- temp.mark();
- if(temp.getNeighbours_().hasAccess())
- {
- GraphNode temp1 = (GraphNode)temp.getNeighbours_().getObject();
- if(temp1.getName()==endknoten)
- {
- gefunden = true;
- }
- else if(temp1.getName()!=endknoten)
- {
- sucheWege(graph, temp1.getName(), endknoten);
- }
- temp.getNeighbours_().next();
- }
- else if(!temp.getNeighbours_().hasAccess())
- {
- }
- }
- public void sucheWege1(Graph graph, String startknoten, String endknoten)
- {
- GraphNode temp = new GraphNode(startknoten);
- temp.mark();
- list.insert(startknoten);
- if(temp.getNeighbours_().hasAccess())
- {
- GraphNode temp1 = (GraphNode)temp.getNeighbours_().getObject();
- list.insert(temp1.getName());
- if(temp1.isMarked())
- {
- list.remove();
- temp.getNeighbours_().next();
- sucheWege1(graph, temp.getName(), endknoten) ;
- }
- else if(!temp1.isMarked())
- {
- if(temp1.getName()==endknoten)
- {
- gefunden = true;
- }
- else
- {
- temp1.mark();
- sucheWege1(graph, temp1.getName(),endknoten);
- }
- }
- }
- else if(temp.getNeighbours_().hasAccess())
- {
- list.remove();
- list.toLast();
- sucheWege(graph,(String) list.getObject(), endknoten);
- }
- }
- public void sucheWege2(Graph graph, String startknoten, String endknoten)
- {
- GraphNode temp = new GraphNode(startknoten);
- temp.mark();
- List list1 = nachbarListe();
- list1.toLast();
- GraphNode temp2=(GraphNode)list1.getObject();
- if(temp2.isMarked())
- {
- if(temp2.getNeighbours_().hasAccess())
- {
- sucheWege2(graph, temp2.getName(), endknoten);
- }
- else
- {
- list1.remove();
- list1.toLast();
- temp2=(GraphNode)list1.getObject();
- sucheWege2(graph, temp2.getName(),endknoten);
- }
- }
- if(!temp2.isMarked())
- {
- if(temp2.getName()==endknoten)
- {
- gefunden=true;
- }
- else if(temp2.getName()!= endknoten)
- {
- temp2.mark();
- if(temp2.getNeighbours_().hasAccess())
- {
- sucheWege2(graph, temp2.getName(), endknoten);
- }
- else
- {
- list1.remove();
- list1.toLast();
- temp2=(GraphNode)list1.getObject();
- sucheWege2(graph, temp2.getName(),endknoten);
- }
- }
- }
- }
- public void actionPerformed(ActionEvent event)
- {
- String cmd = event.getActionCommand();
- if (cmd.equals("Test1"))
- {
- graph = matrixToGraph(adjazenzMatrix, knoten);
- System.out.println("Graph wurde erstellt.");
- }
- else if (cmd.equals("Suche"))
- {
- String text = tfAnzeige.getText();
- if(graph.hasNode(text))
- {
- System.out.println("Der Graph besitzt einen Knoten mit Namen "+text+".");
- }
- else
- {
- System.out.println("Ein Knoten mit Namen "+text+" wurde nicht gefunden.");
- }
- }
- else if(cmd.equals("Test3"))
- {
- List temp = nachbarListe(graph, "Eins", knoten);
- List temp2 = nachbarListe(graph, "Zwei", knoten);
- List temp3 = nachbarListe(graph, "Drei", knoten);
- List temp4 = nachbarListe(graph, "Vier", knoten);
- temp.toFirst();
- temp2.toFirst();
- temp3.toFirst();
- temp4.toFirst();
- System.out.println(" "+adjazenzMatrix[0][0]+adjazenzMatrix[0][1]+adjazenzMatrix[0][2]+adjazenzMatrix[0][3]+" ");
- System.out.println(" "+adjazenzMatrix[1][0]+adjazenzMatrix[1][1]+adjazenzMatrix[1][2]+adjazenzMatrix[1][3]+" ");
- System.out.println(" "+adjazenzMatrix[2][0]+adjazenzMatrix[2][1]+adjazenzMatrix[2][2]+adjazenzMatrix[2][3]+" ");
- System.out.println(" "+adjazenzMatrix[3][0]+adjazenzMatrix[3][1]+adjazenzMatrix[3][2]+adjazenzMatrix[3][3]+" ");
- System.out.println(" ");
- System.out.println("-----------------------------");
- System.out.println("Knoten Eins");
- System.out.println("-----------------------------");
- while(temp.hasAccess())
- {
- System.out.println(" "+(String)temp.getObject());
- temp.next();
- }
- System.out.println("-----------------------------");
- System.out.println("Knoten Zwei");
- System.out.println("-----------------------------");
- while(temp2.hasAccess())
- {
- System.out.println(" "+(String)temp2.getObject());
- temp2.next();
- }
- System.out.println("-----------------------------");
- System.out.println("Knoten Drei");
- System.out.println("-----------------------------");
- while(temp3.hasAccess())
- {
- System.out.println(" "+(String)temp3.getObject());
- temp3.next();
- }
- System.out.println("-----------------------------");
- System.out.println("Knoten Vier");
- System.out.println("-----------------------------");
- while(temp4.hasAccess())
- {
- System.out.println(" "+(String)temp4.getObject());
- temp4.next();
- }
- }
- else if(cmd.equals("Test4"))
- {
- sucheWege2(graph, "Eins", "Eins");
- System.out.println(gefunden + " ");
- }
- }
- /* while(!graph.getNodes().isEmpty())
- {
- if(graph.hasEdge(temp, (GraphNode)graph.getNodes().getObject()))
- {
- ausgabeListe.append((GraphNode)graph.getNodes().getObject());
- }
- graph.getNodes().remove();
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement