2016-10-22 54 views
0

我试图使用Transition类在场景生成器中创建的图形动画。 控制器:动画无法在FXMLController上工作

public class Controller{ 

@FXML 
private Line line1; 

@FXML 
private Line line2; 

@FXML 
private Line line3; 

@FXML 
private Rectangle rectangle1; 
private double mult_factor; 


private double rectangle_height; 

public Controller(){ 
    final Animation anim = new Transition() { 
     { 
      setCycleDuration(Duration.millis(3000)); 
     } 
     @Override 
     protected void interpolate(double frac) { 
      rectangle_height = rectangle1.getHeight(); 
      mult_factor = frac * 5.8; 
      rectangle1.setHeight(rectangle_height * mult_factor); 
     } 
    }; 
}} 

主类:

public class FormTest extends Application { 

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

@Override 
public void start(Stage primaryStage) throws Exception { 
    primaryStage.setTitle("The container test"); 
    primaryStage.setHeight(600); 
    primaryStage.setWidth(600); 

    Pane pane = (Pane) FXMLLoader.load(FormTest.class.getResource("conf.fxml")); 
    Controller ctr = new Controller(); 
    primaryStage.setScene(new Scene(pane)); 
    primaryStage.show(); 

}} 

FXML文件:

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="Controller"> 
    <children> 
    <Line fx:id="line1" endX="100.0" endY="300.0" layoutX="106.0" layoutY="70.0" startX="100.0" /> 
    <Line fx:id="line2" endX="100.0" endY="300.0" layoutX="300.0" layoutY="70.0" startX="100.0" /> 
    <Line fx:id="line3" endX="193.0" layoutX="207.0" layoutY="370.0" /> 
    <Rectangle fx:id="rectangle1" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="42.0" layoutX="207.0" layoutY="327.0" stroke="BLACK" strokeType="INSIDE" width="192.0" /> 
    </children> 
</Pane> 

的场景只是出现但没有任何反应,也不会发生动画。我在那里做错了什么?

回答

0

你永远不启动动画,您可以在Controllerinitialize()方法做:

public class Controller{ 

    @FXML 
    private Line line1; 

    @FXML 
    private Line line2; 

    @FXML 
    private Line line3; 

    @FXML 
    private Rectangle rectangle1; 
    private double mult_factor; 


    private double rectangle_height; 

    public void initialize() { 
     final Animation anim = new Transition() { 
      { 
       setCycleDuration(Duration.millis(3000)); 
      } 
      @Override 
      protected void interpolate(double frac) { 
       rectangle_height = rectangle1.getHeight(); 
       mult_factor = frac * 5.8; 
       rectangle1.setHeight(rectangle_height * mult_factor); 
      } 
     }; 
     anim.play(); 
    } 

} 

动画可能不会做你想让它做什么,但将在最少让它跑。

+0

嗯yea没有意识到这一点,谢谢。但现在我不明白为什么我得到一个NullPointerException在rectangle_height = rectangle1.getHeight(); –

+0

没关系,它的工作原理,我没有添加初始化方法,现​​在它的工作原理。问题是动画没有做我想做的事情,但那只是另一个小问题。 感谢您的帮助 –

1

这应该是诀窍!

public Controller(){ 
>  final Animation anim = new Transition() { 
>   { 
>    setCycleDuration(Duration.millis(3000)); 
>   } 
>   @Override 
>   protected void interpolate(double frac) { 
>    rectangle_height = rectangle1.getHeight(); 
>    mult_factor = frac * 5.8; 
>    rectangle1.setHeight(rectangle_height * mult_factor); 
>   } 
>  }; 
> anim.play(); // <- start the actual animation 
    } 
}