1

在熟悉 JavaFX 2.0 时,我一直在玩动画,我编写了一个小测试程序,旨在沿 X 轴和 Y 轴旋转一个矩形。这是测试程序:

import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class ParallelTransitionTest extends Application
{
    public static void main( String[] args )
    {
        launch( args );
    }

    @Override
    public void start( Stage primaryStage ) throws Exception
    {
        init( primaryStage );
        primaryStage.show();
    }

    private void init( Stage primaryStage )
    {
        primaryStage.setTitle( "Parallel Transition" );
        primaryStage.setResizable( true );

        // Create the scene
        BorderPane root = new BorderPane();
        Scene scene = new Scene( root, 800, 600, true );
        scene.setFill( Color.BLACK );
        primaryStage.setScene( scene );

        Rectangle rect = RectangleBuilder.create()
                .width( 100 ).height( 100 )
                .x( 350 ).y( 250 )
                .fill( Color.BLUE )
                .build();

        RotateTransition rotationY = new RotateTransition();
        rotationY.setAxis( Rotate.Y_AXIS );
        rotationY.setDuration( Duration.seconds( 5 ) );
        rotationY.setByAngle( 360 );
        rotationY.setNode( rect );
        rotationY.setAutoReverse( true );
        rotationY.setCycleCount( Animation.INDEFINITE );

        RotateTransition rotationX = new RotateTransition();
        rotationX.setAxis( Rotate.X_AXIS );
        rotationX.setDuration( Duration.seconds( 5 ) );
        rotationX.setByAngle( 360 );
        rotationX.setNode( rect );
        rotationX.setAutoReverse( true );
        rotationX.setCycleCount( Animation.INDEFINITE );

        FadeTransition fade = new FadeTransition();
        fade.setDuration( Duration.seconds( 5 ) );
        fade.setToValue( 0.2 );
        fade.setNode( rect );
        fade.setAutoReverse( true );
        fade.setCycleCount( Animation.INDEFINITE );

        ParallelTransition transition = new ParallelTransition( rect,
                rotationX, rotationY, fade );
        transition.setAutoReverse( true );
        transition.play();

        root.getChildren().add( rect );
    }
}

不幸的是,旋转只发生在其中一个轴上。我的假设是两个RotationTransitions 都在运行,但是一个正在覆盖另一个应用的旋转。这是 的预期行为RotationTransition吗?

此外,如果注释以下三行:

        rotationY.setNode( rect );
        ...
        rotationX.setNode( rect );
        ...
        fade.setNode( rect );

我得到一个NullPointerException. 文档建议您不需要在ParallelTransition. 这是一个错误吗?

4

1 回答 1

1

您应该创建另一个节点,将您的矩形添加到该节点,然后:

RotateTransition rotationY = new RotateTransition();
        rotationY.setAxis( Rotate.Y_AXIS );
        rotationY.setDuration( Duration.seconds( 5 ) );
        rotationY.setByAngle( 360 );
        rotationY.setNode( rect );
        rotationY.setAutoReverse( true );
        rotationY.setCycleCount( Animation.INDEFINITE );

        RotateTransition rotationX = new RotateTransition();
        rotationX.setAxis( Rotate.X_AXIS );
        rotationX.setDuration( Duration.seconds( 5 ) );
        rotationX.setByAngle( 360 );
        rotationX.setNode( NEWNODE );
        rotationX.setAutoReverse( true );
        rotationX.setCycleCount( Animation.INDEFINITE );
于 2012-09-11T10:10:43.447 回答