Introduction to JavaFX Image
By the use of JavaFX image, we can display or render an image in JavaFX application. Image is also a node of JavaFX. It supports various extension such as jpeg, png, bmp, and gif. If we want to edit our image or want to create a new image, then we can use image node form JavaFX. By the use of it, we can load our custom images on the imageview. JavaFX image node supports various methods to perform operation on image also, by the use of them we can reduce or increase the size of the image as per the requirement. In the next section, we will discuss more the image package.
Syntax:
As we discussed in order to use image node in our program we have to include this package while programming. Then we can initialize the object of image. Let’s see syntax for 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(/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 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("/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 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 ‘new’ keyword inside this we are assigning the absolute path of our image from system. After this, we used imageView to view our image so this is required here, w just need to pass the instance of the image. Then like our usual flow which we follow to set the screen by giving the title. In this way, we can render image to 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 JavaFX application. In any programming, language constructors are used to create the object of the class. Let’s discuss each of them in detail see below;
1) Image(String URL): This is a parameterized constructor that 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 responsible to create an image from the InputStream content. Takes only one parameter as the input.
3) Image(String imgurl, double width, double height, boolean ration, boolean smth): In this type of constructor we specified several parameter, on the basis of these parameter only it construct our image. It takes height, width, image URL, etc.
4) Image(String imgurl, boolean bckgrd): Here we are passing two-parameter as the input and on the basis of these 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 several methods to perform operation on the image. By the use of it, we can specify height, width and other attributes for the image.
1) widthProperty() : This methods used to specify the image width. The default value for the image is 0 if it failes to load it.
2) progressProperty(): This method shows the progress of the image how much it has loaded.
3) isError(): This return the property error of the image.
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 to cancel the background image to be loaded in the background.
8) errorProperty(): This method is responsible to detect the error.
9) exceptionProperty(): This exception which makes image loading to be failed.
10) getException(): This method 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 using image view to display our image in 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 the use of it we can modify or edit the image as well. Also, it provides us 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 image viewer for this in 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 –