📄 mazegraphics.java
字号:
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() //
public boolean removeGold(int x1, int y1, int x2, int y2, Vector dagolds)
{
for (int i=0; i < dagolds.size(); i++) {
int[] goldpos = (int[])(dagolds.elementAt(i));
if ((goldpos.length == 4) &&
(((goldpos[0] == x1) && (goldpos[1] == y1) &&
(goldpos[2] == x2) && (goldpos[3] == y2)) ||
((goldpos[0] == x2) && (goldpos[1] == y2) &&
(goldpos[2] == x1) && (goldpos[3] == y1))))
{
dagolds.removeElementAt(i);
eraseGoldPos(xCell2Pixel(x1), xCell2Pixel(y1), xCell2Pixel(x2), xCell2Pixel(y2));
if (x1 == x2 && y1 == y2-1)
eraseWall(x1, y1, 0);
else if (x1 == x2 && y1 == y2+1)
eraseWall(x1, y1, 2);
else if (y1 == y2 && x1 == x2 -1)
eraseWall(x1, y1, 3);
else if (y1 == y2 && x1 == x2 + 1)
eraseWall(x1, y1, 1);
return(true);
} //endif
} //endfor
return(false);
} // removeGold()2 //
public int[] findOtherGoldCell(int x, int y)
{
return(this.findOtherGoldCell(x,y,this.golds));
} //
public int[] findOtherGoldCell(int x, int y, Vector dagolds)
{
for (int i=0; i < dagolds.size(); i++) {
int[] goldpos = (int[])(dagolds.elementAt(i));
if (goldpos.length == 4) {
if ((goldpos[0] == x) && (goldpos[1] == y)) {
int[] temp = new int[2];
temp[0] = goldpos[2]; temp[1] = goldpos[3];
return(temp);
}
if ((goldpos[2] == x) && (goldpos[3] == y)) {
int[] temp = new int[2];
temp[0] = goldpos[0]; temp[1] = goldpos[1];
return(temp);
}
} // end if (goldpos.length == 4
} // endfor
return((int[])null);
} // findOtherGoldCell() //
// this searches for any accessible piece of gold (from here) and
// removes it.
public boolean removeAnyGold(int x, int y, Vector golds)
{
if (this.removeGold(x,y,golds)) {return(true);}
else
if (this.removeGold(x,y,x-1,y,golds)) {return(true);}
else
if (this.removeGold(x,y,x+1,y,golds)) {return(true);}
else
if (this.removeGold(x,y,x,y-1,golds)) {return(true);}
else
if (this.removeGold(x,y,x,y+1,golds)) {return(true);}
return(false);
} // removeAnyGold() //
// this version does *not* remove gold at the bays, though.
// this is for robot "control"
public boolean removeAnyGoldRobot(int x, int y, Vector golds)
{
if ((x > 0) && (x < this.width-1) &&
(this.removeGold(x,y,golds))) {return(true);}
else
if (this.removeGold(x,y,x-1,y,golds)) {return(true);}
else
if (this.removeGold(x,y,x+1,y,golds)) {return(true);}
else
if (this.removeGold(x,y,x,y-1,golds)) {return(true);}
else
if (this.removeGold(x,y,x,y+1,golds)) {return(true);}
return(false);
} // removeAnyGoldRobot() //
// takes an x,y cell and a wallspec and draws the wallspec for the cell
public boolean cellWallspec(int x, int y, int[] wallspec)
{
for (int Index = 0; Index < 4; Index ++) {
if (wallspec[Index] == 1)
drawWall(x, y, Index);
}
return(true);
} // cellWallSpec() //
public boolean drawWall(int x, int y, int dir)
{
switch(dir) {
case 0:
drawHorizontalLine(this.xCell2Pixel(x),
this.xCell2Pixel(x+1),
this.yCell2Pixel(y),
this.wall_thickness);
break;
// if (wallspec[0]) //
case 2:
drawHorizontalLine(this.xCell2Pixel(x),
this.xCell2Pixel(x+1),
this.yCell2Pixel(y-1),
this.wall_thickness);
break;
// if (wallspec[2]) //
case 1:
drawVerticalLine(this.xCell2Pixel(x),
this.yCell2Pixel(y),
this.yCell2Pixel(y-1),
this.wall_thickness);
break;
// if (wallspec[1]) //
case 3:
drawVerticalLine(this.xCell2Pixel(x+1),
this.yCell2Pixel(y),
this.yCell2Pixel(y-1),
this.wall_thickness);
break;
// if (wallspec[3]) //
}
return true;
}
public boolean eraseWall(int x, int y, int dir)
{
switch(dir) {
case 0:
eraseHorizontalLine(this.xCell2Pixel(x),
this.xCell2Pixel(x+1),
this.yCell2Pixel(y),
this.wall_thickness);
drawHorizontalLine(this.xCell2Pixel(x),
this.xCell2Pixel(x+1),
this.yCell2Pixel(y),
1);
break;
// if (wallspec[0]) //
case 2:
eraseHorizontalLine(this.xCell2Pixel(x),
this.xCell2Pixel(x+1),
this.yCell2Pixel(y-1),
this.wall_thickness);
drawHorizontalLine(this.xCell2Pixel(x),
this.xCell2Pixel(x+1),
this.yCell2Pixel(y-1),
1);
break;
// if (wallspec[2]) //
case 1:
eraseVerticalLine(this.xCell2Pixel(x),
this.yCell2Pixel(y),
this.yCell2Pixel(y-1),
this.wall_thickness);
drawVerticalLine(this.xCell2Pixel(x),
this.yCell2Pixel(y),
this.yCell2Pixel(y-1),
1);
break;
// if (wallspec[1]) //
case 3:
eraseVerticalLine(this.xCell2Pixel(x+1),
this.yCell2Pixel(y),
this.yCell2Pixel(y-1),
this.wall_thickness);
drawVerticalLine(this.xCell2Pixel(x+1),
this.yCell2Pixel(y),
this.yCell2Pixel(y-1),
1);
break;
} // if (wallspec[3]) //
return true;
}
// takes the cell,cell x,y position & direction of the robot & draws
public boolean drawRobot(int[] robotdesc, int robotnumber)
{
int cell_x= robotdesc[0];
int cell_y=robotdesc[1] ;
int dir=robotdesc[2];
int numgolds = robotdesc[3];
int x,y; // drawRobotCircle needs upper left corner of cell block
x = this.xCell2Pixel(cell_x);
y = this.yCell2Pixel(cell_y);
return(this.drawRobotCircle(x,y,dir,numgolds,robotnumber));
} // drawRobot() //
public boolean drawRobotLine(int[] robotdesc, int robotnumber)
{
int cell_x= robotdesc[0];
int cell_y=robotdesc[1] ;
int dir=robotdesc[2];
int numgolds = robotdesc[3];
int x,y; // drawRobotLine needs upper left corner of cell block
x = this.xCell2Pixel(cell_x);
y = this.yCell2Pixel(cell_y);
return(this.drawRobotLine(x,y,dir,numgolds,robotnumber));
} // drawRobotLines() //
// takes the cell,cell xy position of a port and draws it
public boolean drawPort(int cell_x, int cell_y)
{
int x,y; // drawGoldPos needs upper x,y coordinates //
x = this.xCell2Pixel(cell_x);
y = this.yCell2Pixel(cell_y);
return(this.drawPortPos(x,y));
} // drawPort() //
// takes the cell,cell xy position of a 1-cell gold and draws it
public boolean drawGold(int cell_x, int cell_y)
{
int x,y; // drawGoldPos needs upper x,y coordinates //
x = this.xCell2Pixel(cell_x);
y = this.yCell2Pixel(cell_y);
return(this.drawGoldPos(x,y));
} // drawGold() //
// overloaded 2-cell gold case..
public boolean drawGold(int c1_x, int c1_y, int c2_x, int c2_y)
{
int x1, y1, x2, y2;
x1 = this.xCell2Pixel(c1_x);
x2 = this.xCell2Pixel(c2_x);
y1 = this.yCell2Pixel(c1_y);
y2 = this.yCell2Pixel(c2_y);
return(this.drawGoldPos(x1,y1,x2,y2));
}
// draws the interior grid of the maze -- 1 pixel thick //
public boolean drawGrid()
{
for (int i=1; i < this.height; i++) {
this.drawHorizontalLine(0, (int)(this.width *
this.cellsize *
this.magnification),
(int)(i *
this.cellsize *
this.magnification),
1);
} // end for() //
for (int i=1; i < this.width; i++) {
this.drawVerticalLine((int)(i * this.cellsize
* this.magnification),
0,(int)(this.height *
this.cellsize *
this.magnification),
1);
} // end for() //
return(true);
} // end drawGrid() //
// draws the outside wall border of the maze //
public boolean drawBorder()
{
this.drawHorizontalLine(0,
(int)(this.cellsize *
this.width *
this.magnification),
0,
this.wall_thickness);
this.drawHorizontalLine(0,
(int)(this.cellsize *
this.width *
this.magnification),
(int) (this.cellsize *
this.height *
this.magnification),
this.wall_thickness);
this.drawVerticalLine(0,
0,
(int) (this.cellsize *
this.height *
this.magnification),
this.wall_thickness);
this.drawVerticalLine((int) (this.cellsize *
this.width *
this.magnification),
0,
(int) (this.cellsize *
this.height *
this.magnification),
this.wall_thickness);
return(true);
} // drawBorder() //
// translates from cell,cell to x,y //
public int xCell2Pixel(double cell)
{
return( (int)(cell * this.cellsize * this.magnification));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -