Necesito ayuda para dibujar un grafo a partir de su matriz de adyacencia
Publicado por Miguel Paz (8 intervenciones) el 09/09/2020 02:47:57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// lo que llevo me imprime los vertices dependiendo de la matriz que le entra, pero tengo el inconveniente que //algunos me quedan uno encima de otro y no sé como unirlos por medio de las lineas
//agradeceria mucho me echaran una mano con el codigo o que me dieran una pista de como hacer. muchas //gracias
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;
/**
*
* @author Miguel Angel
*/
public class dibujarGrafo extends JFrame {
Container contenedor;
FlowLayout layout;
static int longitud;
public dibujarGrafo(){
super("Grafos");
contenedor = getContentPane();
layout = new FlowLayout();
contenedor.setLayout(layout);
setSize(300,300);
setVisible(true);
}
public void paint( Graphics g )
{
// g.drawOval(100,100,50,50);
for (int i = 0; i < longitud; i++) {
g.drawOval((int) (Math.random()*250), (int) (Math.random()*250) , 30, 30);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
int[][] matriz;
System.out.println(" Ingrese el nombre del archivo");
Scanner scanner = new Scanner (System.in);
String archivo = scanner.next();
BufferedReader br = new BufferedReader(new FileReader(archivo));
//Primera linea nos dice longitud de la matriz
String linea = br.readLine();
longitud = Integer.parseInt(linea);
matriz = new int[longitud][longitud];
//Las siguientes lineas son filas de la matriz
linea = br.readLine();
int fila = 0; //Para recorrer las filas de la matriz
while(linea != null) {
/*
* Tenemos todos los enteros JUNTOS en el String linea.
* Con split() los SEPARAMOS en un array donde cada entero
* es un String individual. Con un bucle, los parseamos a Integer
* para guardarlos en la matriz
*/
String[] enteros = linea.split(" ");
for (int i = 0; i < enteros.length; i++)
matriz[fila][i] = Integer.parseInt(enteros[i]);
fila++; //Incrementamos fila para la próxima línea de enteros
linea = br.readLine(); //Leemos siguiente línea
}
br.close(); //Cerramos el lector de ficheros
//Mostramos la matriz leída
for (int i = 0; i < longitud; i++) {
for (int j = 0; j < longitud; j++)
System.out.print(matriz[i][j] + " ");
System.out.println();
}
dibujarGrafo canvaz = new dibujarGrafo();
canvaz.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch (FileNotFoundException e) {
System.out.println("No se encuentra archivo");
e.printStackTrace();
} catch (NumberFormatException e) {
System.out.println("No se pudo convertir a entero");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Error accediendo al archivo.");
e.printStackTrace();
}
}
}
Valora esta pregunta


0