Animation not working on FXMLController

Im trying to animate a figure created in Scene Builder, using the Transition class. Controller:

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);
        }
    };
}}

Main Class:

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 File:

<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>

The Scene just appears but nothing happens, the animation does not occur. What am I doing wrong there?

Upvotes: 0

Views: 168

Answers (2)

James_D
James_D

Reputation: 209225

You never start the animation, which you can do in the Controller's initialize() method:

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();
    }

}

The animation probably isn't doing what you want it to do, but that will at least make it run.

Upvotes: 0

a-sir
a-sir

Reputation: 74

This should do the trick!

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
   }
}

Upvotes: 1

Related Questions