Read text file into array and display it
Text file
What I want to do is put the whole text file into an array with 2 keys, where the first key corresponds with the horizontal location and where the second key corresponds with the vertical location in the text file.
Raw array
Quote:
array[0][0] = 1
array[1][0] = 1
array[2][0] = 1
array[0][1] = 1
array[1][1] = 0
array[2][1] = 1
array[0][2] = 1
array[1][2] = 1
array[2][2] = 1
As the text file is meant to be altered, it wouldn't be a good idea to do this manually.
After having created the array, I'd like to recreate the text file, but now every 0 should become a blue box and every 1 a red one. Right now this thread does indeed sound like a request for a finished system, but the previous information was just to tell you what I'd like to do with it so you'll get to understand the next part better. Or maybe you know an even better way of doing this stuff.
---
The contents of the text file can be found at the beginning of the file. It wouldn't be possible for me to write the contents of the java file, as I don't know how to do this. What I do know will be written in Java and what I don't will be written as a comment.
Code:
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
public class main extends JFrame {
public main() {
// Creating frame
super("Map");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(30, 30);
setVisible(true);
int y = 0;
int x = 0;
/* read and open file
int width = get length of first line
int height = get amount of lines */
while(y < height) {
while(x < width) {
array[width][height] = // character at this location
x++;
}
y++;
}
}
public void paint(Graphics g) {
y = 0;
x = 0;
while(y < height) {
while(x < width) {
g.drawRect(x*10, y*10, 10, 10);
if(array[width][height] == 1) {
g.setColor(Color.BLACK);
}
g.fillRect(x*10, y*10, 400, 400);
x++;
}
y++;
}
}
public static void main(String[]args) {
main body = new main();
}
}
I hope you understand what I'm trying to do. If not, please ask a question.
Re: Read text file into array and display it
Just ignore this thread. I'll try figuring this out myself.