You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX递归绘制交替颜色同心圆问题求助

问题排查与修复

你的代码核心问题是递归逻辑分支写反,导致仅绘制了初始的森林绿圆,没有触发后续的递归调用生成交替颜色的同心圆。

错误原因分析

原displayCircles方法中,仅当radius > 0时绘制当前圆,但绘制完成后没有继续递归;而else分支才进行递归,但此时radius <= 0,递归调用radius -10只会让半径越来越小,永远不会进入绘制逻辑。

修复后的代码

public class App extends Application {

    private Pane centerpane;            // center pane
    private TextField enterTextField;   // text field for user to enter radius of circle
    private double radius;                  // order for recursion
    public Color color = Color.FORESTGREEN;

    @Override
    public void start(Stage stage) {
        BorderPane outerpane = new BorderPane();

        //CREATE the CENTER PANE
        centerpane = new Pane();
        centerpane.setBackground(new Background(new BackgroundFill(Color.LIGHTBLUE,null,null)));
        outerpane.setCenter(centerpane);

        //CREATE BOTTOM PANE AS HBOX OF LABEL , TEXTFIELD, AND START BUTTOM
        HBox bottompane = new HBox (20);
        bottompane.setAlignment(Pos.CENTER);
        bottompane.setPadding(new Insets(25,25,25,25));
        Label label = new Label ("Enter radius of largest circle: ");
        enterTextField = new TextField();
        Button startButton = new Button("Start");
        startButton.setOnAction(event -> makeCircles());
        bottompane.getChildren().add(label);
        bottompane.getChildren().add(enterTextField);
        bottompane.getChildren().add(startButton);
        outerpane.setBottom(bottompane);

        var scene = new Scene(outerpane, 600, 400);
        stage.setScene(scene);
        stage.show();

    }

    //Event handler for Start button gets radius,
    // and calls recursive display
    public void makeCircles() {
        //clear the center pane after every use
        centerpane.getChildren().clear();
        try {
            radius = Double.parseDouble(enterTextField.getText());

            if((radius*2 <= centerpane.getWidth()) && (radius*2 <= centerpane.getHeight())) {
                displayCircles(radius, color);
            } else {
                showAlert("Circle does not fit in the center pane ");
            }
        } catch(NumberFormatException e) {
            showAlert("Invalid input");
        }
    }

    //Display the Circle with recursion
    public void displayCircles(double radius, Color color) {
        // 终止条件:半径小于等于0时停止递归
        if(radius <= 0) {
            return;
        }

        // 先绘制当前半径的圆
        Circle circle = new Circle(centerpane.getWidth()/2, centerpane.getHeight()/2, radius);
        circle.setStroke(Color.BLACK);
        circle.setFill(color);
        centerpane.getChildren().add(circle);

        // 计算下一个圆的交替颜色
        Color nextColor = color.equals(Color.FORESTGREEN) ? Color.RED : Color.FORESTGREEN;
        // 递归绘制更小的圆(半径减少10)
        displayCircles(radius - 10, nextColor);
    }

    public void showAlert(String message) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }

    public static void main(String[] args) {
        launch();
    }
}

关键修改点

  1. 调整递归逻辑:将终止条件设为radius <= 0时直接返回;当radius > 0时,先绘制当前圆,再计算交替颜色,递归调用displayCircles并传入减小后的半径和新颜色。
  2. 简化颜色切换逻辑:使用三元运算符替代if-else,让代码更简洁。
  3. 修正拼写错误:原标签文本中的cirlce改为circle,提示文本中的thec改为the。

修改后,程序会从输入的最大半径开始,依次绘制半径递减10、颜色在森林绿和红色之间交替的同心圆,直到半径小于等于0时停止。

内容的提问来源于stack exchange,提问作者Down25

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 14:57:36