简体   繁体   English

如何减慢 java 中的程序执行速度?

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

I created a JavaFX program to draw a Sierpinski Carpet recursively, but I want to see the recursive functions in action that is, to slow down the program to see it drawing in real-time.我创建了一个 JavaFX 程序以递归方式绘制谢尔宾斯基地毯,但我想查看递归函数的实际作用,即减慢程序速度以查看它实时绘制。 For that I tried something like this:为此,我尝试了这样的事情:

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

Then I invoked this function in my drawSierpinskiCarpet function something like this:然后我在我的 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);
        }
    }
}

But what happens is the program hang up for a few seconds, and then directly shows up the Carpet.但是会发生什么是程序挂了几秒钟,然后直接显示了地毯。 I'm not able to see the program in execution.我无法看到正在执行的程序。

Any modification on the GUI only yields visual results after it returns.对 GUI 的任何修改都只会在它返回后产生视觉结果。 In the meantime JavaFX cannot handle any events and freezes, until your operation is done.与此同时,JavaFX 无法处理任何事件并冻结,直到您的操作完成。

You can use a Timeline to execute the operation one step at a time though: Use a stack the data for one drawing execution;但是,您可以使用Timeline一次执行一个步骤:使用堆栈数据进行一次绘图执行; this allows you to do one drawing operation at a time in addition to modifying the stack for every frame of the 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