我试图让我的 JaxaFX 程序在用户输入半径时用交替颜色制作圆圈,并像这样出来 ,我的交替颜色是森林绿色和红色,但它只以森林绿色出现
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 cirlce: ");
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 thec center pane ");
}
}catch(NumberFormatException e){
showAlert("Invalid input");
}
}
//Display the Circle with recursion
public void displayCircles(double radius, Color color)
{
//When the base case radius is 0 it stops
if(radius > 0)
{
//Displays circle
Circle circle = new Circle(centerpane.getWidth()/2,centerpane.getHeight()/2,radius);
circle.setStroke(Color.BLACK);
circle.setFill(color);
centerpane.getChildren().add(circle);
}
else // recursive case
{
Color nextColor;
if(color.equals(Color.FORESTGREEN)){
nextColor = Color.RED;
}
else{
nextColor = Color.FORESTGREEN;
}
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();
}
}
我似乎无法让它改变颜色
这里有一些问题需要修复。
首先,用递归。您为
displayCircles
方法编写的基本案例 radius > 0
不是您想要的。您希望继续添加圆圈直到半径 > 0 为止,因此您希望在半径不大于 0 时结束。换句话说,您的基本情况应该是 radius <= 0
。好吧,如果我们解决这个问题并运行代码,我们什么也看不到。这是因为目前仅在基本情况下创建了一个圆。实际上,我们希望每次都创建一个圆圈以获得您正在寻找的结果。
所以这是修改后的
displayCircles
代码:
public void displayCircles(double radius, Color color)
{
//When the base case radius is 0 it stops, otherwise run again.
if(radius > 0)
{
// Create and add the circle to UI
Circle circle = new Circle(centerpane.getWidth()/2,centerpane.getHeight()/2,Math.max(0, radius));
circle.setStroke(Color.BLACK);
circle.setFill(color);
centerpane.getChildren().add(circle);
// Recursively create the next circle.
Color nextColor;
if(color.equals(Color.FORESTGREEN)){
nextColor = Color.RED;
}
else{
nextColor = Color.FORESTGREEN;
}
displayCircles(radius - 10, nextColor);
}
}
输出:
我认为这是为了学校,但万一不是,最好使用 for 循环来创建圆圈,因为递归在这里太过分了。