Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this article, we will learn about the JavaFX images in detail. Using the classes given by JavaFX in the package javafx.scene.image, you can load and manipulate images. JavaFX ImageView is used to paint images and load them using the Image class. Image formats including BMP, GIF, JPEG, and PNG are supported by JavaFX. It would help if you had prerequisites for programming in the Java language.
Let's dive into the article to get more information about JavaFX images.
JavaFX Images
JavaFX ImageView is used to paint images and load them using the Image class. The user can resize the displayed image using the ImageView class without changing the aspect ratio or the image's pixels. We must first build the Image class to load this item into the ImageView class in JavaFX. The scene.image.ImageView package contains the ImageView class, which must be imported to utilize it. We must also import the JavaFX.scene.image to use the image class.Image package, which contains it.
Advantage: We can include the photographs without changing their aspect ratio or pixel size.
Adding a Picture
By creating an instance of the Image class from the JavaFX.scene.image package, you can load an image in JavaFX.
The class's construction must receive one of the following arguments:
a loadable image's InputStream object or,
a string variable containing the image's URL.
Constructors:
ImageView(): Create an ImageView object using the ImageView() method without a constructor argument.
ImageView(Image image): Create an ImageView object using the constructor with the input image.
ImageView(String URL): Construct an ImageView object by passing a string representation of the image's URL to its constructor.
Most common methods:-
setImage(image): The image object is set using setImage().
setFitWidth(): Set the image width with setFitWidth().
setPreserveRatio(true/false): Set the image preserved ratio with setPreserveRatio() or setPreserveRatio(true/false). False is the default setting.
setSmooth(true/false): Set the smoothness of the image with setSmooth() by calling setSmooth(true/false). False is the default setting.
Set the picture cache with setCache() by using the setCache(true/false) method. False is the default setting.
setFill(Color.BLACK): The fill color of an image is set using the setFill() method.
add(): The JavaFX elements are added using the add() method.
show(): The output is displayed using show().
How may an ImageView be created in JavaFX?
User-developed classes that access JavaFX features must extend the Application.
Step 1: Use new to instantiate an image.
Step 2: The second step entails creating an ImageView and assigning an image to the image view.
Step 3: To add the elements, create a VBox or alternative display class (such as a telephone or HBox depending on the necessity).
Step 4: Setting up the scene before applying the show method.
Step 5: Add the Scene reference screen to the Stage object reference. Screen for output is added to Stage. This stage object reference will be obtained via the start predefined JavaFX method.
Step 6: Applying the show() method to the scene object to display output to the user.
Example 1
The example that follows shows how to load an image and set the view in JavaFX.
Program:-
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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;
public class App extends Application {
@Override
public void start(Stage stage) throws FileNotFoundException {
//Creating an image
Image img = new Image(new FileInputStream("C:\\434329.jpg"));
//Setting the image view
ImageView imageView = new ImageView(img);
//Setting the position of the image
imageView.setX(60);
imageView.setY(30);
//setting the fit height and width of the image view
imageView.setFitHeight(500);
imageView.setFitWidth(550);
//Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);
Group root = new Group(imageView);
Scene scene = new Scene(root, 700, 500);
stage.setTitle("Loading an image");
stage.setScene(scene);
//Displaying the contents of stage
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}
You can also try this code with Online Java Compiler
Additionally, you can specify different viewpoints for the same scene's image. The following application serves as an illustration of how to use JavaFX to set different perspectives for an image within a scene.
Program:-
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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;
public class App extends Application {
@Override
public void start(Stage stage) throws FileNotFoundException {
//Creating an image
Image image = new Image(new FileInputStream("C:\\434329.jpg"));
//Setting the image view 1
ImageView imageView1 = new ImageView(image);
imageView1.setX(60);
imageView1.setY(30);
imageView1.setFitHeight(400);
imageView1.setFitWidth(350);
imageView1.setPreserveRatio(true);
//Setting the image view 2
ImageView imageView2 = new ImageView(image);
imageView2.setX(450);
imageView2.setY(30);
imageView2.setFitHeight(150);
imageView2.setFitWidth(350);
imageView2.setPreserveRatio(true);
//Setting the image view 3
ImageView imageView3 = new ImageView(image);
imageView3.setX(350);
imageView3.setY(200);
imageView3.setFitHeight(100);
imageView3.setFitWidth(100);
imageView3.setPreserveRatio(true);
Group root = new Group(imageView1, imageView2, imageView3);
Scene scene = new Scene(root, 800, 400);
stage.setTitle("Multiple views of an image");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}
You can also try this code with Online Java Compiler
Using the classes given by JavaFX in the package JavaFX. scene. image, you can load and manipulate images. JavaFX supports image formats including BMP, GIF, JPEG, and PNG. You will learn how to load images into JavaFX, project an image in different views, and change an image's pixels in this chapter.
Can a button using JavaFX have an image added?
The setGraphic() function of the Button class adds a graphic object (node) to a button.
How do I include a picture in a JavaFX scene?
A FileInputStream should represent the image you want to load. You bypass the input stream object you just generated and send it as a constructor parameter, instantiate the Image class instead. Create an instance of the ImageView class. By providing the image object as a parameter to the setImage() method, you can set the image to it.
Conclusion
In this article, we have extensively discussed the JavaFX images. In detail, we will discuss adding the image and writing pixels with the help of various programs.