我在eclipe上的java小程序不起作用它写“applet intialized”但是没有导致“Applet启动”并且applet没有启动(只是空白屏幕)并且控制台窗口中也没有错误
图片:http://i.stack.imgur.com/pvalc.png
我的代码是关于深度优先的迷宫,问题出在genrate()函数中,因为当我评论它的调用程序运行良好时
Maze.java:
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);
}
}