Eclipse java Maze Don';行不通
我的eclipe上的java小程序不工作它写入“applet initialized”,但不处理为“applet start”,小程序不启动(只是空白屏幕),而且控制台窗口中也没有错误 图片: 我的代码是关于深度优先迷宫的,问题出在genrate()函数中,因为当我注释它的调用时,程序运行良好 Maze.java:Eclipse java Maze Don';行不通,java,eclipse,applet,Java,Eclipse,Applet,我的eclipe上的java小程序不工作它写入“applet initialized”,但不处理为“applet start”,小程序不启动(只是空白屏幕),而且控制台窗口中也没有错误 图片: 我的代码是关于深度优先迷宫的,问题出在genrate()函数中,因为当我注释它的调用时,程序运行良好 Maze.java: import java.awt.Color; import java.awt.Graphics; import java.util.Random; public class Maz
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Maze {
// Direction Variables
public static final int Direction_Up=1;
public static final int Direction_Down=2;
public static final int Direction_Right=3;
public static final int Direction_Left=4;
//
//
int width, height;
int SizeX,SizeY;
public int[] start={0,0},end={3,3};
int offset=2;
boolean[][] WallsVer = { { false,true, false ,false},
{ false,false, false ,false}, { false,false, false ,true}};
boolean[][] WallsHor ={ { true,false, false },
{ true,false, true }, { true,true, true }, { false,true, false}};
public Maze(int width, int height,int sizeX,int sizeY) {
// TODO Auto-generated constructor stub
this.width = width;
this.height = height;
SizeX = sizeX;
SizeY = sizeY;
}
//
public boolean CollisionCheck(int x,int y,int Direction){
switch(Direction){
case Direction_Up:
if(y == 0){
return false;
}else if(!WallsVer[y-1][x]){
return true;
}else{
return false;
}
case Direction_Down:
if(y==SizeY-1){
return false;
}else if(!WallsVer[y][x]){
return true;
}else{
return false;
}
case Direction_Right:
if(x==SizeX-1){
return false;
}else if(!WallsHor[y][x]){
return true;
}else{
return false;
}
case Direction_Left:
if(x == 0){
return false;
}else if(!WallsHor[y][x-1]){
return true;
}else{
return false;
}
}
return false;
}
public void update() {
}
public void paint(Graphics g) {
//draw border
g.setColor(Color.RED);
g.drawLine(0, 0, width, 0);
g.drawLine(0, height, width, height);
g.drawLine(0, 0, 0, height);
g.drawLine(width, 0, width, height);
//draw maze
g.setColor(Color.RED);
for(int j=0;j<SizeX;j++){
for(int i=0;i<(SizeY-1);i++){
if(WallsVer[i][j])
g.drawLine(j*100,(i+1)*100 , (j*100)+100,(i+1)*100 );
}
}
for(int j=0;j<(SizeX-1);j++){
for(int i=0;i<SizeY;i++){
if(WallsHor[i][j])
g.drawLine((j+1)*100, i*100,(j+1)*100, (i*100)+100);
}
}
}
public void generate(){
// horX = sizeX -1
// horY = sizeY
// verX = sizeX
// verY = sizeY -1
//set and initialize variables
int TotalCells=SizeX*SizeY, VistedCells=0;
int[] CurrentCell = start;
int[] AvaliableCells = new int[4];
int AvaliableCellsNum = 0;
int[][] stack= new int[TotalCells][2];
int stackPointer = 0;
boolean[][] map = new boolean[SizeX][SizeY];
//initialize walls
for(int j=0;j<SizeX;j++){
for(int i=0;i<(SizeY-1);i++){
WallsVer[i][j]= true;
}
}
for(int j=0;j<(SizeX-1);j++){
for(int i=0;i<SizeY;i++){
WallsHor[i][j] = true;
}
}
//initialize map
for(int j=0;j<(SizeX-1);j++){
for(int i=0;i<(SizeY-1);i++){
map[0][0]= false;
}
}
map[CurrentCell[0]][CurrentCell[1]] = true;
stack[stackPointer] = start;
VistedCells=1;
while(VistedCells < TotalCells){
// test all the walls and enter them in AvaliableCells
if(CurrentCell[0]!=0 && !map[CurrentCell[0]-1][CurrentCell[1]]){
AvaliableCells[AvaliableCellsNum] = Direction_Right;
AvaliableCellsNum++;
}
if(CurrentCell[0]!=3 && !map[CurrentCell[0]+1][CurrentCell[1]]){
AvaliableCells[AvaliableCellsNum] = Direction_Left;
AvaliableCellsNum++;
}
if(CurrentCell[1]!=0 && !map[CurrentCell[0]][CurrentCell[1]-1]){
AvaliableCells[AvaliableCellsNum] = Direction_Up;
AvaliableCellsNum++;
}
if(CurrentCell[1]!=3 && !map[CurrentCell[0]][CurrentCell[1]+1]){
AvaliableCells[AvaliableCellsNum] = Direction_Down;
AvaliableCellsNum++;
}
// randomization process
if(AvaliableCellsNum > 0){
Random RndGen = new Random();
int RndValue = RndGen.nextInt(AvaliableCellsNum);
AvaliableCellsNum = 0;
switch(AvaliableCells[RndValue]){
case Direction_Up:
WallsVer[CurrentCell[1]-1][CurrentCell[0]] = false;
CurrentCell[1]--;
map[CurrentCell[0]][CurrentCell[1]] = true;
stackPointer++;
stack[stackPointer] = CurrentCell;
VistedCells++;
break;
case Direction_Down:
WallsVer[CurrentCell[1]][CurrentCell[0]] = false;
CurrentCell[1]++;
map[CurrentCell[0]][CurrentCell[1]] = true;
stackPointer++;
stack[stackPointer] = CurrentCell;
VistedCells++;
break;
case Direction_Right:
WallsHor[CurrentCell[1]][CurrentCell[0]-1] = false;
CurrentCell[0]--;
map[CurrentCell[0]][CurrentCell[1]] = true;
stackPointer++;
stack[stackPointer] = CurrentCell;
VistedCells++;
break;
case Direction_Left:
WallsHor[CurrentCell[1]][CurrentCell[0]] = false;
CurrentCell[0]++;
map[CurrentCell[0]][CurrentCell[1]] = true;
stackPointer++;
stack[stackPointer] = CurrentCell;
VistedCells++;
break;
}
}else{
stack = popStack(stack,stackPointer);
CurrentCell = stack[stack.length-1];
}
}
end = CurrentCell;
}
private int[][] popStack(int[][] stack,int newlength){
int[][] temp = new int[newlength][2];
for(int i=0;i<newlength;i++){
temp[i] = stack[i];
}
return temp;
}
//getters and setters
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public int getSizeX() {
return SizeX;
}
public int getSizeY() {
return SizeY;
}
public int getStartX() {
return (start[0]*(width/SizeX))+offset;
}
public int getStartY() {
return (start[1]*(height/SizeY))+offset;
}
public int getEndX() {
return (end[0]*(width/SizeX))+offset;
}
public int getEndY() {
return (end[1]*(height/SizeY))+offset;
}
public int getBlockW() {
return (width/SizeX)-(2*offset);
}
public int getBlockH() {
return (height/SizeY)-(2*offset);
}
}
导入java.awt.Color;
导入java.awt.Graphics;
导入java.util.Random;
公共类迷宫{
//方向变量
公共静态最终整数方向_Up=1;
公共静态最终整数方向_向下=2;
公共静态最终整数方向_Right=3;
公共静态最终整数方向_左=4;
//
//
int宽度、高度;
int-SizeX,SizeY;
公共int[]start={0,0},end={3,3};
整数偏移=2;
布尔[][]WallsVer={{false,true,false,false},
{假,假,假,假},{假,假,假,真};
布尔[][]WallsHor={{true,false,false},
{真,假,真},{真,真,真},{假,真,假};
公共迷宫(整数宽度、整数高度、整数大小、整数大小){
//TODO自动生成的构造函数存根
这个。宽度=宽度;
高度=高度;
SizeX=SizeX;
SizeY=SizeY;
}
//
公共布尔冲突检查(整数x,整数y,整数方向){
开关(方向){
案件方向(向上):
如果(y==0){
返回false;
}如果(!WallsVer[y-1][x]){
返回true;
}否则{
返回false;
}
案例方向(u Down):
如果(y==SizeY-1){
返回false;
}如果(!WallsVer[y][x]){
返回true;
}否则{
返回false;
}
案例方向(右):
如果(x==SizeX-1){
返回false;
}如果(!WallsHor[y][x]){
返回true;
}否则{
返回false;
}
案例方向(左):
如果(x==0){
返回false;
}如果(!WallsHor[y][x-1]){
返回true;
}否则{
返回false;
}
}
返回false;
}
公共无效更新(){
}
公共空间涂料(图g){
//划界
g、 setColor(Color.RED);
g、 抽绳(0,0,宽度,0);
g、 抽绳(0,高度,宽度,高度);
g、 抽绳(0,0,0,高度);
g、 抽绳(宽度、0、宽度、高度);
//画迷宫
g、 setColor(Color.RED);
对于(int j=0;jA),建议减少generate()函数的大小。这是一个比较大的方面。通过将小部分提取到它们自己的方法中,您可以使程序更易于阅读,并且您可以自己在过程中发现错误。您的generate()可能会函数在无限循环上运行。我的魔法球说VistedCells