Change javafx circles color in certain time using multithreads

前端 未结 3 562
情深已故
情深已故 2021-01-27 18:45

I am creating traffic light simulator with javafx that changes colour in every 2 seconds (first red light blinks and remain for 2 seconds) using the concept of multithreading. I

3条回答
  •  没有蜡笔的小新
    2021-01-27 19:37

    Here is an MCVE that uses Timeline. To get different durations use @fabian Timeline example. This example has a 2-second duration for all lights.

    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 TrafficLightsFX extends Application
    {
    
        String currentLight = "GREEN";
    
        @Override
        public void start(Stage primaryStage)
        {
            //Light GUI
            //https://www.color-hex.com/color-palette/35021
            String COLOR_GREEN_DARK = "#008000";
            String COLOR_GREEN = "47C746";
            String COLOR_YELLOW_DARK = "CA7602";
            String COLOR_YELLOW = "FFFF40";
            String COLOR_RED_DARK = "A30504";
            String COLOR_RED = "FF0000";
    
            Circle red = new Circle(25, Color.valueOf(COLOR_RED_DARK));
            Circle green = new Circle(25, Color.valueOf(COLOR_GREEN_DARK));
            Circle yellow = new Circle(25, Color.valueOf(COLOR_YELLOW_DARK));
    
            VBox trafficLight = new VBox(red, yellow, green);
    
            Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(2), (ActionEvent event) -> {
                switch (currentLight) {
                    case "GREEN":
                        red.setFill(Color.valueOf(COLOR_RED_DARK));
                        green.setFill(Color.valueOf(COLOR_GREEN));
                        currentLight = "YELLOW";
                        break;
                    case "RED":
                        yellow.setFill(Color.valueOf(COLOR_YELLOW_DARK));
                        red.setFill(Color.valueOf(COLOR_RED));
                        currentLight = "GREEN";
                        break;
                    case "YELLOW":
                        green.setFill(Color.valueOf(COLOR_GREEN_DARK));
                        yellow.setFill(Color.valueOf(COLOR_YELLOW));
                        currentLight = "RED";
                        break;
                }
            }));
            timeline.setCycleCount(Timeline.INDEFINITE);
    
            Button btn = new Button();
            btn.setText("Start");
            btn.setOnAction((ActionEvent event) -> {
                switch (timeline.getStatus()) {
                    case STOPPED:
                    case PAUSED:
                        timeline.play();
                        break;
                    case RUNNING:
                        timeline.pause();
                        break;
                }
            });
    
            VBox root = new VBox(new StackPane(trafficLight), btn);
    
            Scene scene = new Scene(root, 300, 250);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
            launch(args);
        }
    
    }
    

提交回复
热议问题