How to organize images in Gridpane? [duplicate] - java

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?

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.

Related

Make image as tall as text

I have a Button with text and an ImageView as icon. The image might be of any resolution. Currently, the button will adapt its size to fit the image, e.g. if the image is 400x400, the button will be huge and the text will be tiny next to it.
What I want though, is the image to fit into the button. It should always be as tall as the text is.
ImageView img = new ImageView(image);
button = new Button("Some text", img);
button.setStyle("-fx-background-radius: 6em;");
Wouldn't have thought the solution to this is so easy:
img.setPreserveRatio(true);
img.setFitHeight(button.getFont().getSize());
does the trick for me. JavaFX is weirdly unintuitive sometimes. If the font size may change (e.g. via CSS styling), use a binding instead:
img.fitHeightProperty().bind(Bindings.createDoubleBinding(() -> button.getFont().getSize(), button.fontProperty()));
You can set your image height via
image_view.getLayoutParams().height = YOURHEIGHT
as discussed here.
I would suggest setting your text size to an appropriate value and use the above command to scale the image to the same height.
Test around whether you can size the image with em, otherwise use cm/inch.
If there a problems with image distortion when setting height, refer to this thread.

Change the size of the image inside the javafx interface

I have got an image in my javafx software and I want to define its size from my code. The code where with which I am reading the pic is the following:
image= new Image(gui.getClass().getResourceAsStream("img.gif"));
imageIMG.setGraphic(new ImageView(image));
I want to define the size of this image in my GUI. When I am trying to setup the size I found out that I cannot use a method like image.setHeight/Width. Why I cannot define the size of the image? Is there any alternative way to do so?
EDIT: This is what I am performing to load the image in my graphics.
An Image in JavaFX is only a container for the image data. You can scale the image at loading time (using another constructor) but this change is permanent (but can be useful to save memory).
When you want to display an Image, you wrap it in an ImageView. This class has methods to scale the image dynamically, depending on where you need it. It won't change the underlying data.
Image can be re-used. You can use the same Image in multiple ImageViews and show it at different sizes without having to have multiple copies of the same image loaded.
Update
image= new Image(gui.getClass().getResourceAsStream("img.gif"));
ImageView iv = new ImageView(image);
iv.setFitWidth(100); // Change size
imageIMG.setGraphic(iv);

Drawing an image using its full size rather than asking me for its dimensions in JavaFX

I am loading an external image with JavaFX:
Image tile = new Image(imageFile.toURI().toURL().toString(),width, height, true,true);
You need to supply the width and height. How can I get the image's width and height?
Now, I know that the width and height of a PNG image are determined by the bytes from 12 to 20 or something like that, so technically you could open a byte stream and interpret the bytes. However, this seems overkill for the simple task of drawing an image in my JavaFX application. Is there not a way to draw an image with its full size without asking me to supply the dimensions?
Just create the Image using a constructor which does not include dimensions (e.g. new Image(url)):
Image image = new Image(url);

ImageView doesn't stretch image properly

So I have no clue why this is happening. I am using Universal Image Loader to load these images. It seems like the last line of pixels is being streched for some weird reason. I want the image to just stretch out evenly. (I don't care that it will look weird. The images below are for demo purposes.)
Also, don't mind the first and last image. I purposely blurred that out because it had someone's face on it.
This is how I set up my Universal Image Loader:
//setup Image Loader for loading cruise line logos
displayImageOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)//show this image when image is loading
.showImageForEmptyUri(R.drawable.ic_launcher)//show this image incase image doesn't exist
.showImageOnFail(R.drawable.ic_launcher)//show this image if fetching image from URL didn't work
.cacheInMemory(true)//cache image in RAM
.cacheOnDisc(true)//cache image in device for later use
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(5))//super subtle rounded corners on images
.build();
This is caused by the way RoundedBitmapDisplayer draws the bitmap.
If you look at the source, you'll see that it uses a RoundedDrawable, which uses canvas.drawRoundRect() to draw a rounded rectangle of the desired size of the Drawable, using the downloaded image as the texture via a BitmapShader. BitmapShader does not support scaling (only clamping and tile modes). Try using a SimpleBitmapDisplayer instead which uses the normal ImageView.setImageBitmap() way of displaying the image.
If you need rounded corners, you'll have to find a different way to implement that, for example by scaling the Bitmap to the desired size first. Another option is to call Canvas.saveLayer() before delegating to BitmapDrawable for the scaling, and then applying the rounded corner masking effect using PorterDuff.Mode.DST_IN. Either way you'll end up writing a bit more low-level code, but you should be able to encapsulate everything nicely in a custom BitmapDisplayer.

how to create a bitmap which is composed multiple images?

I have a few pictures, i know their position and size... How do I place my images in one ImageView? (as shown in the picture below)
you have two options:
create a relative layout, include 4 images views and set one image to each bitmap
that's a bit more complex, use Bitmap.createBitmap(int width, int height, Bitmap.Config config); to create a mutable bitmap with the size you want, create a new canvas with new Canvas(bitmap); to create a canvas into that bitmap, and use canvas.drawBitmap(...) to draw the 4 bitmaps into the final one.

Categories