简体   繁体   English

JavaFX使用TimeLine暂停程序

[英]JavaFX Using TimeLine to Pause Program

I need to set my background colour of my Pane for one second and then switch it to transparent. 我需要将Pane的背景色设置一秒钟,然后将其切换为透明。 I have this set up to change the background colour, use a TimeLine with a duration of 1000ms to pause it, and then switch to transparent. 我已进行此设置来更改背景颜色,使用持续时间为1000ms的TimeLine暂停它,然后切换为透明。

The Timeline is not pausing and the program is flying past it and setting the background to transparent too quickly. 时间轴没有暂停,程序正在飞越它,并将背景设置得太快了。 Anyone know how to fix this? 有人知道怎么修这个东西吗?

I can not use thread.sleep or anything like that because of the scope of this project. 由于该项目的范围,我不能使用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);
                }

You are using Timeline as a substitute for Thread.sleep. 您正在使用时间轴代替Thread.sleep。 That is not how Timeline works. 时间轴不是这样工作的。

When you call play() on an Animation, it starts the animation in the background and returns immediately. 当您在Animation上调用play()时,它将在后台启动动画并立即返回。

Have a look at the constructors of the KeyFrame class . 看一下KeyFrame类构造函数 You need to pass a KeyValue to your KeyFrame, so a change occurs at the point in time represented by the KeyFrame: 您需要将KeyValue传递给KeyFrame,以便在KeyFrame表示的时间点发生更改:

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

As you can see, a KeyValue consists of two parts: a property, and the new value you want assigned to that property at some point during the animation. 如您所见,KeyValue由两部分组成:一个属性,以及您希望在动画过程中的某个时刻分配给该属性的新值。

  • The first at Duration.ZERO (the start), which sets the background property to your starting color. 第一个为Duration.ZERO(开始),它将background属性设置为您的开始颜色。
  • The second occurs after 1000 milliseconds have passed, and sets the background property to transparent again. 第二次发生在经过1000毫秒后,并将background属性再次设置为transparent。

By the way, you might find Duration.seconds(1) a little more readable. 顺便说一句,您可能会发现Duration.seconds(1)更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM