开发者

Java: How can I wait for the updateUI?

I was doing a demo on the ChessBoard Game.I guess you must know it.How ever I meet with a problem.The result just show to quickly.I want to see how it works out slowly.

Here is the main code:

public void ChessBoard(int tr, int tc, int dr, int dc, int size) throws InterruptedException   {
        if (size == 1) {
            return;
        }
        Random rd = new Random();
        float red = rd.nextFloat();
        float green = rd.nextFloat();
        float blue = rd.nextFloat();
        Color col = new Color(red, green, blue);
        int s = size / 2;
        if (dr < tr + s && dc < tc + s) {
            ChessBoard(tr, tc, dr, dc, s);
        } else {
            button[tr + s - 1][tc + s - 1].setBackground(col);
            ChessBoard(tr, tc, tr + s - 1, tc + s - 1, s);
        }
        if (dr < tr + s && dc >= tc + s) {
            ChessBoard(tr, tc + s, dr, dc, s);
        } else {
            button[tr + s - 1][tc + s].开发者_运维知识库setBackground(col);
            ChessBoard(tr, tc + s, tr + s - 1, tc + s, s);
        }
        if (dr >= tr + s && dc < tc + s) {
            ChessBoard(tr + s, tc, dr, dc, s);
        } else {
            button[tr + s][tc + s - 1].setBackground(col);
            ChessBoard(tr + s, tc, tr + s, tc + s - 1, s);

        }
        if (dr >= tr + s && dc >= tc + s) {
            ChessBoard(tr + s, tc + s, dr, dc, s);
        } else {
            button[tr + s][tc + s].setBackground(col);
            ChessBoard(tr + s, tc + s, tr + s, tc + s, s);
        }


        mainPanel.updateUI();
        for (long i=1;i<10000;i++){
            for (long j=0;j<10000;j++){
                i+=1;
                i-=1;
            }
        }

    }

But the result just turned out in a flash!It seems the mainPanel didn't finished its job. The mainJanel is a JPanel object.


I also add the sleep at the beginning, but only the progress is slow down, it seems has nothing to do with the showing up

It appears your code is executing on the Event Dispatch Thread. You should NEVER tell the EDT to sleep(). This prevents the GUI from repainting itself. Read the section from the Swing tutorial on Concurrency for more information.

Instead you should be using a Swing Timer to schedule the animation of each move. The tutorial also has a section on Timers.


updateUI is used to change the look&feel of the component. To redraw the contents of the panel you should use

mainPanel.repaint()


Just use Thread.sleep( /* time in milliseconds */ ) to slow the execution.


Halting execution with sleep .

Example

public class SleepMessages {
    public static void main(String args[]) throws InterruptedException {
        String importantInfo[] = {
            "Mares eat oats",
            "Does eat oats",
            "Little lambs eat ivy",
            "A kid will eat ivy too"
        };

        for (int i = 0; i < importantInfo.length; i++) {
            //Pause for 4 seconds
            Thread.sleep(4000);
            //Print a message
            System.out.println(importantInfo[i]);
        }
    }
}

This way it would slow down your program.

 //Pause for 4 seconds
   Thread.sleep(4000);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