JavaFX new Image
Introduction
In JavaFX, the Image
class is used to represent and manipulate images. It allows developers to load images from various sources and perform various operations on them, such as resizing, cropping, and applying filters. This article will provide a comprehensive guide on how to use the Image
class in JavaFX, along with code examples.
Loading an Image
To load an image in JavaFX, you can use the Image
constructor. The constructor requires a valid URL or file path to the image file. Here's an example code snippet:
import javafx.scene.image.Image;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class ImageExample extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// Load the image from a file
Image image = new Image(file:path/to/image.png);
// Display the image using an ImageView
ImageView imageView = new ImageView(image);
// Create a scene with the ImageView
Scene scene = new Scene(imageView);
// Set the scene and show the stage
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
In the code above, we create an Image
object by passing the file path of the image to the constructor. We then create an ImageView
and set the Image
as its content. Finally, we create a Scene
with the ImageView
and display it in a Stage
using the start
method.
Resizing an Image
To resize an image in JavaFX, you can use the Image
class's getWidth
and getHeight
methods to get the original dimensions of the image. You can then create a new Image
object with the desired dimensions using the new Image
constructor. Here's an example code snippet:
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ImageExample extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// Load the image from a file
Image originalImage = new Image(file:path/to/image.png);
// Resize the image to half of its original dimensions
double newWidth = originalImage.getWidth() / 2;
double newHeight = originalImage.getHeight() / 2;
Image resizedImage = new Image(file:path/to/image.png, newWidth, newHeight, true, true);
// Display the resized image using an ImageView
ImageView imageView = new ImageView(resizedImage);
// Create a scene with the ImageView
Scene scene = new Scene(imageView);
// Set the scene and show the stage
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
In the code above, we first create an Image
object from the original image file. We then calculate the new dimensions for the resized image by dividing the original dimensions by 2. Finally, we create a new Image
object with the new dimensions and display it using an ImageView
.
Applying Filters to an Image
JavaFX provides various filters that you can apply to an image to achieve different visual effects. The Image
class's getPixelReader
method returns a PixelReader
object, which allows you to read the pixel data of an image. You can then use the PixelReader
to apply filters to the image. Here's an example code snippet:
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.WritableImage;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.effect.GaussianBlur;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ImageExample extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// Load the image from a file
Image originalImage = new Image(file:path/to/image.png);
// Apply a color adjustment filter to the image
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setHue(0.3);
colorAdjust.setBrightness(0.2);
colorAdjust.setContrast(0.9);
colorAdjust.setSaturation(0.8);
ImageView filteredImageView = applyFilter(originalImage, colorAdjust);
// Apply a Gaussian blur filter to the image
GaussianBlur blur = new GaussianBlur(10);
ImageView blurredImageView = applyFilter(originalImage, blur);
// Create a scene with the filtered image views
Scene scene = new Scene(new HBox(filteredImageView, blurredImageView));
// Set the scene and show the stage
primaryStage.setScene(scene);
primaryStage.show();
}
private ImageView applyFilter(Image image, Effect filter) {
ImageView imageView = new ImageView(image);
imageView.setEffect(filter);
return imageView;
}
public static void main(String[] args) {
launch(args);
}
}
In the code above, we first create an Image
object from the original image file. We then create two different