If you have a bigger image and it has to be loaded into your application, then it can be loaded in background. While the bigger is getting loaded, we can show a thumb nail or we can show a place holder image. Once the bigger image gets loaded into your memory, it can be shown into your application. The percentage of the image which is getting loaded can be tracked. This is shown in the following sample.
import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; /** * * @author Lawrence PremKumar */ public class ImgBackgroundLoading extends Application { public static void main(String[] args) { Application.launch(ImgBackgroundLoading.class, args); } @Override public void start(final Stage primaryStage) { primaryStage.setTitle("Hello World"); Group root = new Group(); Scene scene = new Scene(root, 600, 600); Rectangle bg = new Rectangle(); bg.setWidth(600.0); bg.setHeight(600.0); final Text progressText = new Text("0 %"); progressText.setFont(new Font(100)); progressText.setFill(Color.BLUEVIOLET); final ImageView imgView = new ImageView(); final Image img = new Image("http://www.webmastergrade.com/wp-content/uploads/2009/12/Christmas-Snow-Man.jpg" ,true); img.progressProperty().addListener(new ChangeListener<Number>() { public void changed(ObservableValue ov, Number old_val, Number new_val) { progressText.setContent((int)new_val.doubleValue() +" %"); if(new_val.doubleValue() == 100.0){ imgView.setFitHeight(600.0); imgView.setFitWidth(600.0); imgView.setImage(img); } } }); final Image tempImg = new Image(ProgressBarExample.class.getResourceAsStream("bird1.jpg")); VBox layout = new VBox(); imgView.setImage(tempImg); layout.getChildren().addAll(progressText,imgView); root.getChildren().addAll(bg,layout); primaryStage.setScene(scene); primaryStage.setVisible(true); } }
Thank you for this Lawrence.
On a related issue, we’re doing a lot of work with various types of java.awt.BufferedImages. Is there a preferred method for converting a BufferedImage into a JavaFX 2.0 Image?
Hello,
I tried to follow the tutorial but did not work,
could you help me?
public class Thumbnail extends HBox {
private ImageView imageView = new ImageView();
private Image image = null;
public Thumbnail(String caminhoImage) {
setPadding(new Insets(11));
image = new Image(“D:/images/aa.jpg”, true);
image.progressProperty().addListener(new ChangeListener() {
public void changed(ObservableValue observable, Number oldValue, Number newValue) {
if (newValue.doubleValue() == 100.0) {
imageView.setFitHeight(200.0);
imageView.setFitWidth(200.0);
imageView.setImage(image);
}
}
});
Image temp = new Image(“D:/images/temp.jpg”, true);
imageView.setImage(temp);
getChildren().add(imageView);
}
}
Hi Leandro Angelo,
There are two mistake in your code, first is setPadding(new Insets(11)); which will take four arguments and its type is double.
Second is in your image ChangeListener , since we are tracking the percentage of the image loading and percentage is a number.
Change your ChangeListener() it will work.
There is another point you have to remember, since you are trying to load the image files locally, you wn’t see the percentage of image loading. Mostly you will the final value. This is because the local files loads faster.
Thanks,
Lawrence