mazebuildertest.java
来自「一个用JAVA语言编写的迷宫程序,可以实现迷宫的行走」· Java 代码 · 共 122 行
JAVA
122 行
/**
*
*/
package test.org.freemind.maze2d;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.freemind.maze2d.Maze;
import org.freemind.maze2d.MazeBuilder;
import org.freemind.maze2d.Room;
/**
* @author Jerric
* @created Feb 9, 2006
*/
public class MazeBuilderTest {
private File resultDir;
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("Usage: run <width> <height>");
System.exit(-1);
}
// get width & height
int width = Integer.parseInt(args[0]);
int height = Integer.parseInt(args[1]);
if (width <= 0) width = 20;
if (height <= 0) height = 20;
MazeBuilderTest tester = new MazeBuilderTest();
tester.testCreateMaze(width, height);
}
public MazeBuilderTest() {
// load result dir
if (System.getProperties().containsKey("resultDir")) {
resultDir = new File(System.getProperty("resultDir"));
} else resultDir = new File("./");
if (!resultDir.exists() || !resultDir.isDirectory())
resultDir.mkdirs();
}
/*
* Test method for 'org.freemind.maze2d.MazeBuilder.createMaze(int, int)'
*/
public void testCreateMaze(int width, int height) {
System.out.println("MazeBuilderTest.testCreateMaze()");
MazeBuilder builder = new MazeBuilder(System.currentTimeMillis());
// create maze
long start = System.currentTimeMillis();
Maze maze = builder.createMaze(width, height);
long end = System.currentTimeMillis();
// save the maze image
System.out.println("\tSaving the image");
String name = "maze_" + width + "_" + height + ".png";
File file = new File(resultDir, name);
try {
saveMaze(file, maze);
} catch (IOException ex) {
// TODO Auto-generated catch block
// ex.printStackTrace();
System.err.println(ex);
}
System.out.println("\tCreated " + width + "*" + height + " maze in "
+ ((end - start) / 1000D) + " seconds.");
}
private void saveMaze(File file, Maze maze) throws IOException {
int roomSize = 14;
int wallThick = 3;
int doorSize = roomSize - 2 * wallThick;
int width = maze.getWidth();
int height = maze.getHeight();
BufferedImage image = new BufferedImage(width * roomSize, height
* roomSize, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// draw each room
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int sx = x * roomSize;
int sy = y * roomSize;
// draw walls
g.setColor(new Color(75, 60, 30));
g.fillRect(sx, sy, roomSize, roomSize);
// draw room
g.setColor(new Color(255, 255, 0));
g.fillRect(sx + wallThick, sy + wallThick, roomSize - 2
* wallThick, roomSize - 2 * wallThick);
// draw west door, if have
Room room = maze.getRoom(x, y);
if (room.isWestOpen())
g.fillRect(sx, sy + wallThick, wallThick, doorSize);
// draw north door
if (room.isNorthOpen())
g.fillRect(sx + wallThick, sy, doorSize, wallThick);
// draw east door
if (room.isEastOpen())
g.fillRect(sx + roomSize - wallThick, sy + wallThick,
wallThick, doorSize);
// draw south door
if (room.isSouthOpen())
g.fillRect(sx + wallThick, sy + roomSize - wallThick,
doorSize, wallThick);
}
}
// save the image
ImageIO.write(image, "PNG", file);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?