lectura de archivos en interfaz
Publicado por alba (4 intervenciones) el 16/07/2021 17:24:50
Tengo el siguiente código y necesito que me lo muestre en la interfaz, pero me da estos errores:
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at View.VentanaLaberinto.dibujarLaberinto(VentanaLaberinto.java:26)
at Control.Sistema.main(Sistema.java:50)
26: int ancho = s.laberinto.maze[0].length;
50: v.dibujarLaberinto(sistema); //Paso Sistema para que luego la ventana encuentre el laberinto a dibujar
package Model;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.JFileChooser;
import java.awt.image.ImageObserver;
import Control.ControladorPersonajes;
import Control.Sistema;
public class Laberinto {
public static PrintWriter stdOut = new PrintWriter(System.out, true);
public static PrintWriter stdErr = new PrintWriter(System.err, true);
public int anchoLaberinto;
public String maze[][];
public Vector<Personaje> listaGatos = new Vector <Personaje> ();
public Personaje raton;
public Personaje Gato;
public ControladorPersonajes cp;
public ImageObserver raton1;
public ImageObserver gato1;
public ImageObserver muro5;
public void addGato (Personaje p) {
listaGatos.add(p);
p.laberinto=this;
}
public void addRaton(Raton r) {
// TODO Auto-generated method stub
this.raton=r;
r.laberinto=this;
}
public static int contar_Lineas(String ruta) {
int lineas = 0;
File fichero = new File(ruta);
try {
Scanner sc = new Scanner(fichero);
while (sc.hasNextLine()) {
sc.nextLine();
lineas++;
}
sc.close();
} catch (FileNotFoundException e) {
System.out.println("ERROR: No se puede visualizar el fichero.");
}
return lineas;
}
public static int contar_Columnas (String ruta) {
int columnas = 0;
String fila = "0";
File FicheroLaberinto= new File(ruta);
try {
Scanner sc = new Scanner(FicheroLaberinto);
while (sc.hasNextLine()) {
fila = sc.nextLine();
}
sc.close();
} catch (FileNotFoundException e) {
System.out.println("ERROR: No se puede visualizar el fichero.");
}
columnas = fila.split(";").length;
return columnas;
}
public static String[][] importarDatos (String ruta) {
String[][] datos = {};
File fichero = new File(ruta);
/*Scanner sc = new Scanner(fichero);
datos = new String[contar_Lineas(ruta)][contar_Columnas(ruta)];
int i = 0;
String fila = "0";
String[] array_fila = {};
while (sc.hasNextLine()) {
fila = sc.nextLine();
array_fila = fila.split(";");
for (int j = 0; j < array_fila.length; j++) {
datos[i][j] = array_fila[j];
}
i++;
}
sc.close();*/
Scanner entrada = null;
JFileChooser fileChooser = new JFileChooser();
int valor = fileChooser.showOpenDialog(fileChooser);
if (valor == JFileChooser.APPROVE_OPTION) {
String ruta1 = fileChooser.getSelectedFile().getAbsolutePath();
try {
File v = new File(ruta1);
entrada = new Scanner(v);
while (entrada.hasNext()) {
System.out.println(entrada.nextLine());
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} finally {
if (entrada != null) {
entrada.close();
}
}
}
return datos;
}
public void crearLaberinto (int anchoLaberinto){
this.anchoLaberinto = anchoLaberinto;
maze = importarDatos("fileChooser");
}
public void mostrarLaberinto(){
for (int f=0; f<maze.length; f++) {
for (int c=0; c<maze[f].length; c++) {
System.out.print(maze [f][c]);
}
System.out.println();
}
}
public void mover_Personajes () {
raton.mover();
for (Personaje p: this.listaGatos) {
p.mover();
}
}
public void dibujar_Personajes () {
raton.dibujar();
for (Personaje p: this.listaGatos) {
p.dibujar();
}
}
public boolean encontradaSalida () {
return (raton.x==(Sistema.ANCHO_LABERINTO-1) && raton.y==(Sistema.ANCHO_LABERINTO-1));
}
public boolean ratonComido () {
boolean comido=false;
for (Personaje g: this.listaGatos) {
comido = (comido || (raton.x==g.x && raton.y==g.y));
}
return comido;
}
}
Valora esta pregunta


0