我正在尝试使用Transition类对在Scene Builder中创建的图形进行动画处理。控制器:
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>
场景只是出现,但什么也没有发生,动画也没有发生。我在那里做错了什么?
您永远不会启动动画,可以使用Controller
的initialize()
方法进行动画处理:
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();
}
}
动画可能没有按照您想要的去做,但这至少会使它运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句