I'm trying to get my JaxaFX program to make circle with alternating color when the user enters a radius and come out like this (with my alternating colors being forest green and red):
But its only coming out as forest green
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();
}
}
i can't seem to get it to alternate colors
There are a few issues here to fix.
First, with the recursion. The base case you wrote for the displayCircles
method, radius > 0
is not what you want. You want to keep adding circles for as long as radius > 0, so you want to end when the radius isn't bigger than 0. In other words, your base case should be radius <= 0
.
Okay, if we fix that and run the code, we see nothing. That's because currently only in the base case is a circle created. In reality, we want to create a circle every time to get the result you're looking for.
So here's the modified displayCircles
code:
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);
}
}
And the output:
I assume this is for school, but in the off chance it isn't it would be better to use a for-loop to create the circles as recursion is overkill here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With