Updated July 1, 2023
Introduction to JavaFX Image
By the use of JavaFX image, we can display or render an image in the JavaFX application. The image is also a node of JavaFX. It supports various extensions such as jpeg, png, bmp, and gif. If we want to edit our image or create a new one, we can use an image node from JavaFX. By the use of it, we can load our custom images on the image view. JavaFX image node supports various methods to perform operations on images. also, by using them, we can reduce or increase the image size as per the requirement. In the next section, we will discuss more the image package.
Syntax:
As we discussed, in order to use the image node in our program, we have to include this package while programming. Then we can initialize the object of the image. Let’s see the syntax for a better understanding. See below;
import javafx.scene.image.Image;
Image name_variable = new Image(image_url);
As you can see in the above lines of code, we are importing the image package and creating the instance of the image. This takes one parameter as the input and which is the image URL ‘path’. Let’s see one sample syntax for a better understanding of its usage. See below;
Image myimage = new Image(https://cdn.educba.com/tem/myimage.png);
So here are just mentioning the path of the image from the specified path while creating the object.
How JavaFX Image Function works?
As of now, we know that JavaFX image is used to render or modify our image on the image view. By the use of it, we can display our custom images in the JavaFX application. Now we will discuss how to use this inside the program; in order to use this, we have to import the ‘image’ package into our program. Let’s see one sample example, and its working; see below:
Image myimage = new Image(new FileInputStream("https://cdn.educba.com/path/myimage.png"));
ImageView imageView = new ImageView(myimage);
Group grproot = new Group(imageView);
Scene myscn = new Scene(grproot, 500, 500);
stage.setTitle("any titile !!!");
stage.setScene(myscn);
stage.show();
In the above lines of code, you can see the basic snippet of the code which I required to show our image on the image view in the JavaFX application. For this, we need to import the corresponding packages into our program for exam image, Stage, Scene, ImageView, etc they will be available inside the JavaFX library only. First, we are creating the object of the image class by using the ‘new’ keyword. Inside this, we assign the absolute path of our image from the system. Then like our usual flow which, we follow to set the screen by giving the title. In this way, we can render images to the window JavaFX application.
Constructors
In the JavaFX image node, we have several constructors available by the use of it; we can initialize the image object in the JavaFX application. Let’s discuss each of them in detail; see below;
1) Image(String URL): This parameterized constructor is used to load an image from the specified path. Here we can give any path from the system
2) Image(InputStream inp): In this co-type, we pass InputStream object then it is responsible for creating an image from the InputStream content. Takes only one parameter as the input.
3) Image(String imgurl, double width, double height, boolean ratio, boolean smth): In this type of constructor, we specified several parameters; on the basis of these parameters, only it constructs our image. It takes height, width, image URL, etc.
4) Image(String imgurl, boolean bckgrd): Here, we are passing a two-parameter as the input, and on the basis of this parameter, it will create the image for us.
5) Image(String URL, double width, double height, boolean ration, boolean smt, boolean back): This will create the image with the specified parameter passed.
Methods
JavaFX image provides us with several methods to perform operations on the image. By the use of it, we can specify height, width, and other attributes for the image.
1) widthProperty(): This method is used to specify the image width. The default value for the image is 0 if it fails to load it.
2) progressProperty(): This method shows the progress of the image and how much it has loaded.
3) isError(): This returns the image’s property error.
4) isBackgroundLoading(): This method is used to check the image is being.
5) getWidth(): This method is used to get the width property.
6) getHeight(): This method is used to get the height property.
7) cancel(): This method is responsible for canceling the background image to be loaded in the background.
8) errorProperty(): This method is responsible for detecting the error.
9) exceptionProperty(): This exception makes image loading to be failed.
10) getException(): This method is issued to get the exception.
Examples of JavaFX Image
In this example, we are loading an image from the system by mentioning the path. After this use image view to display our image in the JavaFX application.
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import java.io.FileInputStream;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception{
Image myimage = new Image(new FileInputStream("C:\\Users\\sshg3k\\Downloads\\logo.jpg"));
ImageView myview = new ImageView(myimage);
myview.setX(50);
myview.setY(50);
myview.setFitHeight(300);
myview.setFitWidth(300);
myview.setPreserveRatio(true);
Group root = new Group(myview);
Scene scene = new Scene(root, 650, 530);
stage.setTitle("Demo For image in Javafx !!!");
stage.setScene(scene);
//using show method to display output !!
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}
Output:
Conclusion
JavaFX Image is used to load the image; by using it, we can modify or edit the image as well. Also, it provides us with various methods that can be used to perform various operations in the image, it is easy to see, but in order to view the image, we need to use an image viewer for this in the JavaFX application.
Recommended Articles
This is a guide to JavaFX Image. Here we discuss the definition, syntax, constructors, methods, and How JavaFX Image Function works? along with the examples respectively. You may also have a look at the following articles to learn more –