【问题标题】:How to serialise events in javafx?如何在 javafx 中序列化事件?
【发布时间】:2015-02-08 13:30:54
【问题描述】:

我正在尝试创建一个 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();
    }

}

【问题讨论】:

    标签: java animation javafx


    【解决方案1】:

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

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

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

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

    并将其添加到动画中:

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

    【讨论】:

      猜你喜欢
      • 2012-06-17
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多