Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resize an imageview image in javafx?

I need to resize an image to specific dimensions, 100 by 100 pixels for example, in JavaFX.

How can I achieve that? Could the Image or the ImageView class be used for this purpose?

like image 253
Jeremy Avatar asked Jan 12 '15 03:01

Jeremy


People also ask

What is ImageView in JavaFX?

The ImageView is a Node used for painting images loaded with Image class. This class allows resizing the displayed image (with or without preserving the original aspect ratio) and specifying a viewport into the source image for restricting the pixels displayed by this ImageView .

Does JavaFX support PNG?

JavaFX supports the image formats like Bmp, Gif, Jpeg, Png. This chapter teaches you how to load images in to JavaFX, how to project an image in multiple views and how to alter the pixels of an image.

How do I center an image in JavaFX?

getChildren(). add(imgView); That'll put an image in an empty HBox you can align to the horizontal center of the canvas.


1 Answers

Yes, using an ImageView. Just call

ImageView imageView = new ImageView("..."); imageView.setFitHeight(100); imageView.setFitWidth(100); 

By default, it will not preserve the width:height ratio: you can make it do so with

imageView.setPreserveRatio(true); 

Alternately you can resize the Image directly on loading:

Image image = new Image("my/res/flower.png", 100, 100, false, false); 

Resizing the image on loading is useful for things like thumbnails of larger images as the memory required is lower than storing the larger image data representation in memory.

like image 103
James_D Avatar answered Sep 18 '22 17:09

James_D