'Changing colors of one rectangle two times in simulation java

Hello i am trying to develop a simple javafx application where i can change the color of the rectangle using threads but without pressing the any buttons just while the thread is running it will be black and after the sleep it will be green I thought the problem might be in the UI because it cant load the change as quickly as code so that s why i used timer and pause.play etc etc but no result

I am also new in java so yeah sorry about the horrible coding

using these libraries

import java.lang.*;
import javafx.animation.Animation;
import javafx.animation.AnimationTimer;
import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        AnimationTimer timer = new AnimationTimer() {
            int count = 2;
            public void handle(long l) {

                {
                    if (--count <= 0) {
                        stage.setIconified(true);
                        this.stop();
                    }
                }
            }
        };
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        PauseTransition pause = new PauseTransition(Duration.seconds(3));
        Thread recthread = new Thread();
        Rectangle rect = new Rectangle();
        rect.setX(150.0 f);
        rect.setY(75.0 f);
        rect.setWidth(300.0 f);
        rect.setHeight(150.0 f);
        rect.setFill(Color.BLACK);
        Group root = new Group(rect);
        Scene scene = new Scene(root, 600, 300);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
        //for (int i = 0; i < 5; i++) {
        pause.play();
        rect.setFill(Color.BLACK);
        recthread.start();
        pause.play();
        try {
            timer.start();
            System.out.println("Hello next step is chocolate");
            rect.setFill(Color.BLACK);
            pause.play();

            recthread.sleep(5000);
            System.out.println("Hello Next step is green");
            rect.setFill(Color.GREEN);


        } catch (InterruptedException e) {
            System.out.println(e);
        }

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


        launch();
    }
}

PS: new to stackoverflow so i dont know how to add my block of code correctly sorry



Solution 1:[1]

Instead of using a pure Thread use a Task. The rectangle is set to black while the Task is running. When the Task completes, the rectangle is set to green.

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

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

    @Override
    public void start(Stage primaryStage)
    {
        Rectangle rectangle = new Rectangle(200, 100);
        rectangle.setFill(Color.GREEN);
        Task<Void> task = new Task<Void>() {
            @Override protected Void call() throws Exception {
                for (int iterations = 0; iterations < 300000; iterations++) {
                    System.out.println("running: " + iterations);
                }
                return null;
            }
        };
        task.setOnSucceeded((t) -> {
            rectangle.setFill(Color.GREEN);
            System.out.println("Stopped!");
        });
        
        Thread thread = new Thread(task);
        thread.setDaemon(true);
        rectangle.setFill(Color.BLACK);
        thread.start();
        
        
        
        StackPane root = new StackPane(rectangle);
        Scene scene = new Scene(root, 500, 500);

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

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

}

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SedJ601