Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.jgrapht.jgrapht_core;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import javax.swing.JFrame;
- import com.mxgraph.swing.mxGraphComponent;
- import com.mxgraph.view.mxGraph;
- public class tp_graphes extends JFrame {
- /** Pour éviter un warning venant du JFrame */
- private static final long serialVersionUID = -8123406571694511514L;
- public int nbsommets;
- static List <String> mon_fichier = new ArrayList <String> ();
- public static void lecture_fichier ()
- {
- String pathFichier = "C:\\Users\\Timothée\\Desktop\\fichier_graphes.txt";
- BufferedReader fluxEntree = null;
- String line;
- // String[] str = null;
- int i,j,x = 0,y=0;
- String sommmetDep[] = null;
- String sommetArr[] =null ;
- int poids[] = null;
- try {
- //Creation du flux vers le fichier texte utilise
- fluxEntree = new BufferedReader (new FileReader (pathFichier));
- //Lecture d'une ligne qu'on ajoute à la liste
- while (( line = fluxEntree.readLine()) != null)
- {
- String ligneLue = fluxEntree.readLine();
- //ligneLue = fluxEntree.readLine();
- mon_fichier.add(ligneLue);
- for (i=0; i<mon_fichier.size(); i++) //lecture du nb de lignes
- {
- int k=2;
- int m=0;
- String[] str = ligneLue.split(" ");
- //for (j=0; j<str.length; j++) //lecture du nb de mots sur la ligne
- // {
- if (str[0].equals("sommet")) //Methoque equals ? -> str[0].equals("sommet")
- {
- x += 20;
- y += 20;
- // Object v1 = graph.insertVertex(parent, null, str[j+1], x, y, 80, 30);
- }
- if (str[0].equals("arcs"))
- {
- sommmetDep[0] = str[1];
- while (str[k] != null)
- {
- if (str[k].charAt(0) == 's') {
- sommetArr[m] = str[k];
- }
- else
- {
- poids[n] = str[k];
- }
- //Reparcourir les tableaux pour créer l'arc
- // graph.insertEdge(parent, null, "3.0", v2, v1);
- }
- }
- //}
- }
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- finally {
- try {
- if (fluxEntree != null)
- {
- fluxEntree.close();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }//Fin de la fonction
- public tp_graphes() {
- super("JGrapghX TP Langages et Grammaires GRISLIN");
- int ligne = 0;
- int i = 0;
- String mot_sommet = "sommet";
- String mot_arc = "arc";
- int x =0,y =20;
- mxGraph graph = new mxGraph();
- Object parent = graph.getDefaultParent();
- graph.getModel().beginUpdate();
- try {
- /* while ((ligne - 1) != mon_fichier.get(ligne).length()) {
- if (mot_sommet.equals("sommet"))
- {
- x += 20; //Decaler la position d'un sommet à chaque création
- y += 30;
- Object v1 = graph.insertVertex(parent, null, "s1", x, y, 80, 30); //80&30 rpz taille de la case
- }
- if (mot_arc.equals("arc"))
- {
- graph.insertEdge(parent, null, "3.0", v2, v1);
- }
- }
- */
- Object v1 = graph.insertVertex(parent, null, "s3", 20, 20, 80, 30);
- Object v2 = graph.insertVertex(parent, null, "s1", 240, 150, 80, 30);
- Object v3 = graph.insertVertex(parent, null, "s2", 20, 150, 80, 30);
- graph.insertEdge(parent, null, "3.0", v2, v1);
- graph.insertEdge(parent, null, "2.0", v3, v1);
- graph.insertEdge(parent, null, "5.0", v2, v3);
- graph.insertEdge(parent, null, "10.0", v3, v2);
- graph.insertEdge(parent, null, "0.0", v3, v3);
- } finally {
- graph.getModel().endUpdate();
- }
- mxGraphComponent graphComponent = new mxGraphComponent(graph);
- getContentPane().add(graphComponent);
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- Scanner sc = new Scanner (System.in);
- lecture_fichier();
- tp_graphes frame = new tp_graphes();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(400, 320);
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement