i am trying to run simple JavaFx(2.0) application which will show three images as thumbnails. OS is Windows7 and i am using NetBeans 7.2
Code is this-
public class FX17 extends Application{
public static void main(String[] args){
launch(args);
}
public void start(Stage stage){
HBox photoBar = new HBox();
Group root = new Group();
File f1 = new File("C:\\Users\\Pictures\\IMG_0021.jpg");
File f2 = new File("C:\\Users\\Pictures\\IMG_0022.jpg");
File f3 = new File("C:\\Users\\Pictures\\IMG_0023.jpg");
Image i1 = new Image(f1.toURI().toString());
Image i2 = new Image(f2.toURI().toString());
Image i3 = new Image(f3.toURI().toString());
ImageView iv1 = new ImageView(i1);
//iv1.setImage(i1);
iv1.setFitWidth(50);
iv1.setPreserveRatio(true);
iv1.setCache(true);
ImageView iv2 = new ImageView(i2);
//iv2.setImage(i2);
iv2.setFitWidth(50);
iv2.setPreserveRatio(true);
iv2.setCache(true);
ImageView iv3 = new ImageView(i3);
// iv3.setImage(i3);
iv3.setFitWidth(50);
iv3.setPreserveRatio(true);
iv3.setCache(true);
photoBar.getChildren().add(iv1);
photoBar.getChildren().add(iv2);
photoBar.getChildren().add(iv3);
//C:\Users\Public\Pictures\Sample Pictures
BorderPane pane = new BorderPane();
pane.setTop(photoBar);
root.getChildren().add(photoBar);
//pane.setLeft(linkBar);
Scene scene = new Scene(root);
scene.setFill(Color.BLACK);
stage.setScene(scene);
stage.setWidth(415);
stage.setHeight(200);
stage.sizeToScene();
stage.show();
}
}
For two images program runs and shows two thumbnails but for 3 or more images, program throws OutOfMemoryError. Image is jpg and average size is 2.5MB. Are there some setting or image format i need to check ?
Following constructor works fine for me.
Image img = new Image(file.toURI().toString(),100,100,false,false);aspect ratio,smooth is false.
I guess your jpgs are very large in terms of height and width.
The memory taken by a jpg versus the loaded image buffer is different. The loaded image is a decoded uncompressed bitmap and depends on the image height/width/color depth rather than the original jpg file size.
To fix your OutOfMemory condition:
Specifying a size in the image constructor ensures that, once the image is loaded, Java only keeps a buffer large enough to hold the resized image rather than the full size, uncompressed image.