如何在javafx中序列化事件?

时间:2015-02-08 13:30:54

标签: java animation javafx

我正在尝试创建一个JavaFX应用程序。作为其中的一部分,我必须绘制一个圆圈,如果按下按钮则会发生动画,之后我必须绘制另一个圆圈。但是在动画结束之前绘制了第二个圆圈。我试试这一段时间没有运气。任何提示都表示赞赏。

这里draw()将绘制初始圆,play()将执行动画,drawAfter()必须在play()之后发生,但它在play()完成之前发生。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import javafx.scene.shape.Path;
import javafx.scene.shape.*;
import javafx.animation.PathTransition;
import javafx.animation.PathTransition.OrientationType;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;



public class CircularPlay extends Application {

    public static void main(String[] args) {
        launch(args);
    }
    TextField tf=new TextField("Write a number");
    Group root = new Group();
    Scene scene = new Scene(root, 800, 600, Color.WHITE);
    Button bt=new Button("Start");
    Label label=new Label("change this");
    Rectangle rect = new Rectangle (50, 50, 5, 5);
    Canvas canvas = new Canvas(600, 400);
    GraphicsContext gc = canvas.getGraphicsContext2D();



    @Override
    public void init()
    {
        bt.setLayoutX(650);
        bt.setLayoutY(50);
        label.setLayoutX(650);
        label.setLayoutY(100);
        rect.setArcHeight(50);
        rect.setArcWidth(50);
        rect.setFill(Color.VIOLET);

    }
    private void draw()
    {
        gc.strokeOval(50, 0, 100, 100);
        gc.rect(1, 1, 599, 399);
        gc.stroke();
    }
    private void drawAfter()
    {
        gc.strokeOval(100, 50, 100, 100);
    }
    private void play()
    {
        Path path = new Path();

        path.getElements().add (new MoveTo (50, 50));
        path.getElements().add (new ArcTo(50,50,90,100,100,true,true));

        PathTransition pathTransition = new PathTransition();

        pathTransition.setDuration(Duration.millis(2000));
        pathTransition.setNode(rect);
        pathTransition.setPath(path);
        pathTransition.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
        pathTransition.setCycleCount(1);
        pathTransition.play();
    }

    @Override
    public void start(Stage primaryStage)
    {

        primaryStage.setScene(scene);

        root.getChildren().add(canvas);
        root.getChildren().add(rect);
        root.getChildren().add(bt);
        root.getChildren().add(label); 

        draw();

        bt.setOnAction((ActionEvent e) -> {
            label.setText("Accepted");
            play();
            drawAfter();
        });  

        primaryStage.show();
    }

}

1 个答案:

答案 0 :(得分:0)

您需要等到动画结束才能绘制第二个圆圈。

为此,每个动画都有一个方法:setOnFinished()

在您的情况下,从按钮侦听器中删除drawAfter(()

bt.setOnAction(e -> {
        label.setText("Accepted");
        play();
    });

并将其添加到动画中:

private void play(){
    ...
    PathTransition pathTransition = new PathTransition();
    ...
    pathTransition.setOnFinished(e->drawAfter());
    pathTransition.play();
}