JavaFX使用TimeLine暂停程序

时间:2017-03-15 15:54:40

标签: java javafx

我需要将我的Pane的背景颜色设置为一秒,然后将其切换为透明。我将此设置为更改背景颜色,使用持续时间为1000毫秒的TimeLine暂停,然后切换为透明。

时间轴没有暂停,程序飞过它并将背景设置为透明太快。任何人都知道如何解决这个问题?

由于这个项目的范围,我不能使用thread.sleep或类似的东西。

package assign3;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import javafx.animation.FillTransition;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Question2 extends Application
{

    public static final int RED = 1;
    public static final int GREEN = 2;
    public static final int BLUE = 3;
    public static final int ORANGE = 4;

    @Override
    public void start( Stage obPrimeStage ) throws Exception
    {        
        boolean runGame = true;
        int level = 1;
        BorderPane obBorder = new BorderPane();
        HBox obPane = new HBox();
        HBox obStart = new HBox();
        Button btRed = new Button("Red");
        Button btGreen = new Button("Green");
        Button btBlue = new Button("Blue");
        Button btOrange = new Button("Orange");
        Button btStart = new Button("Start");
        Timeline pause = new Timeline();
        pause.setCycleCount(1);
        pause.getKeyFrames().add(new KeyFrame(Duration.millis(1000)));


        obStart.getChildren().add(btStart);
        obPane.getChildren().addAll(btRed, btGreen, btBlue, btOrange);
        obBorder.setCenter(obPane);
        obBorder.setBottom(obStart);
        obPane.setAlignment(Pos.CENTER);
        obStart.setAlignment(Pos.CENTER);

        Scene obScene = new Scene(obBorder, 400, 400);

        obPrimeStage.setTitle("Question 2");
        obPrimeStage.setScene(obScene);
        obPrimeStage.show();

        ArrayList<Integer> colours = new ArrayList<>();
        ArrayList<Integer> guesses = new ArrayList<>();

        btStart.setOnAction((ActionEvent start) -> {
            for(int i = 0; i <= level; i++)
            {
                int randomColour = (int)((Math.random() * 4) + 1);
                randomColour = 1;

                if(randomColour == RED)
                {
                    obBorder.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
                    pause.play();
                    obBorder.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));
                    colours.add(RED);
                }

1 个答案:

答案 0 :(得分:2)

您正在使用Timeline作为Thread.sleep的替代品。这不是时间轴的工作方式。

在动画上调用play()时,它会在后台启动动画并立即返回。

查看constructors of the KeyFrame class。您需要将KeyValue传递给KeyFrame,因此在KeyFrame表示的时间点发生更改:

$data = [
    'tags' =>  '[env:prod]'

];
    $headers = [
        'Accept: application/json',
        'Content-Type: application/json'
    ];
     $tagUrl="https://app.datadoghq.com/api/v1/tags/hosts/".$host."?api_key=".$api_key."&application_key=".$app_key;

        $http_method='POST';
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_URL, $tagUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
        $out=curl_exec($ch);

如您所见,KeyValue由两部分组成:一个属性,以及您希望在动画期间的某个时刻分配给该属性的新值。

  • 第一个在Duration.ZERO(开始),它将背景属性设置为您的起始颜色。
  • 第二个在1000毫秒过后发生,并将background属性再次设置为透明。

顺便说一下,您可能会发现pause.getKeyFrames().add(new KeyFrame(Duration.ZERO, new KeyValue(obBorder.backgroundProperty(), new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY))))); pause.getKeyFrames().add(new KeyFrame(Duration.millis(1000), new KeyValue(obBorder.backgroundProperty(), new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY))))); 更具可读性。