📄 mazegraphics.java
字号:
/* The MazeGraphics, when instantiated, creates a brand
new maze in which robots may be placed.
Use this file to instantiate mazes and draw robots, gold and walls
on them. This file does everything, screen-wise, in this respect! */
/* Abstract Programmer's Interface: here's how to use this class! :
create a new maze by declaring a variable of type MazeClass:
MazeGraphics maze = new MazeGraphics(<thisframe>, <width in cells>,
<height in cells>, <anchor_x>,
<anchor_y>)
use anchor to specify the exact x,y pixel coordinate in the frame
of the top left corner of the desired maze.
use addWall(<cell_x>,<cell_y>,<dir>) to specify new walls. <dir> is
a number from 0 to 3, with 0 being straight up (north) and 3 being
to the right (east)
use addGold() to add a gold piece. Gold that goes in a single cell
is specified using addGold(<cell_x>,<cell_y>). Gold that sits on the
border between two cells is specified via those two cells. (4 param's)
use addRobot(<cell_x>,<cell_y>,<dir>,<numgolds>) to specify the position
and direction of the robot. <cell_x>,<cell_y> are integers giving the
coordinates in the maze, <dir> is 0 for up, 1 for left, 2 for down, and 3 for right;
<numgolds> is 0,1,or 2 specifying the number of gold pieces in the robot.
use addSmoothRobot(<cell_x>,<cell_y>,<angle>,<numgolds>) to place
a robot in a non-grid-aligned position and orientation. This is useful for
displaying where within a cell the robot thinks it is. <cell_x>,<cell_y>
are doubles for this call, and the robot is centered in a cell when both are
integer values. <angle> is different for this call: it is a double representing
the angle, in radians, that the robot is at, with zero facing to the right, and
increasing counterclockwise.
refresh() or update() is used to redraw the maze. You must specify this call in
your window-redraw event handler: public void paint(Graphics g).
removeWall works the obvious way, as does removeGold, removeRobot, and
removeSmoothRobot.
also, don't forget removeAllRobots, removeAllGolds and removeAllWalls
*/
import java.awt.*;
import java.util.*;
//import java.lang.reflect.*;
import java.io.*;
public class MazeGraphics extends java.lang.Object
{
transient Frame parent_frame;
int anchor_x, anchor_y; // top left anchor point for maze
float magnification;
int width, height; // number of cells width & height of world //
int[][][] walls;
Vector golds; // vector of int[] for each gold position
Vector ports; // vector of int[] for each port position
Vector robots; // vector of int[] for each robot position //
int cellsize; // # of pixels width/height of a single cell //
int wall_thickness; // thickness of a wall
Vector smooth_robots; // vector of SmoothPoses for continuous robot poses
private class SmoothRobotPose
{
double x, y, angle;
int num_golds;
}
public MazeGraphics(Frame parent_frame, int width, int height,
int x, int y)
{
this.parent_frame = parent_frame;
this.anchor_x = x;
this.anchor_y = y;
this.magnification = (float)1.0;
this.cellsize = 40;
this.wall_thickness = 4;
this.width = width; this.height = height;
// by default, set walls to have no walls everywhere //
this.initializeWalls(this.width, this.height);
this.robots = new Vector();
this.golds = new Vector();
this.ports = new Vector();
this.smooth_robots = new Vector();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
refresh();
}
public void initializeWalls(int width, int height)
{
this.walls = new int[width][height][4];
for (int i=0; i < width; i++)
for (int j=0; j < height; j++)
for (int k=0; k < 4; k++)
this.walls[i][j][k] = 0;
// after zeroing everything, make the border 1's!!! //
for (int i=0; i < width; i++) {
this.walls[i][0][2] = 1;
this.walls[i][height - 1][0] = 1;
} // endfor
for (int j=0; j < height; j++) {
this.walls[0][j][1] = 1;
this.walls[width - 1][j][3] = 1;
} // endfor
}
// moves the maze to a new position in the world.
public boolean moveMaze(int new_x, int new_y)
{
// undraw everything! //
// get background color and make a filled rec
this.anchor_x = new_x;
this.anchor_y = new_y;
// now redraw everything! //
this.refresh();
return(true);
} // moveMaze() //
public boolean drawWalls()
{
for (int i=0; i < this.width; i++)
for (int j=0; j < this.height; j++) {
this.cellWallspec(i,j,this.walls[i][j]);
} // end for (int j... //
return(true);
} // drawWalls() //
public boolean drawRobots()
{
for (int i=0; i < this.robots.size(); i++) {
int[] robotpos = (int[])this.robots.elementAt(i);
drawRobot(robotpos,i);
} // end for //
for (int i=0; i < this.robots.size(); i++) {
int[] robotpos = (int[])this.robots.elementAt(i);
drawRobotLine(robotpos,i);
} // end for //
for (int i = 0; i < this.smooth_robots.size(); i++) {
drawSmoothRobot((SmoothRobotPose)this.smooth_robots.elementAt(i));
}
return(true);
} // drawRobots() //
public boolean drawRobots(int x, int y)
{
for (int i=0; i < this.robots.size(); i++) {
int[] robotpos = (int[])this.robots.elementAt(i);
if ((robotpos[0] == x) && (robotpos[1] == y))
drawRobot(robotpos,i);
} // end for //
for (int i=0; i < this.robots.size(); i++) {
int[] robotpos = (int[])this.robots.elementAt(i);
if ((robotpos[0] == x) && (robotpos[1] == y))
drawRobotLine(robotpos,i);
} // end for //
return(true);
} // drawRobots(x,y) //
public boolean drawPorts()
{
for (int i=0; i < this.ports.size(); i++) {
int[] portspec = (int[])this.ports.elementAt(i);
this.drawPort(portspec[0],portspec[1]);
} // endfor
return(true);
} //drawPorts() //
public boolean drawGolds()
{
for (int i=0; i < this.golds.size(); i++) {
int[] goldspec = (int[])this.golds.elementAt(i);
if (goldspec.length == 2) {
this.drawGold(goldspec[0],goldspec[1]);
} else if (goldspec.length == 4) {
this.drawGold(goldspec[0],goldspec[1],
goldspec[2],goldspec[3]);
} else return(false);
} // endfor
return(true);
} //drawGolds() //
public boolean drawGolds(int x, int y)
{
for (int i=0; i < this.golds.size(); i++) {
int[] goldspec = (int[])this.golds.elementAt(i);
if (goldspec.length == 2) {
if ((goldspec[0] == x) && (goldspec[1] == y))
this.drawGold(goldspec[0],goldspec[1]);
} else if (goldspec.length == 4) {
if (((goldspec[0] == x) && (goldspec[1] == y)) ||
((goldspec[2] == x) && (goldspec[3] == y)))
this.drawGold(goldspec[0],goldspec[1],
goldspec[2],goldspec[3]);
} else return(false);
} // endfor
return(true);
} //drawGolds(x,y) //
//takes a cell (x,y) and a direction and adds a wall there//
public boolean addWall(int cell_x, int cell_y, int dir)
{
this.walls[cell_x][cell_y][dir] = 1;
// add the symmetric cases if such a cell exists //
int sym_x, sym_y, sym_dir;
sym_dir = (dir + 2) % 4;
if (dir == 0) {
sym_x = cell_x; sym_y = cell_y + 1;
} else if (dir == 1) {
sym_x = cell_x - 1; sym_y = cell_y;
} else if (dir == 2) {
sym_x = cell_x; sym_y = cell_y - 1;
} else {
sym_x = cell_x + 1; sym_y = cell_y;
}
if ((sym_x >= 0) && (sym_y >= 0) &&
(sym_x < this.width) && (sym_y < this.height))
this.walls[sym_x][sym_y][sym_dir] = 1;
drawWall(cell_x, cell_y, dir);
return(true);
} // addWall() //
public boolean removeAllWalls()
{
this.initializeWalls(this.width, this.height);
return(true);
} // removeAllWalls() //
public boolean removeWall(int cell_x, int cell_y, int dir)
{
// do not remove any border walls! new -- 10/12/98 illah //
if ((cell_x == 0) && (dir == 1)) return(false);
if ((cell_y == 0) && (dir == 2)) return(false);
if ((cell_x == (this.width - 1)) && (dir == 3)) return(false);
if ((cell_y == (this.height - 1)) && (dir == 0)) return(false);
this.walls[cell_x][cell_y][dir] = 0;
// add the symmetric cases if such a cell exists //
int sym_x, sym_y, sym_dir;
sym_dir = (dir + 2) % 4;
if (dir == 0) {
sym_x = cell_x; sym_y = cell_y + 1;
} else if (dir == 1) {
sym_x = cell_x - 1; sym_y = cell_y;
} else if (dir == 2) {
sym_x = cell_x; sym_y = cell_y - 1;
} else {
sym_x = cell_x + 1; sym_y = cell_y;
}
if ((sym_x >= 0) && (sym_y >= 0) &&
(sym_x < this.width) && (sym_y < this.height))
this.walls[sym_x][sym_y][sym_dir] = 0;
eraseWall(cell_x, cell_y, dir);
return(true);
} // removeWall() //
// The display is automatically refreshed at the end of this call.
public boolean addRobot(int x, int y, int dir, int numgolds)
{
int[] robotpos = new int[4];
robotpos[0] = x; robotpos[1]=y; robotpos[2]=dir;
robotpos[3] = numgolds;
int Counter = 0;
for (int Index = 0; Index < robots.size(); Index++) {
int [] tempRobot = (int [])robots.elementAt(Index);
if (tempRobot[0] == x && tempRobot[1] == y)
Counter++;
}
this.robots.addElement(robotpos);
refresh();
return(true);
} // addRobot() //
// Add a smoothly-positioned robot to the display. When x and y have integer
// values, the robot is centered in a cell, the same as with addRobot().
// Unlike addRobot(), the angle parameter is 0 for East (right), and increases
// counterclockwise in radians. North is Math.PI/2, West is Math.PI, etc.
public void addSmoothRobot(double x, double y, double angle, int num_golds)
{
SmoothRobotPose pose = new SmoothRobotPose();
pose.x = x;
pose.y = y;
pose.angle = angle;
pose.num_golds = num_golds;
this.smooth_robots.addElement(pose);
drawSmoothRobot(pose);
}
// Remove a robot smoothly-positioned robot from the display. All parameters
// must match *exactly* those of a previously added robot in order to delete it.
// There is no epsilon position or angle distance tolerance.
// The display is automatically refreshed at the end of this call.
public void removeSmoothRobot(double x, double y, double angle, int num_golds)
{
boolean removed = false;
for(int i = 0; i < this.smooth_robots.size(); i++) {
SmoothRobotPose pose = (SmoothRobotPose) this.smooth_robots.elementAt(i);
if(pose.x == x &&
pose.y == y &&
pose.angle == angle &&
pose.num_golds == num_golds) {
this.smooth_robots.removeElementAt(i);
removed = true;
}
}
if(removed)
refresh();
}
// The display is automatically refreshed at the end of this call.
public boolean removeAllRobots()
{
this.robots = new Vector();
this.smooth_robots = new Vector();
refresh();
return(true);
} // removeAllRobots()
public boolean removeRobot(int x, int y, int dir, int golds)
{
boolean removed = false;
for (int i=0; i < this.robots.size(); i++) {
int[] robotpos = (int [])(this.robots.elementAt(i));
if ((robotpos[0] == x) && (robotpos[1] == y) &&
(robotpos[2] == dir) && (robotpos[3] == golds)) {
this.robots.removeElementAt(i);
removed = true;
} // endif
} // endfor
if(removed) {
refresh();
return(true);
}
else {
return(false);
}
} // removeRobot()
public boolean addPort(int x, int y)
{
int[] portpos = new int[2];
portpos[0] = x; portpos[1] = y;
this.ports.addElement(portpos);
drawPort(x, y);
return(true);
} // addPort() //
public boolean addGold(int x, int y)
{
int[] goldpos = new int[2];
goldpos[0] = x; goldpos[1] = y;
this.golds.addElement(goldpos);
drawGold(x, y);
return(true);
} // addGold() //
public boolean addGold(int x1, int y1, int x2, int y2)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -