Advertisement
cr34tiv3

Untitled

Mar 18th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 KB | None | 0 0
  1. // LK IF-1 J Q1
  2. // graphische Benutzeroberfläche zum ADT Graph.
  3.  
  4. // Rg, 13.3.2013
  5.  
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import javax.swing.*;
  9.  
  10. public class GUI extends JFrame implements ActionListener
  11. {
  12. private JTextField tfAnzeige;
  13. private boolean gefunden;
  14. private List list;
  15. private String[] knoten = {"Eins", "Zwei", "Drei", "Vier"} ;
  16. private int[][] adjazenzMatrix = { {0,1,0,1}, {1,0,0,1}, {0,0,0,1}, {1,1,1,0}} ;
  17. // 0 1 0 1
  18. // 1 0 0 1
  19. // 0 0 0 1
  20. // 1 1 1 0
  21. private Graph graph = new Graph();
  22.  
  23. public GUI()
  24. {
  25. super.getContentPane().setLayout(null);
  26. super.getContentPane().setBackground(Color.lightGray);
  27. //Textfelder
  28. tfAnzeige = new JTextField (20);
  29. tfAnzeige.setBounds (250, 100, 150, 30);
  30. super.getContentPane().add (tfAnzeige);
  31.  
  32. //Dialog-Buttons
  33. JButton button1 = new JButton("Test1");
  34. button1.setBounds (50, 30, 150, 25);
  35. button1.setBackground (Color.yellow);
  36. button1.addActionListener(this);
  37. this.getContentPane().add(button1);
  38. JButton button2 = new JButton("Suche");
  39. button2.setBounds (50, 60, 150, 25);
  40. button2.setBackground (Color.yellow);
  41. button2.addActionListener(this);
  42. this.getContentPane().add(button2);
  43. JButton button3 = new JButton("Test3");
  44. button3.setBounds (50, 90, 150, 25);
  45. button3.setBackground (Color.yellow);
  46. button3.addActionListener(this);
  47. this.getContentPane().add(button3);
  48. JButton button4 = new JButton("Test4");
  49. button4.setBounds (50, 120, 150, 25);
  50. button4.setBackground (Color.yellow);
  51. button4.addActionListener(this);
  52. this.getContentPane().add(button4);
  53.  
  54.  
  55.  
  56. //Window-Listener
  57. addWindowListener(new WindowAdapter() {
  58. public void windowClosing(WindowEvent event){
  59. setVisible(false);
  60. dispose();
  61. System.exit(0);
  62. }
  63. });
  64. }
  65.  
  66. public Graph matrixToGraph(int[][] matrix, String[] knoten)
  67. {
  68. Graph ausgabeGraph = new Graph();
  69. int n = matrix.length;
  70.  
  71. for (int i=0; i<n; i++) // Einfuegen der Knoten in den Graph
  72. {
  73. GraphNode aktNode = new GraphNode(knoten[i]);
  74. ausgabeGraph.addNode(aktNode);
  75. }
  76.  
  77. for (int i=0; i<n; i++)
  78. {
  79. GraphNode aktNode01 = ausgabeGraph.getNode(knoten[i]);
  80.  
  81. for(int j=0; j<n; j++)
  82. {
  83. GraphNode aktNode02 = ausgabeGraph.getNode(knoten[j]);
  84.  
  85. if(matrix[i][j]==1)
  86. {
  87. ausgabeGraph.addEdge(aktNode01, aktNode02, 100);
  88. }
  89. }
  90.  
  91. }
  92.  
  93.  
  94.  
  95. return ausgabeGraph;
  96. }
  97.  
  98. public List nachbarListe(Graph graph, String knoten, String[] alleKnoten)
  99. {
  100. List ausgabeListe = new List();
  101. GraphNode temp = graph.getNode(knoten);
  102. int n = alleKnoten.length;
  103.  
  104. for(int i=0; i<n; i++)
  105. {
  106. if(graph.hasEdge(graph.getNode(knoten), graph.getNode(alleKnoten[i])))
  107. {
  108. ausgabeListe.append(alleKnoten[i]);
  109. }
  110. }
  111.  
  112. return ausgabeListe;
  113. }
  114.  
  115. public void sucheWege(Graph graph, String startknoten, String endknoten)
  116. {
  117. GraphNode temp = new GraphNode(startknoten);
  118. temp.mark();
  119. if(temp.getNeighbours_().hasAccess())
  120. {
  121. GraphNode temp1 = (GraphNode)temp.getNeighbours_().getObject();
  122. if(temp1.getName()==endknoten)
  123. {
  124. gefunden = true;
  125. }
  126. else if(temp1.getName()!=endknoten)
  127. {
  128. sucheWege(graph, temp1.getName(), endknoten);
  129. }
  130. temp.getNeighbours_().next();
  131. }
  132. else if(!temp.getNeighbours_().hasAccess())
  133. {
  134.  
  135. }
  136. }
  137.  
  138. public void sucheWege1(Graph graph, String startknoten, String endknoten)
  139. {
  140. GraphNode temp = new GraphNode(startknoten);
  141. temp.mark();
  142. list.insert(startknoten);
  143. if(temp.getNeighbours_().hasAccess())
  144. {
  145. GraphNode temp1 = (GraphNode)temp.getNeighbours_().getObject();
  146. list.insert(temp1.getName());
  147. if(temp1.isMarked())
  148. {
  149. list.remove();
  150. temp.getNeighbours_().next();
  151. sucheWege1(graph, temp.getName(), endknoten) ;
  152. }
  153. else if(!temp1.isMarked())
  154. {
  155. if(temp1.getName()==endknoten)
  156. {
  157. gefunden = true;
  158. }
  159. else
  160. {
  161. temp1.mark();
  162. sucheWege1(graph, temp1.getName(),endknoten);
  163. }
  164. }
  165. }
  166. else if(temp.getNeighbours_().hasAccess())
  167. {
  168. list.remove();
  169. list.toLast();
  170. sucheWege(graph,(String) list.getObject(), endknoten);
  171. }
  172. }
  173.  
  174. public void sucheWege2(Graph graph, String startknoten, String endknoten)
  175. {
  176. GraphNode temp = new GraphNode(startknoten);
  177. temp.mark();
  178. List list1 = nachbarListe();
  179. list1.toLast();
  180. GraphNode temp2=(GraphNode)list1.getObject();
  181. if(temp2.isMarked())
  182. {
  183. if(temp2.getNeighbours_().hasAccess())
  184. {
  185. sucheWege2(graph, temp2.getName(), endknoten);
  186. }
  187. else
  188. {
  189. list1.remove();
  190. list1.toLast();
  191. temp2=(GraphNode)list1.getObject();
  192. sucheWege2(graph, temp2.getName(),endknoten);
  193. }
  194. }
  195. if(!temp2.isMarked())
  196. {
  197. if(temp2.getName()==endknoten)
  198. {
  199. gefunden=true;
  200. }
  201. else if(temp2.getName()!= endknoten)
  202. {
  203. temp2.mark();
  204. if(temp2.getNeighbours_().hasAccess())
  205. {
  206. sucheWege2(graph, temp2.getName(), endknoten);
  207. }
  208. else
  209. {
  210. list1.remove();
  211. list1.toLast();
  212. temp2=(GraphNode)list1.getObject();
  213. sucheWege2(graph, temp2.getName(),endknoten);
  214. }
  215. }
  216. }
  217.  
  218. }
  219.  
  220.  
  221. public void actionPerformed(ActionEvent event)
  222. {
  223. String cmd = event.getActionCommand();
  224. if (cmd.equals("Test1"))
  225. {
  226. graph = matrixToGraph(adjazenzMatrix, knoten);
  227.  
  228. System.out.println("Graph wurde erstellt.");
  229. }
  230. else if (cmd.equals("Suche"))
  231. {
  232. String text = tfAnzeige.getText();
  233.  
  234. if(graph.hasNode(text))
  235. {
  236. System.out.println("Der Graph besitzt einen Knoten mit Namen "+text+".");
  237. }
  238. else
  239. {
  240. System.out.println("Ein Knoten mit Namen "+text+" wurde nicht gefunden.");
  241. }
  242.  
  243.  
  244. }
  245. else if(cmd.equals("Test3"))
  246. {
  247. List temp = nachbarListe(graph, "Eins", knoten);
  248. List temp2 = nachbarListe(graph, "Zwei", knoten);
  249. List temp3 = nachbarListe(graph, "Drei", knoten);
  250. List temp4 = nachbarListe(graph, "Vier", knoten);
  251. temp.toFirst();
  252. temp2.toFirst();
  253. temp3.toFirst();
  254. temp4.toFirst();
  255. System.out.println(" "+adjazenzMatrix[0][0]+adjazenzMatrix[0][1]+adjazenzMatrix[0][2]+adjazenzMatrix[0][3]+" ");
  256. System.out.println(" "+adjazenzMatrix[1][0]+adjazenzMatrix[1][1]+adjazenzMatrix[1][2]+adjazenzMatrix[1][3]+" ");
  257. System.out.println(" "+adjazenzMatrix[2][0]+adjazenzMatrix[2][1]+adjazenzMatrix[2][2]+adjazenzMatrix[2][3]+" ");
  258. System.out.println(" "+adjazenzMatrix[3][0]+adjazenzMatrix[3][1]+adjazenzMatrix[3][2]+adjazenzMatrix[3][3]+" ");
  259. System.out.println(" ");
  260. System.out.println("-----------------------------");
  261. System.out.println("Knoten Eins");
  262. System.out.println("-----------------------------");
  263. while(temp.hasAccess())
  264. {
  265. System.out.println(" "+(String)temp.getObject());
  266. temp.next();
  267. }
  268. System.out.println("-----------------------------");
  269. System.out.println("Knoten Zwei");
  270. System.out.println("-----------------------------");
  271. while(temp2.hasAccess())
  272. {
  273. System.out.println(" "+(String)temp2.getObject());
  274. temp2.next();
  275. }
  276. System.out.println("-----------------------------");
  277. System.out.println("Knoten Drei");
  278. System.out.println("-----------------------------");
  279. while(temp3.hasAccess())
  280. {
  281. System.out.println(" "+(String)temp3.getObject());
  282. temp3.next();
  283. }
  284. System.out.println("-----------------------------");
  285. System.out.println("Knoten Vier");
  286. System.out.println("-----------------------------");
  287. while(temp4.hasAccess())
  288. {
  289. System.out.println(" "+(String)temp4.getObject());
  290. temp4.next();
  291. }
  292.  
  293. }
  294. else if(cmd.equals("Test4"))
  295. {
  296. sucheWege2(graph, "Eins", "Eins");
  297. System.out.println(gefunden + " ");
  298. }
  299.  
  300. }
  301.  
  302.  
  303.  
  304. /* while(!graph.getNodes().isEmpty())
  305. {
  306. if(graph.hasEdge(temp, (GraphNode)graph.getNodes().getObject()))
  307. {
  308. ausgabeListe.append((GraphNode)graph.getNodes().getObject());
  309. }
  310. graph.getNodes().remove();
  311. }*/
  312.  
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement