Search code examples
javaeclipsebuttonjavafxfxml

how can I do something while the program is waiting to press the button?


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!


Solution

  • 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);
        }
    
    }