如何延迟显示迷宫的最终结果

时间:2014-02-27 09:19:59

标签: java eclipse maze

我正在创建一个迷宫生成应用程序。我想要发生的是,当我运行我的程序时,它将显示关于如何创建迷宫的动画(它将显示它如何敲击墙来创建路径)。

我试图延迟它的某些部件,但它不会运行。谢谢你的巨大帮助! 这是代码:

public class Maze extends JPanel {

private Room[][] rooms;// m x n matrix of rooms
private ArrayList<Wall> walls; // List of walls
private Random rand;// for random wall
private int height;// height of matrix
private int width;// width of matrix
private int num;// incrementor
private JoinRoom ds;// union paths

// paint methods //
private int x_cord; // x-axis rep
private int y_cord;// y-axis rep
private int roomSize;
private int randomWall;
private int create;
int mazectr;

public static int m;// these are variables for the size of maze (m x n)
public static int n;

public Maze(int m, int n) {

    JPanel j = new JPanel();
    final JFrame f = new JFrame();
    this.height = m;
    this.width = n;
    rooms = new Room[m][n];
    walls = new ArrayList<Wall>((m - 1) * (n - 1));
    long startTime = System.currentTimeMillis(); 
    generateRandomMaze();
    long endTime = System.currentTimeMillis();
    final JLabel jl = new JLabel("Time Taken: " + (endTime-startTime) + "ms");
    final JButton y = new JButton("OK");
    j.add(jl);
    j.add(y);
    f.add(j);
    y.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent e)
        {
            f.setVisible(false);
        }
    });
    f.setVisible(true);
    f.setSize(100, 100);


    //jl.setLocation(1000, 1500);
    //jl.setBounds(0, 0, 110, 130);
    setPreferredSize(new Dimension(800, 700));


   }


private void generateRandomMaze() {
    generateInitialRooms();// see next method
    ds = new JoinRoom(width * height);
    rand = new Random(); // here is the random room generator
    num = width * height;
    while (num > 1) {
       // when we pick a random wall we want to avoid the borders getting eliminated
        randomWall = rand.nextInt(walls.size());
        Wall temp = walls.get(randomWall);
        // we will pick two rooms randomly 
        int roomA = temp.currentRoom.y + temp.currentRoom.x * width;
        int roomB = temp.nextRoom.y + temp.nextRoom.x * width;

        // check roomA and roomB to see if they are already members 
        if (ds.find(roomA) != ds.find(roomB)) {
            walls.remove(randomWall);
            ds.unionRooms(ds.find(roomA), ds.find(roomB));
            temp.isGone = true;
            temp.currentRoom.adj.add(temp.nextRoom);
            temp.nextRoom.adj.add(temp.currentRoom);
            num--;
        }// end of if

    }// end of while

}

 // name the room to display
private int roomNumber = 0;
private static Label input;
private static Label input2;

/**
 * Sets the grid of rooms to be initially boxes
 * This is self explanitory, we are only creating an reverse L for all
 * The rooms and there is an L for the border
 */
