Feeds:
Posts
Comments

Archive for July, 2011

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.

InitialImage

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);
    }
}

lastimg

Read Full Post »