public static void main(String[] args) throws IOException {
JFileChooser file = new JFileChooser();
file.setDialogTitle("Where is your Rat Race map ?");
if (file.showDialog(null,"OK") == JFileChooser.APPROVE_OPTION) {
File f = file.getSelectedFile();
BufferedReader read = new BufferedReader(new FileReader(f));
//determine the dimensions of the character array
String lineRead = "";
int rows = 0;
int columns = 0;
while ((lineRead = read.readLine())!=null) {
columns = lineRead.length();
rows++;
}
//put the characters in the map file into a 2d array of characters
String temp = "";
BufferedReader br = new BufferedReader(new FileReader(f));
char[][] myMap = new char[rows][columns];
for (int r = 0; r < rows; r++) {
while ((temp=br.readLine())!= null) {
for (int c = 0; c < columns ; c++) {
myMap[r][c] = temp.charAt(c);
}
}
}
JFrame window = new JFrame("Rat Race");
window.getContentPane().setLayout(new GridLayout(map.length, map[0].length));
JLabel[][]label = new JLabel[map.length][map[0].length];
for (int r = 0; r < map.length; r++) {
for (int c = 0; c < map[0].length ; c++) {
label[r][c] = new JLabel();
label[r][c].setText(Character.toString(map[r][c]));
window.getContentPane().add(label[r][c]);
}
}
window.pack();
window.show();
}
}
Im using Dr Java. When I call the main method in RatRace, by
java RatRace
a window will show up, prompting me for a map file with the content depicting a maze. After I choose that file, Iwant its content to appear in
a JFrame window. That is, I want to have sth like this in
the JFrame window called "Rat Race"
hhmmzz.. very strange, seems that most of the time charAt(c) returns one character, which is as it should be. and sometimes it returns an entire string of characters....
try changing it from
Code:
for (int r = 0; r < rows; r++) {
while ((temp=br.readLine())!= null) {
for (int c = 0; c < columns ; c++) {
myMap[r][c] = temp.charAt(c);
}
}
to
Code:
int line = 0;
while ((temp=br.readLine())!= null) {
for (int c = 0; c < temp.length ; c++) {
myMap[line][c] = temp.charAt(c);
}
line++;
}
This takes the file as l;eading instead of the array.
java.lang.ArrayIndexOutOfBoundsException: 23
at RatRace.main(RatRace.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
It seems so weird to me. I think my idear is correct. Set a grid layout to a JFrame window, bring in a map file which contains of characters, put those characters in to an array. Pass that array to the constructor of the MapWindow. Cant explain why it doesnt work.
This works. I have modified your code it a bit; used an ArrayList of bytearrays to hold the lines instead of reading the file twice (and the FileDialog since this one remembers the last map visisted).
One more thing, using labels to draw is not the optimal solution, but you're probably aware of that.
public static void main(String[] args) throws IOException {
FileDialog fd=new FileDialog(new JFrame(),"Where is your ratrace map ?",FileDialog.LOAD);
fd.setVisible(true);
String fileName=fd.getFile();
if (fd==null) return; // user bailed out
String filePath=fd.getDirectory()+fileName;
BufferedReader read = new BufferedReader(new FileReader(filePath));
//get the mapfile's lines into an arraylist
String lineRead = "";
ArrayList myMap=new ArrayList();
int maxWidth=Integer.MIN_VALUE;
int lineCount=0;
while ((lineRead = read.readLine())!=null) {
lineCount++;
byte[] b=lineRead.trim().getBytes();
if (maxWidth > 0 && b.length != maxWidth) {
JOptionPane.showMessageDialog(
null,"A RatRace Maze must be a rectangle, \r\nline: "+
lineCount+" in "+filePath+" differs in length ("+
maxWidth+" != "+b.length+")", "you goofed",JOptionPane.ERROR_MESSAGE);
read.close();
return;
}
maxWidth=(maxWidth < b.length) ? b.length : maxWidth;
myMap.add(b);
}
read.close();
MapWindow window = new MapWindow(myMap, maxWidth);
}
}
class MapWindow extends JFrame {
public MapWindow(ArrayList map, int mapWidth){
JFrame window = new JFrame("Rat Race");
window.getContentPane().setLayout(new GridLayout(map.size(), mapWidth));
JLabel[][]label = new JLabel[map.size()][mapWidth];
for (int r = 0; r < map.size(); r++) {
for (int c = 0; c < mapWidth ; c++) {
label[r][c] = new JLabel();
byte b=((byte[])map.get(r))[c];
label[r][c].setText(Character.toString((char)b));
window.getContentPane().add(label[r][c]);
}
}
window.pack();
window.show();
}
}