You simply need to change Tile tile = new Tile(y, x, f, Integer.parseInt(tokens[x]));
to
Tile tile = new Tile(x, y, f, Integer.parseInt(tokens[x]));
.
Everytime you're making a new Tile, you're switching x and y which is causing your problems.
문제
Recently I've been trying to make a little game for my learning purpose but I got stuck. I was able to generate a map from a .txt file but then it seems to paint it sideways?
this is my map loaded which reads from a text file and add them to an ArrayList. Width is 20 Height is 10. Same thing applies in my map 20 lines wide 10 down.
public void loadMap(String name) {
try {
this.name = name;
FileInputStream file_stream = new FileInputStream(name);
DataInputStream in = new DataInputStream(file_stream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
map = new Tile[WIDTH][HEIGHT];
String line;
int y = 0;
while ((line = br.readLine()) != null) {
String[] tokens = line.split(" ");
for (int x = 0; x < WIDTH; x++) {
boolean f = false;
if (Integer.parseInt(tokens[x]) == 1)
f = true;
Tile tile = new Tile(y, x, f, Integer.parseInt(tokens[x]));
tiles.add(tile);
map[x][y] = tile;
System.out.println("adding tile (x: " + x + " y: " + y + ") " + f + " " + Integer.parseInt(tokens[x]));
}
y++;
}
in.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "The map: " + name + " was not found.");
e.printStackTrace();
}
}
This is the draw method for the map
public void paint(Graphics2D g) {
for (Tile t : getTiles()) {
t.draw(g);
//g.draw(t.getBounds());
}
}
Here is an example map
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
The way it paints the map is basically a vertical version of this instead of the correct way like it is above. I have tried messing with the numbers within the mapLoaded (width, height) and switching the x and y around but nothing seems to work. Please help
해결책
You simply need to change Tile tile = new Tile(y, x, f, Integer.parseInt(tokens[x]));
to
Tile tile = new Tile(x, y, f, Integer.parseInt(tokens[x]));
.
Everytime you're making a new Tile, you're switching x and y which is causing your problems.