递归地将椭圆添加到窗格

时间:2015-07-16 13:23:09

标签: java recursion javafx

我正在尝试以递归方式将椭圆添加到窗格中以进行作业。我已经编写了我认为应该工作的代码,虽然它编译和运行,但它在我的窗格中没有显示任何内容。对于一些背景,椭圆应该全部在窗格中居中,每个应该距离下一个椭圆边缘10px ,外椭圆应距离窗格边缘10px。

这是我的代码

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import java.util.Random;
import javafx.scene.paint.Color;

public class DisplayCircles extends Application {

    private static Pane mainPane = new Pane();

    public void start(Stage primaryStage) {

        double horRadius = (mainPane.getWidth() / 2) - 10;
        double vertRadius = (mainPane.getHeight() / 2) - 10;
        addCircles(horRadius, vertRadius);
        Scene scene = new Scene(mainPane, 500, 500);
        primaryStage.setTitle("Circle Display");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    /**
     * Recursively adds circles to the pane from largest to smallest.
     *
     * @param horizontal - The starting horizontal radius.
     * @param vertical - The starting vertical radius.
     */
    public static void addCircles(double horizontal, double vertical) {

        if (horizontal <= 10 || vertical <= 10) {

            createEllipse(horizontal, vertical);

        } else {

            createEllipse(horizontal, vertical);
            addCircles(horizontal - 10, vertical - 10);
        }

    }

    /**
     * Creates an ellipse with the given horizontal and vertical radii.
     *
     * @param horizontal - The x based radius.
     * @param vertical - the y based radius.
     */
    private static void createEllipse(double horizontal, double vertical) {

        Random rand = new Random();
        Ellipse ellipse = new Ellipse(horizontal, vertical);
        ellipse.centerXProperty().bind(
            mainPane.widthProperty().divide(2.0));
        ellipse.centerYProperty().bind(
            mainPane.heightProperty().divide(2.0));
        double r = rand.nextDouble();
        double g = rand.nextDouble();
        double b = rand.nextDouble();
        double o = rand.nextDouble();
        ellipse.setFill(Color.color(r, g, b, o));
        mainPane.getChildren().add(ellipse);

    }

}

1 个答案:

答案 0 :(得分:1)

Pane的宽度和高度将0添加到Scene并且Scene已经过布局。当然,在这种情况下,你知道窗格的初始大小是什么,所以你可以做到

    double width = 500 ;
    double height = 500 ;
    double horRadius = (width / 2) - 10;
    double vertRadius = (height / 2) - 10;
    addCircles(horRadius, vertRadius);
    Scene scene = new Scene(mainPane, width, height);

另一种解决方案是在窗格大小发生变化时重新计算图形。在此解决方案中,当窗格首次放置在场景中时绘制圆圈,然后在窗口调整大小时重新绘制以填充窗格。这可能不是你想要的这个应用程序,但在其他情况下可能是一个有用的想法:

    mainPane.boundsInLocalProperty().addListener((obs, oldBounds, newBounds) -> {
        mainPane.getChildren().clear();
        double horRadius = (mainPane.getWidth() / 2) - 10;
        double vertRadius = (mainPane.getHeight() / 2) - 10;
        addCircles(horRadius, vertRadius);
    });
    Scene scene = new Scene(mainPane, 500, 500);

顺便说一句,你为什么要做所有事情static?它并不重要,因为只创建了Application子类的一个实例,但一般情况下使用static时不好的做法是不好的设计理由这样做。