📄 origmazegraphics.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. <numgolds> is 0,1,or 2 specifying the
number of gold pieces in the robot.
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 and removeRobot.
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
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();
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 //
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() //
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);
drawRobot(robotpos, Counter);
return(true);
} // addRobot() //
public boolean removeAllRobots()
{
this.robots = new Vector();
refresh();
return(true);
} // removeAllRobots()
public boolean removeRobot(int x, int y, int dir, int golds)
{
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);
eraseRobotCircle(x, y);
return(true);
} // endif
} // endfor
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)
{
int[] goldpos = new int[4];
goldpos[0]=x1; goldpos[1]=y1; goldpos[2]=x2; goldpos[3]=y2;
this.golds.addElement(goldpos);
drawGold(x1, y1, x2, y2);
return(true);
} // addGold(2)()
public boolean removeAllGolds()
{
this.golds = new Vector();
refresh();
return(true);
} // removeAllGolds() //
public boolean removePort(int x, int y)
{
return(this.removePort(x,y,this.ports));
} // removePort() //
//this one takes a vector
public boolean removePort(int x, int y, Vector daports)
{
for (int i=0; i < daports.size(); i++) {
int[] portpos = (int[])(daports.elementAt(i));
if ((portpos[0] == x) && (portpos[1] == y)) {
erasePortPos(x, y);
daports.removeElementAt(i);
return(true);
} //endif
} //endfor
return(false);
} // removePort() //
public boolean removeGold(int x, int y)
{
return(this.removeGold(x,y,this.golds));
} // removeGold() //
//this one takes a vector
public boolean removeGold(int x, int y, Vector dagolds)
{
for (int i=0; i < dagolds.size(); i++) {
int[] goldpos = (int[])(dagolds.elementAt(i));
if ((goldpos.length == 2) &&
(goldpos[0] == x) && (goldpos[1] == y)) {
eraseGoldPos(x, y);
dagolds.removeElementAt(i);
return(true);
} //endif
} //endfor
return(false);
} // removeGold() //
public boolean hasGold(int x, int y)
{
for (int i=0; i < this.golds.size(); i++) {
int[] goldpos = (int[])(this.golds.elementAt(i));
if ((goldpos.length == 2) &&
(goldpos[0] == x) && (goldpos[1] == y)) {
return(true);
} // endif
} // endfor
return(false);
} // hasGold() //
public boolean removeGold(int x1, int y1, int x2, int y2)
{
return(this.removeGold(x1,y1,x2,y2,this.golds));
} // removeGold() //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -