Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. package org.jgrapht.jgrapht_core;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Scanner;
  9.  
  10. import javax.swing.JFrame;
  11.  
  12. import com.mxgraph.swing.mxGraphComponent;
  13. import com.mxgraph.view.mxGraph;
  14.  
  15. public class tp_graphes extends JFrame {
  16.  
  17. /** Pour éviter un warning venant du JFrame */
  18. private static final long serialVersionUID = -8123406571694511514L;
  19.  
  20. public int nbsommets;
  21. static List <String> mon_fichier = new ArrayList <String> ();
  22.  
  23.  
  24.  
  25. public static void lecture_fichier ()
  26. {
  27. String pathFichier = "C:\\Users\\Timothée\\Desktop\\fichier_graphes.txt";
  28. BufferedReader fluxEntree = null;
  29. String line;
  30. // String[] str = null;
  31. int i,j,x = 0,y=0;
  32. String sommmetDep[] = null;
  33. String sommetArr[] =null ;
  34. int poids[] = null;
  35.  
  36.  
  37. try {
  38. //Creation du flux vers le fichier texte utilise
  39. fluxEntree = new BufferedReader (new FileReader (pathFichier));
  40.  
  41. //Lecture d'une ligne qu'on ajoute à la liste
  42. while (( line = fluxEntree.readLine()) != null)
  43. {
  44. String ligneLue = fluxEntree.readLine();
  45. //ligneLue = fluxEntree.readLine();
  46.  
  47. mon_fichier.add(ligneLue);
  48.  
  49. for (i=0; i<mon_fichier.size(); i++) //lecture du nb de lignes
  50. {
  51. int k=2;
  52. int m=0;
  53. String[] str = ligneLue.split(" ");
  54. //for (j=0; j<str.length; j++) //lecture du nb de mots sur la ligne
  55. // {
  56.  
  57. if (str[0].equals("sommet")) //Methoque equals ? -> str[0].equals("sommet")
  58. {
  59. x += 20;
  60. y += 20;
  61. // Object v1 = graph.insertVertex(parent, null, str[j+1], x, y, 80, 30);
  62. }
  63.  
  64. if (str[0].equals("arcs"))
  65. {
  66. sommmetDep[0] = str[1];
  67.  
  68. while (str[k] != null)
  69. {
  70. if (str[k].charAt(0) == 's') {
  71. sommetArr[m] = str[k];
  72. }
  73. else
  74. {
  75. poids[n] = str[k];
  76. }
  77. //Reparcourir les tableaux pour créer l'arc
  78. // graph.insertEdge(parent, null, "3.0", v2, v1);
  79. }
  80.  
  81.  
  82. }
  83.  
  84. //}
  85. }
  86. }
  87.  
  88. }
  89.  
  90. catch (IOException e)
  91. {
  92. e.printStackTrace();
  93. }
  94. finally {
  95. try {
  96. if (fluxEntree != null)
  97. {
  98. fluxEntree.close();
  99. }
  100. }
  101. catch (IOException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105.  
  106.  
  107.  
  108.  
  109. }//Fin de la fonction
  110.  
  111.  
  112. public tp_graphes() {
  113. super("JGrapghX TP Langages et Grammaires GRISLIN");
  114. int ligne = 0;
  115. int i = 0;
  116. String mot_sommet = "sommet";
  117. String mot_arc = "arc";
  118. int x =0,y =20;
  119.  
  120. mxGraph graph = new mxGraph();
  121. Object parent = graph.getDefaultParent();
  122.  
  123. graph.getModel().beginUpdate();
  124. try {
  125.  
  126. /* while ((ligne - 1) != mon_fichier.get(ligne).length()) {
  127.  
  128. if (mot_sommet.equals("sommet"))
  129. {
  130. x += 20; //Decaler la position d'un sommet à chaque création
  131. y += 30;
  132. Object v1 = graph.insertVertex(parent, null, "s1", x, y, 80, 30); //80&30 rpz taille de la case
  133. }
  134.  
  135. if (mot_arc.equals("arc"))
  136. {
  137. graph.insertEdge(parent, null, "3.0", v2, v1);
  138. }
  139. }
  140. */
  141.  
  142. Object v1 = graph.insertVertex(parent, null, "s3", 20, 20, 80, 30);
  143. Object v2 = graph.insertVertex(parent, null, "s1", 240, 150, 80, 30);
  144. Object v3 = graph.insertVertex(parent, null, "s2", 20, 150, 80, 30);
  145. graph.insertEdge(parent, null, "3.0", v2, v1);
  146. graph.insertEdge(parent, null, "2.0", v3, v1);
  147. graph.insertEdge(parent, null, "5.0", v2, v3);
  148. graph.insertEdge(parent, null, "10.0", v3, v2);
  149. graph.insertEdge(parent, null, "0.0", v3, v3);
  150.  
  151. } finally {
  152. graph.getModel().endUpdate();
  153. }
  154.  
  155. mxGraphComponent graphComponent = new mxGraphComponent(graph);
  156. getContentPane().add(graphComponent);
  157. }
  158.  
  159. /**
  160. * @param args
  161. */
  162. public static void main(String[] args) {
  163.  
  164. Scanner sc = new Scanner (System.in);
  165.  
  166. lecture_fichier();
  167. tp_graphes frame = new tp_graphes();
  168.  
  169.  
  170.  
  171. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  172. frame.setSize(400, 320);
  173. frame.setVisible(true);
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement