-2

i want to change the cirecle color all the time the COUNTER is smaller than 3 and while i waiting to click button. When the the cirecle is red he need to push on 'Stop'. This is the program:

public class Q1_2 extends Application {
    private MyCircle circle = MyCircle.getInstance();
    public int COUNTER;
    public Color CURRENT_COLOR;
    public Color currentColor;
    public static void main(String[] args) {
        launch();
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        circle.setRadius(100);
        RadioButton bColor = new RadioButton("Stop");
        COUNTER = 0;
        HBox box = new HBox(40, bColor);
        box.setPadding(new Insets(30));
        StackPane pane = new StackPane(circle, box);
        Scene scene = new Scene(pane, 500, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
        bColor.setOnAction(e -> {
            if (bColor.isSelected() == true && currentColor != javafx.scene.paint.Color.RED) {
                COUNTER++;
                bColor.setSelected(false);
            }
            if (bColor.isSelected() == true && currentColor == javafx.scene.paint.Color.RED)
                System.out.println("GREAT");
        });
        while (COUNTER < 3) {
            currentColor = chooseColor();
            circle.setFill(currentColor);
            if (COUNTER == 3)
                System.out.println("YOU LOSE");
        }
    }
}

Thank you!

Chen
  • 35
  • 5
  • 3
    There's a possible solution described here: https://stackoverflow.com/questions/46369046/how-to-wait-for-user-input-on-javafx-application-thread-without-using-showandwai . However I recommend using an event-based approach in the program instead of a loop. – fabian Apr 05 '19 at 18:19
  • Use [`Timeline`](https://stackoverflow.com/questions/9966136/javafx-periodic-background-task) to loop through some colors every second. When the `Button` is pressed, stop the `Timeline`. You can increase the speed(decrease the amount of time before the next color change) of the `Timeline` later. – SedJ601 Apr 05 '19 at 20:53

1 Answers1

1

If I understand you correctly, you can use Timeline to change the circle's color and stop the Timeline when the button is pressed.

Example Code:

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication357 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        List<Color> colors = Arrays.asList(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, Color.PURPLE);
        AtomicInteger counter = new AtomicInteger(0);
        Circle circle = new Circle(200, Color.TRANSPARENT);

        Timeline timeline = new Timeline(new KeyFrame(Duration.millis(300), (event) -> {
            circle.setFill(colors.get(counter.getAndIncrement() % colors.size()));
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.play();

        Button btn = new Button();
        btn.setText("Stop");
        btn.setOnAction((ActionEvent event) -> {
            timeline.stop();
        });

        VBox root = new VBox(new StackPane(circle), new StackPane(btn));

        Scene scene = new Scene(root, 450, 450);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}
SedJ601
  • 12,173
  • 3
  • 41
  • 59