Java applet刷新?

时间:2016-06-20 02:52:46

标签: java applet

我甚至不确定如何标题这个问题。

但基本上这是一个迷宫程序,它会生成一个具有指定参数的迷宫,它的功能非常好。问题是,当它完成创建迷宫并将所有路径切片设置为白色时,它会刷新并且网格停留在那里,但路径切片会消失。

这是在完成生成调用reDrawPath时的情况:

这是在它完成调用reDrawPath之后立即:

也没有调用reDrawPath根本没有帮助,结果相同。

static final int border = 0;
static final int path = 1;
static final int wall = 2;

static final int cellSize = 15; // min 3
static final int cellSizeMask = (cellSize-1)/2;

static final int gridSizeh = 52; //grid gridSize (max 52)
static final int gridSizev = 52;

static final int choice = 1; // choice of algorithm (1-3)


public static int tx =1; //starting coords
public static int ty = 1; 

public static int var;
public static int varP;

public static Point[][] cords = new Point [gridSizeh][gridSizev]; // cords for each cells 
public static int tile[][] = new int [gridSizeh][gridSizev];      // if the cell will be a path, wall, or border
public static int Record[][] = new int [gridSizeh][gridSizev];



public void paint(Graphics g)   
{
    setCells(); //assigns starting grid values
    drawGrid(g); //draws the grid
    drawPath(g,tx,ty); //initial starting cord

    while(var != 16) //if the maze isnt finished then keep creating it
    {
        Checks();
        setTile(g);
    }

    if(var == 16) //when the maze is finished set all path cells to white and print done
    {
        reDrawPath(g); //redraws all path tiles to white (originally green)
        System.out.println("Done");
    }
}


public static void drawGrid(Graphics g) //draws grid
{
    Expo.setBackground(g, Expo.gray);
    for(int p = 0; p<gridSizeh; p++)
        for(int k = 0; k<gridSizev; k++)
        {   
            if(tile[p][k] == border) // If the cell is a border, set to dark red
                Expo.setColor(g,Expo.darkRed);
            else                     // if not a border set all cells to black by default
                Expo.setColor(g,Expo.black);
            Expo.fillRectangle(g,(int)cords[p][k].getX()-cellSizeMask,(int)cords[p][k].getY()-cellSizeMask, (int)cords[p][k].getX()+cellSizeMask, (int)cords[p][k].getY()+cellSizeMask);

        }   
}
public static void setCells() // set initial values for grid
{
    for(int p = 0; p<gridSizeh; p++)
        for(int k = 0; k<gridSizev; k++) // set all cells to walls by default
        {   
            cords[p][k] = new Point((p+1)*cellSize,(k+1)*cellSize);
            tile[p][k] = wall;
        }
    for(int p = 0; p<gridSizeh; p++)
        for(int k = 0; k<gridSizev; k++) // if a cell is on the outer edge, set it to a border cell
        {   
            tile[p][0] = border;
            tile[p][gridSizev-1] = border;
            tile[0][k] = border;
            tile[gridSizeh-1][k] = border;
        }
}

public static void reDrawPath(Graphics g) //set all path cells to white
{
    for(int p = 1; p<gridSizeh-1; p++)
        for(int k = 1; k<gridSizev-1; k++)
        {   
            if(tile[p][k] == path)
            {
                drawRecursed(g, p, k);

            }
            Expo.delay(1);
        }
}

public static void drawPath(Graphics g, int x, int y) //sets tile path and color green
{
    Expo.setColor(g,Expo.green);
    tile[x][y] = path;
    Expo.fillRectangle(g,(int)cords[x][y].getX()-cellSizeMask,(int)cords[x][y].getY()-cellSizeMask, (int)cords[x][y].getX()+cellSizeMask, (int)cords[x][y].getY()+cellSizeMask);
}
public static void drawRecursed(Graphics g, int x, int y) // sets tile to white
{
    Expo.setColor(g,Expo.white);
    Expo.fillRectangle(g,(int)cords[x][y].getX()-cellSizeMask,(int)cords[x][y].getY()-cellSizeMask, (int)cords[x][y].getX()+cellSizeMask, (int)cords[x][y].getY()+cellSizeMask);
}

0 个答案:

没有答案