我在熟悉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 );
}
}
不幸的是,旋转仅发生在其中一个轴上。我的假设是两个RotationTransition
都在运行,但是那个正在覆盖另一个应用的轮换。这是RotationTransition
的预期行为吗?
另外,如果评论以下三行:
rotationY.setNode( rect );
...
rotationX.setNode( rect );
...
fade.setNode( rect );
我得到NullPointerException
。文档建议您不需要在ParallelTransition
中包含的转换上设置节点。这是一个错误吗?
答案 0 :(得分:1)
您应该创建另一个节点,将rect添加到该节点,然后:
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 );