private void generateInitialRooms() {
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            // create north walls
            rooms[i][j] = new Room(i, j);
            if (i == 0) {
                rooms[i][j].north = new Wall(rooms[i][j]);
            } else {
                rooms[i][j].north = new Wall(rooms[i - 1][j], rooms[i][j]);
                walls.add(rooms[i][j].north);
            }
            if (i == height - 1) {
                rooms[i][j].south = new Wall(rooms[i][j]);
            }
            if (j == 0) {
                rooms[i][j].west = new Wall(rooms[i][j]);
            } else {
                rooms[i][j].west = new Wall(rooms[i][j - 1], rooms[i][j]);
                walls.add(rooms[i][j].west);
            }
            if (j == width - 1) {
                rooms[i][j].east = new Wall(rooms[i][j]);
            }
            rooms[i][j].roomName = roomNumber++;// we will name the rooms
        }
    }
    // initalize entrance and exit
    rooms[0][0].west.isGone = true;// you can replace .west.isGone with .north.isGone
    // this is just saying the roomName for top left is 0 
    rooms[0][0].roomName = 0;
    // we will remove the south wall of the last room
    rooms[height - 1][width - 1].south.isGone = true;
    // this is just saying the roomName for bottom right is the last element in the mxn room matrix
    rooms[height - 1][width - 1].roomName = (height * width);
}

 public void paintComponent(Graphics g) {
    x_cord = 40;
    y_cord = 40;
   Thread t = new Thread();
    // could have taken height as well as width
    // just need something to base the roomsize
    roomSize = (width - x_cord) / width + 20;

    // temp variables used for painting
    int x = x_cord;
    int y = y_cord;

    for (int i = 0; i <= height - 1; i++) {
        for (int j = 0; j <= width - 1; j++) {
            if (!(rooms[i][j].north.isGone)) {
                g.drawLine(x, y, x + roomSize, y);
            }//end of north if
            // west wall not there draw the line
            if (rooms[i][j].west.isGone == false) {
                g.drawLine(x, y, x, y + roomSize);
            }// end of west if
            if ((i == height - 1) && rooms[i][j].south.isGone == false) {
                g.drawLine(x, y + roomSize, x + roomSize,
                        y + roomSize);
            }// end of south if
            if ((j == width - 1) && rooms[i][j].east.isGone == false) {
                g.drawLine(x + roomSize, y, x + roomSize,
                        y + roomSize);
            }// end of east if
            x += roomSize;// change the horizontal
            try
            {
                Thread.sleep(50);
            } catch (Exception e) {};

            t.start();
        }// end of inner for loop
        x = x_cord;
        y += roomSize;

    }// end of outer for loop

    }



    public static void main(String[] args) throws IOException {

    // use JFrame to put the created panel on 
   String path = "E:\\Iskul\\trys\\tryy\\bin\\GUI.jpg";
   File file = new File("E:\\Iskul\\trys\\tryy\\bin\\GUI.jpg");
   BufferedImage image = ImageIO.read(file);
   File fileRec = new File("E:\\Iskul\\trys\\tryy\\bin\\re.jpg");
   BufferedImage imageRec = ImageIO.read(fileRec);
   File fileHex = new File("E:\\Iskul\\trys\\tryy\\bin\\hexx.jpg");
   BufferedImage imageHex = ImageIO.read(fileHex);


    final JFrame frame = new JFrame("Prim's Algorithm");
    final JPanel jp = new JPanel();
    final JTextField input = new JTextField(10);
    final JTextField input2 = new JTextField(10); 
    final JButton jb = new JButton(new ImageIcon(imageRec));
    jb.setBorder(BorderFactory.createEmptyBorder());
    final JButton jb1 = new JButton(new ImageIcon(imageHex));
    jb1.setBorder(BorderFactory.createEmptyBorder());
    final JLabel label = new JLabel(new ImageIcon(image));


    jb.setLocation(100, 10);
    frame.getContentPane().add(label);
    frame.pack();
    frame.setVisible(true);
    frame.setSize(400, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(new Dimension(795, 501));
    jp.add(input);
    jp.add(input2);
    frame.add(jp);
    jp.add(jb);
    jp.add(jb1);
    //jb.setImage(image);


    jb.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            //int mazectr = 1;
            int m = Integer.valueOf(input.getText());   
            int n = Integer.valueOf(input2.getText());
            frame.remove(label);
            frame.remove(jp);
            //frame.add(new Maze(m,n));
            frame.getContentPane().add(new Maze(m, n));
            frame.pack();


    }});

jb1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){ 
            //int m = Integer.valueOf(input.getText()); 
            //int n = Integer.valueOf(input2.getText());
            Hexa1 hexa = new Hexa1();
            hexa.main();
            //hexa.run();
        }
    });


   }}// end of main

1 个答案:

答案 0 :(得分:0)

你的代码不会工作,你不能在Paint方法中睡觉,因为如果你这样做,他不会画,他只是等待,最后他会画出你的整个图像。

如果你想做一个很好的效果,你应该先拉一步,但你应该意识到这需要做很多工作......