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);
Related
I need to set an image inside the square box under the hanger, what is the best technique to set the new image over this background to exactly fit inside the box in case of small or large screen devices.
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.
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.
I was wondering how you can switch the picture being loaded into a buffered image when an action is called?
For example, if the buffered image is currently loading "Hand.png", I want to be able to press a button or a key, and have it load "Foot.png" instead. My action listeners and buffered image are set up correctly, I'm just unsure of the syntax required to change a BufferedImage.
don't change the image, create several images and display different images depending on events/actions
I'm wondering if it's possible to combine more than 1 image into a gif?
So, essentially you import 2 photos and make a gif out of them.
Thanks.
Certainly, here is a way to do this:
Create the final image as a new BufferedImage(...)
get a Graphics object on the image with image.getGraphics()
call graphics.drawImage() for each image you want to put on the larger image (you can load these images with ImageIO)
use ImageIO to write the BufferedImage out as a GIF