6 Replies Latest reply on Apr 30, 2012 7:54 PM by 931827

    Peoblem with backgroundloading

    931827
      Is anyone able to explain to me why when I set the properties of the image: backgroundLoading: true I have bad display (image should be in the centre of the window but it changes the position to the lower right side)?
      ImageView iv = new ImageView(new Image(imageUrl,true));
      
              iv.translateXProperty().bind((scene.widthProperty().subtract(iv.layoutBoundsProperty().get().getWidth())).divide(2));
              iv.translateYProperty().bind((scene.heightProperty().subtract(iv.layoutBoundsProperty().get().getHeight())).divide(2));
        • 1. Re: Peoblem with backgroundloading
          shakir.gusaroff
          It works for me. I have javafx2.1 GA, jdk1.7.0_04, Win 7, Netbeans 7.1.1
          The image is in the same place (center) regardless of backgroundLoading being true or false.
          import javafx.application.Application;
          import javafx.event.ActionEvent;
          import javafx.event.EventHandler;
          import javafx.scene.Scene;
          import javafx.scene.control.Button;
          import javafx.scene.layout.StackPane;
          import javafx.stage.Stage;
          import javafx.scene.image.*;
          import javafx.scene.*;
          
          
          public class ImageV extends Application {
          
              public static void main(String[] args) {
                  launch(args);
              }
              
              @Override
              public void start(Stage primaryStage) {
                  primaryStage.setTitle("Hello World!");
                
                      
                  ImageView iv = new ImageView(new Image("http://i46.tinypic.com/6ga8v5.png",true));
               
                  Group root = new Group(); 
                  Scene scene = new Scene(root, 500, 550);
                  root.getChildren().add(iv);
                
                  
                  iv.translateXProperty().bind((scene.widthProperty().subtract(iv.layoutBoundsProperty().get().getWidth())).divide(2));
                  iv.translateYProperty().bind((scene.heightProperty().subtract(iv.layoutBoundsProperty().get().getHeight())).divide(2));
                  primaryStage.setScene(scene);
                  primaryStage.show();
                  
                  
              }
          }
          1 person found this helpful
          • 2. Re: Peoblem with backgroundloading
            931827
            It works for the picture that you've sent to me. But only for this one. Can you try with that? http://www.veryicon.com/icon/png/Nature/Nature/Blue%20Flower.png
            • 3. Re: Peoblem with backgroundloading
              shakir.gusaroff
              You are right. For simple images it works because the loading time is small.
              The problem is in layoutBoundsProperty. Add the following statement
              and you can see the problem.
                 System.out.print(iv.layoutBoundsProperty().get().getHeight());
              When backgroundLoading = true, the output is 1 //It is wrong
              When backgroundLoading = false, the output is 256

              When backgroundLoading = true, the calculation is incorrect.
              It starts calculation before the loading is finished. I think this is a bug.

                import javafx.application.Application;
              import javafx.event.ActionEvent;
              import javafx.event.EventHandler;
              import javafx.scene.Scene;
              import javafx.scene.control.Button;
              import javafx.scene.layout.StackPane;
              import javafx.stage.Stage;
              import javafx.scene.image.*;
              import javafx.scene.*;
               
               
              public class ImageV extends Application {
               
                  public static void main(String[] args) {
                      launch(args);
                  }
                  
                  @Override
                  public void start(Stage primaryStage) {
                      primaryStage.setTitle("Hello World!");
                    
                       String url = "http://www.veryicon.com/icon/png/Nature/Nature/Blue%20Flower.png";
                    
                      
                      ImageView iv = new ImageView(new Image(url,false));
                   
                      Group root = new Group(); 
                      Scene scene = new Scene(root, 500, 550);
                      root.getChildren().add(iv);
                    
                      
                      iv.translateXProperty().bind((scene.widthProperty().subtract(iv.layoutBoundsProperty().get().getWidth())).divide(2));
                      iv.translateYProperty().bind((scene.heightProperty().subtract(iv.layoutBoundsProperty().get().getHeight())).divide(2));
                      
                     
                     
                      
                      primaryStage.setScene(scene);
                      primaryStage.show();
                       System.out.print(iv.layoutBoundsProperty().get().getHeight());
                      
                      
                  }
              }
              • 4. Re: Peoblem with backgroundloading
                931827
                Thank you so much! You solve my problem :)

                I add 1 and it works fine but I agree with you that's a bug. Anyway I won't try this without your tip :) :
                iv.translateYProperty().bind((scene.heightProperty().subtract(iv.layoutBoundsProperty().get().getHeight()).add(1)).divide(2));
                Thank you very much for your time! :))
                • 5. Re: Peoblem with backgroundloading
                  Evak-Oracle
                  Hi,
                  This is not a bug, layoutBounds are recalculated after the image is loaded.
                  The reason why your binding this doesn't work is that iv.layoutBoundsProperty().get().getWidth() doesn't return an ObservableValue (but plain double), so your binding doesn't actually listen on layoutBounds changes. To make it dependent on layoutBounds, you could write it this way:
                  iv.translateXProperty().bind(new DoubleBinding() {
                      { super.bind(scene.heightProperty(), iv.layoutBoundsProperty()); }
                  
                      @Override
                      protected double computeValue() {
                          return (scene.getWidth() - iv.getLayoutBounds().getWidth()) / 2;
                      }
                  });
                  By the way, if you just want your image centered, it's easier to use layouts for that - if you use StackPane instead of Group for root node you won't need to calculate the position of the image.
                  1 person found this helpful
                  • 6. Re: Peoblem with backgroundloading
                    931827
                    Thanks for your explanation :)