-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMaze2PNG.java
71 lines (65 loc) · 2.1 KB
/
Maze2PNG.java
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
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Maze2PNG {
static public void main(String args[]) throws Exception {
System.out.println("Starting ...");
if (args.length == 2) {
try {
// read the maze file created from MST program with -m option
BufferedReader br = new BufferedReader(new FileReader(args[0]));
String mazeString = "";
String line = "";
while ((line = br.readLine()) != null) {
if (line.charAt(0) == ' ' || line.charAt(0) == '|'
|| line.charAt(0) == '+' || line.charAt(0) == '-') {
// only use lines containing the maze
mazeString += line + "\n";
}
}
br.close();
String[] mazeArray = mazeString.split("\n");
// +2 for padding
int width = mazeArray[0].length() + 2;
int height = mazeArray.length + 2;
// format: 8-bit rgba integer pixels
BufferedImage mazeImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
for (int row = 0; row < height; row++) {
for (int column = 0; column < width; column++) {
if (row == 0 || column == 0 || row == height - 1
|| column == width - 1) {
// black padding
mazeImage.setRGB(column, row, Color.BLACK.getRGB());
} else {
// actual mace
if (mazeArray[row - 1].charAt(column - 1) == ' ') {
// space white
mazeImage.setRGB(column, row,
Color.BLACK.getRGB());
} else {
// all other black
mazeImage.setRGB(column, row,
Color.WHITE.getRGB());
}
}
}
}
ImageIO.write(mazeImage, "PNG", new File(args[1]));
System.out.println("Finished ...");
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.err
.println("\nYou need to pass two arguments.\n"
+ "First argument is the text file, e. g. /home/user/maze.txt.\n"
+ "Second argument is the PNG file to store the image, e. g. /home/user/maze.png.\n");
}
System.out.println("Exiting ...");
}
}