2012-04-21 3 views
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); 
    } 
} 

불행하게도 축 중 하나에서만 회전이 발생합니다. 내 가정은 모두 RotationTransition이 실행되고 있지만 다른 하나가 적용한 회전을 덮어 쓰는 것입니다. 이 의도 된 동작은 RotationTransition입니까? 다음 세 줄은 주석 경우에도

:

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

은 내가 NullPointerException를 얻을. 문서에서는 ParallelTransition에 포함 된 전환에 노드를 설정할 필요가 없다고 제안합니다. 이거 버그 야?

그런 다음 다른 노드가 해당 노드에 RECT를 추가 생성하고,해야
+0

시도 [설정] (http://docs.oracle.com/javafx/2.0/api/javafx/scene/ Scene.html # setCamera % 28javafx.scene.Camera % 29) a [PerspectiveCamera] (http://docs.oracle.com/javafx/2.0/api/javafx/scene/PerspectiveCamera.html)를 선택합니다. – jewelsea

+0

@jewelsea PerspectiveCamera를 사용하면 (예상대로) 원근감을 변경했지만 동시에 'RotationTransition'문제를 수정하지 않았습니다. – lifelongcoug

답변

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

+1을 수정하는 것이 간단하기 때문에 효과가있을 것입니다. 그러나, 내 질문에 대한 예제를 얻는 방법에 대해 덜 (이 같은 목표를 달성하기위한 방법이 많이 있습니다) 내 코드에 의해 전시 된 행동에 대한 이유에 대한 자세한 내용입니다. 나는 최근에 문서를 보지 않았으므로 읽은 이후로 업데이트되었을 수도 있지만 당시 나는 코드가 작동해서는 안된다. 누군가 (어쩌면 당신도)가 왜 그런지 알고 있기를 바란다. Btw, 네가 대답 할 시간이 너무 오래 걸려서 미안해. – lifelongcoug

관련 문제