簡體   English   中英

如何減慢 java 中的程序執行速度?

[英]How to slow down a program execution in java?

我創建了一個 JavaFX 程序以遞歸方式繪制謝爾賓斯基地毯,但我想查看遞歸函數的實際作用,即減慢程序速度以查看它實時繪制。 為此,我嘗試了這樣的事情:

public void pause(int sleepTime)
{
    try
    {
        Thread.sleep(sleepTime);
    }
    catch(InterruptedException e)
    {
        System.err.println("Error in running rotation animation!");
        System.exit(-1);
    }
}

然后我在我的 drawSierpinskiCarpet function 中調用了這個 function ,如下所示:

public void drawSeripinskiCarpet(GraphicsContext gc, int x, int y, int width, int height)
{
    if(width != height || width < 3)
        return;

    int size = width /= 3;
    gc.setFill(colors[index]);
    gc.fillRect(x + size, y + size, size, size);

    index++;

    if(index == colors.length)
    {
        index = 0;
    }

    pause(1);


    for(int i = 0 ; i < 3 ; i++)
    {
        for(int j = 0 ; j < 3 ; j++)
        {
            if(i == 1 && j == 1)
            {
                continue;
            }

            drawSeripinskiCarpet(gc, x + j * size, y + i * size, size, size);
        }
    }
}

但是會發生什么是程序掛了幾秒鍾,然后直接顯示了地毯。 我無法看到正在執行的程序。

對 GUI 的任何修改都只會在它返回后產生視覺結果。 與此同時,JavaFX 無法處理任何事件並凍結,直到您的操作完成。

但是,您可以使用Timeline一次執行一個步驟:使用堆棧數據進行一次繪圖執行; 除了為Timeline的每一幀修改堆棧之外,這還允許您一次執行一個繪圖操作。

@Override
public void start(Stage primaryStage) throws IOException {
    Canvas canvas = new Canvas(729, 729);
    final GraphicsContext gc = canvas.getGraphicsContext2D();

    Scene the_scene = new Scene(new StackPane(canvas));

    primaryStage.setScene(the_scene);
    primaryStage.show();

    final Color[] colors = new Color[] { Color.BLUE, Color.ORANGE, Color.GREEN, Color.RED, Color.BROWN,
            Color.MAGENTA, Color.YELLOW, Color.CYAN, Color.BLACK };
    class IndexHolder {
        int index = 0;

        Color getNextFill() {
            Color fill = colors[index];
            ++index;
            if (index >= colors.length) {
                index = 0;
            }
            return fill;
        }
    }
    final IndexHolder indexHolder = new IndexHolder();

    class StackEntry {
        int x;
        int y;
        int size;

        public StackEntry(int x, int y, int size) {
            this.x = x;
            this.y = y;
            this.size = size;
        }

        void executeStep(List<StackEntry> stack, IndexHolder indexHolder, GraphicsContext gc) {
            int size = this.size /= 3;
            gc.setFill(indexHolder.getNextFill());
            gc.fillRect(x + size, y + size, size, size);

            if (size >= 3) {
                // add new "calls" in reverse order here
                // (first call in original needs to be popped from the stack first)
                for (int i = 2; i >= 0; --i) {
                    for (int j = 2; j >= 0; --j) {
                        if (i == 1 && j == 1) {
                            continue;
                        }

                        stack.add(new StackEntry(x + j * size, y + i * size, size));
                    }
                }
            }
        }
    }

    // finally create the animation
    final List<StackEntry> stack = new ArrayList<>();
    stack.add(new StackEntry(0, 0, (int) canvas.getHeight()));

    final Timeline timeline = new Timeline();
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.getKeyFrames().add(new KeyFrame(Duration.millis(1),
            evt -> {
                // pop
                StackEntry entry = stack.remove(stack.size() - 1);

                entry.executeStep(stack, indexHolder, gc);
                if (stack.isEmpty()) {
                    timeline.stop();
                }
            }));

    timeline.play();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM