2013-06-03 44 views
0

我想用JavaFx创建一些形状,我希望每个形状都有自己定义的TranslateTransition对象,并且我希望在点击形状时使用TranslateTransition进行播放。我的代码:Javafx,TranslateTransition,动画

形状类:

package com; 
import javafx.animation.TranslateTransition; 
import javafx.beans.property.DoubleProperty; 
import javafx.event.Event; 
import javafx.event.EventHandler; 
import javafx.scene.Parent; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.util.Duration; 

public class MShape extends Parent{ 
private TranslateTransition Transition; 
Circle circle; 
DoubleProperty tox; 
DoubleProperty toy; 
public MShape(int i, int j) { 
    circle = new Circle(20); 
    circle.setCenterX(i); 
    circle.setCenterY(j); 
    circle.setFill(Color.RED); 
    Transition= new TranslateTransition(); 
    Transition.setToX(350); 
    Transition.setToX(375); 
    Transition.setDuration(Duration.seconds(2)); 
    Transition.setNode(this.circle); 
    this.getChildren().add(circle); 
    circle.setOnMouseClicked(new EventHandler<Event>() { 

     @Override 
     public void handle(Event paramT) { 
      Transition.play(); 
      System.out.println("hello"); 
     } 
    }); 


} 

} 

主类

package com; 

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

public class Test extends Application{ 

@Override 
public void start(Stage stage) throws Exception { 
    VBox root = new VBox(); 
    stage.setScene(new Scene(root,800,600)); 

    MShape test =new MShape(100, 100); 
    root.getChildren().add(test); 
    stage.show(); 
} 
public static void main(String[] args) { 
launch(args); 
} 


} 

,当我按形状不动的问题

回答

2

不要延长Parent

这个工作对我来说:

public class Test extends Application { 
    private Circle circle; 
    private TranslateTransition transition; 

    @Override 
    public void start(Stage stage) throws Exception { 

     /** 
     * MShape 
     */ 
     circle = new Circle(20); 
     circle.setCenterX(100); 
     circle.setCenterY(100); 
     circle.setFill(Color.RED); 


     transition = new TranslateTransition(); 
     transition.setToX(500); 
     transition.setToX(500); 

     transition.setDuration(Duration.seconds(5)); 
     transition.setNode(this.circle); 

//  this.getChildren().add(circle); 

     circle.setOnMouseClicked(new EventHandler<MouseEvent>() { 
      @Override 
      public void handle(MouseEvent paramT) { 
       transition.play(); 
       System.out.println("hello " + circle.getCenterX() + " " + circle.getCenterY()); 

      } 
     }); 

     VBox root = new VBox(); 
     Scene scene = new Scene(root, 800, 600); 
     stage.setScene(scene); 

     root.getChildren().add(circle); 
     stage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
+0

谢谢tarrsalah 我做这个 transition.node解决我的问题(这一点); 而不是transition.node(this.circle); –