'Animating an imageView on the secondary Window
I'm new to the JavaFX and I want to build an application with two windows. The problem I have is that I want to animate an image on the secondary window from a button on the first window. I managed to pass arguments between the controllers but the actual animation doesn't happen except when I press a button on the secondary window.
Here is my Main:
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
//BorderPane root = new BorderPane();
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setX(50);
primaryStage.setY(50);
Stage stage2 = new Stage();
Parent root2 = FXMLLoader.load(getClass().getResource("FrontEnd.fxml"));
Scene scene2 = new Scene(root2);
scene2.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
stage2.setScene(scene2);
//stage2.initStyle(StageStyle.UNDECORATED);
stage2.setX(1000);
stage2.setY(50);
stage2.show();
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Main Controller:
public class ControllerMain implements Initializable{
private Parent rootFrontend;
private ControllerFrontend controller;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
public void But(ActionEvent e) {
System.out.println("buton apasat");
FXMLLoader loader = new FXMLLoader(getClass().getResource("FrontEnd.fxml"));
try {
rootFrontend = loader.load();
} catch (IOException err) {
err.printStackTrace();
}
controller = loader.getController();
controller.animate_intrebare(0);
}
}
Secondary controller:
public class ControllerFrontend implements Initializable{
@FXML
ImageView IntrebareA;
@FXML
ImageView IntrebareB;
@FXML
ImageView IntrebareC;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
public void animate_intrebare(int intrebare) {
TranslateTransition translate = new TranslateTransition();
if(intrebare == 0) {
System.out.println("Move A");
translate.setNode(IntrebareA);
translate.setDuration(Duration.millis(500));
translate.setByX(100);
translate.play();
}else if(intrebare == 1) {
System.out.println("Move B");
translate.setNode(IntrebareB);
translate.setDuration(Duration.millis(500));
translate.setByX(0-IntrebareB.getX());
translate.play();
}else if(intrebare == 2) {
System.out.println("Move C");
translate.setNode(IntrebareC);
translate.setDuration(Duration.millis(500));
translate.setByX(0-IntrebareC.getX());
translate.play();
}
}
public void But2() {
System.out.println("le but");
TranslateTransition translate = new TranslateTransition();
translate.setNode(IntrebareA);
translate.setDuration(Duration.millis(500));
translate.setByX(100);
translate.play();
}
}
Main FXML:
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ControllerMain">
<children>
<Button fx:id="But" layoutX="274.0" layoutY="188.0" mnemonicParsing="false" onAction="#But" text="Button" />
</children>
</AnchorPane>
Secondary FXML:
<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" style="-fx-background-color: #000000;" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.ControllerFrontend">
<children>
<ImageView fx:id="IntrebareA" fitHeight="68.0" fitWidth="399.0" layoutX="31.0" layoutY="63.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Resources/element-intrebare-A.png" />
</image>
</ImageView>
<ImageView fx:id="IntrebareB" fitHeight="107.0" fitWidth="399.0" layoutX="31.0" layoutY="125.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Resources/element-intrebare-B.png" />
</image>
</ImageView>
<ImageView fx:id="IntrebareC" fitHeight="95.0" fitWidth="399.0" layoutX="31.0" layoutY="187.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Resources/element-intrebare-C.png" />
</image>
</ImageView>
<Button fx:id="testBut" layoutX="374.0" layoutY="425.0" mnemonicParsing="false" onAction="#But2" text="ButtonFrontend" />
</children>
</AnchorPane>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
