Search code examples
javaanimationjavafx-8fxmlscenebuilder

Why am I NOT getting any scale transition in Java FX.?


I have been trying to get Scale Transition using Java Fx for an imageView . The code that I have written seems to be fine. But when i run this code I am not getting any transition effect and no errors too, the image stay in its respective position . Can somebody help me ???

In the below code I want to do scale transition for the image resumeBtn.

import java.io.IOException;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;

public class TryFXML extends Application{

    @Override
    public void start(Stage stage) throws IOException  {
        // TODO Auto-generated method stub
        try {
            Parent root = FXMLLoader.load(getClass().getResource("PauseScreen.fxml"));
            Scene scene=new Scene(root);
            stage.setScene(scene);
            stage.setTitle("Pause Screen");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    
    stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

My Controller class:

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.annotation.Resource;

import javafx.animation.AnimationTimer;
import javafx.animation.ScaleTransition;
import javafx.event.ActionEvent;
public class TimeController {
    
    
    
     @FXML
        private ImageView resumeBtn;
     @FXML
        private ImageView restartBtn;
     @FXML
        private ImageView saveBtn;
    
     
     
     
    @FXML
    public void initialize(Stage stage) throws IOException {
        
        
 
        ScaleTransition rsm=new ScaleTransition(Duration.seconds(5),resumeBtn);
        rsm.setAutoReverse(true);
        rsm.setCycleCount(1000);
        rsm.setFromX(1);
        rsm.setToX(4);
        rsm.setFromY(1);
        rsm.setToY(4);
        rsm.play();
        
        
    }
    
    

    
    @FXML
    void restartClicked(MouseEvent event) {
        System.out.println("you clicked here 1");
    }
    
    @FXML
    void saveClicked(MouseEvent event) {
        System.out.println("You clicked here 2");
    }
}

My FXML code::

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.effect.ColorAdjust?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.effect.Light.Distant?>
<?import javafx.scene.effect.Lighting?>
<?import javafx.scene.effect.Shadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="850.0" prefWidth="580.0" style="-fx-background-color: rgb(41,41,41);" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TimeController">
   <children>
      <ImageView fx:id="resumeBtn" accessibleRole="BUTTON" accessibleRoleDescription="Button " fitHeight="200.0" fitWidth="200.0" layoutX="190.0" layoutY="104.0" style="-fx-scale-x: 1;">
         <image>
            <Image url="@Image/PlayButton.png" />
         </image>
         <effect>
            <InnerShadow choke="0.32" color="#5e5959" height="22.56" radius="11.1675" width="24.11" />
         </effect>
      </ImageView>
      <ImageView fx:id="restartBtn" fitHeight="200.0" fitWidth="200.0" layoutX="190.0" layoutY="354.0" onMouseClicked="#restartClicked">
         <image>
            <Image url="@Image/RestartButton.png" />
         </image>
         <effect>
            <Lighting diffuseConstant="1.63" specularConstant="0.15" specularExponent="40.0">
               <bumpInput>
                  <Shadow />
               </bumpInput>
               <light>
                  <Light.Distant />
               </light>
            </Lighting>
         </effect>
      </ImageView>
      <ImageView fx:id="saveBtn" fitHeight="168.0" fitWidth="166.0" layoutX="212.0" layoutY="611.0" onMouseClicked="#saveClicked">
         <image>
            <Image url="@Image/SaveButton.png" />
         </image>
         <effect>
            <ColorAdjust brightness="0.45" contrast="0.09" hue="1.0" saturation="0.02" />
         </effect>
      </ImageView>
   </children>
</Pane>

Solution

  • From the documentation of javafx.fxml.Initializable:

    FXMLLoader will now automatically call any suitably annotated no-arg initialize() method defined by the controller.

    Notice the no-arg part. You need to define an initialize() method which accepts zero arguments. Your current initialize method does not meet the stated requirement, so it is never being called.

    Change this:

    public void initialize(Stage stage) throws IOException {
    

    to this:

    public void initialize() throws IOException {