Image class represents graphical images and is used for loading
images from a specified URL.
JavaFX has built-in support for the following image formats:
For all other formats, JavaFX uses theJava Image I/O API on supported platforms.
Image I/O is extensible so that developers can "plug-in" support for additional formats.
Images can be resized as they are loaded (for example to reduce the amount of memory consumed by the image). The application can specify the quality of filtering used when scaling, and whether to preserve the original image's aspect ratio.
If a URL string is passed to a constructor, it can be any of the following:
ClassLoader for this thread
java.io.File
java.net.URL and for
which a protocol handler exists
The RFC 2397 "data" scheme for URLs is supported in addition to
the protocol handlers that are registered for the application.
If a URL uses the "data" scheme, the data must be base64-encoded
and the MIME type must be a subtype of the image type.
The MIME type must match the image format of the data contained in
the URL. In case of a mismatch between MIME type and image format,
the image will be loaded if the image format can be determined by
JavaFX, and a warning will be logged.
Use ImageView for displaying images loaded with this
class. The same Image instance can be displayed by multiple
ImageViews.
Example code for loading images:
import javafx.scene.image.Image;
// load an image in background, displaying a placeholder while it's loading
// (assuming there's an ImageView node somewhere displaying this image)
// The image is located in default package of the classpath
Image image1 = new Image("/flower.png", true);
// load an image and resize it to 100x150 without preserving its original
// aspect ratio
// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png", 100, 150, false, false);
// load an image and resize it to width of 100 while preserving its
// original aspect ratio, using faster filtering method
// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png", 100, 0, false, false);
// load an image and resize it only in one dimension, to the height of 100 and
// the original width, without preserving original aspect ratio
// The image is located in the current working directory
Image image4 = new Image("file:flower.png", 0, 100, false, false);
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private class | |
| private class | |
| private class |
| Modifier and Type | Field and Description |
|---|---|
| private Image. | |
| private PlatformImage[] | |
| private final boolean | backgroundLoading
Indicates whether the image is being loaded in the background. |
| private Image. | |
| private ReadOnlyBooleanWrapper | error
Indicates whether an error was detected while loading an image. |
| private ReadOnlyObjectWrapper | exception
The exception which caused image loading to fail. |
| private Image. | height
The image height or |
| private final InputStream | |
| private volatile boolean | |
| private static final int | |
| private static final Queue | |
| private Image. | |
| private final boolean | preserveRatio
Indicates whether to preserve the aspect ratio of the original image
when scaling to fit the image within the bounding box provided by
|
| private ReadOnlyDoubleWrapper | progress
The approximate percentage of image's loading that has been completed. |
| private PixelReader | |
| private final double | requestedHeight
The height of the bounding box within which the source image is resized as necessary to fit. |
| private final double | requestedWidth
The width of the bounding box within which the source image is resized as necessary to fit. |
| private static int | |
| private final boolean | smooth
Indicates whether to use a better quality filtering algorithm or a faster
one when scaling this image to fit within the
bounding box provided by |
| private final String | url
The string representing the URL to use in fetching the pixel data. |
| private static final Pattern | |
| private Image. | width
The image width or |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public | Image(String
a resource path, file path, or URL url, double the image's bounding box width requestedWidth, double the image's bounding box height requestedHeight, boolean indicates whether to preserve the aspect ratio of
the original image when scaling to fit the image within the
specified bounding box preserveRatio, boolean indicates whether to use a better quality filtering
algorithm or a faster one when scaling this image to fit within
the specified bounding box smooth)Constructs an |
| public | Image(String
a resource path, file path, or URL url, double the image's bounding box width requestedWidth, double the image's bounding box height requestedHeight, boolean indicates whether to preserve the aspect ratio of
the original image when scaling to fit the image within the
specified bounding box preserveRatio, boolean indicates whether to use a better quality filtering
algorithm or a faster one when scaling this image to fit within
the specified bounding box smooth, boolean indicates whether the image
is being loaded in the background backgroundLoading)Constructs an |
| public | Image(InputStream
the stream from which to load the image is)Constructs an |
| public | Image(InputStream
the stream from which to load the image is, double the image's bounding box width requestedWidth, double the image's bounding box height requestedHeight, boolean indicates whether to preserve the aspect ratio of
the original image when scaling to fit the image within the
specified bounding box preserveRatio, boolean indicates whether to use a better quality filtering
algorithm or a faster one when scaling this image to fit within
the specified bounding box smooth)Constructs a new |
| pack-priv | Image(int
the width of the empty image width, int the height of the empty image height)Package private internal constructor used only by |
| pack-priv | Image(PixelBuffer<T extends Buffer>
the pixelBuffer)PixelBuffer used to construct this image.Package private internal constructor used only by |
| private | |
| private | Image(String url, InputStream is, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean smooth, boolean backgroundLoading)
|
| Modifier and Type | Method and Description |
|---|---|
| pack-priv final ReadOnlyObjectProperty | |
| public void | |
| private static String | |
| private void | |
| pack-priv void | |
| public final ReadOnlyBooleanProperty | |
| private ReadOnlyBooleanWrapper | |
| public final ReadOnlyObjectProperty | |
| private ReadOnlyObjectWrapper | |
| private void | |
| private void | |
| pack-priv static Image | |
| public final Exception | |
| public final double | |
| pack-priv final InputStream | |
| public final PixelReader | Returns: thePixelReader for reading the pixel data of the imageThis method returns a |
| pack-priv final Object | |
| public final double | |
| public final double | Returns: The requested heightGets the height of the bounding box within which the source image is resized as necessary to fit. |
| public final double | Returns: The requested widthGets the width of the bounding box within which the source image is resized as necessary to fit. |
| public final String | Returns: a String containing the URL used to fetch the pixel data for this Image instance.Returns the url used to fetch the pixel data contained in the Image instance, if specified in the constructor. |
| public final double | |
| pack-priv PlatformImage | |
| public final ReadOnlyDoubleProperty | |
| private Image. | |
| private void | |
| private void | |
| pack-priv boolean | |
| public final boolean | Returns: true if the image is loaded in the backgroundIndicates whether the image is being loaded in the background. |
| public final boolean | |
| public final boolean | Returns: true if the aspect ratio of the original image is to be preserved when scaling to fit the image within the bounding box provided bywidth and height.Indicates whether to preserve the aspect ratio of the original image
when scaling to fit the image within the bounding box provided by
|
| public final boolean | Returns: true if a better quality (but slower) filtering algorithm is used for scaling to fit within the bounding box provided bywidth and height.Indicates whether to use a better quality filtering algorithm or a faster
one when scaling this image to fit within the
bounding box provided by |
| private static ImageLoader | |
| private static ImageLoader | |
| private static AsyncOperation | loadImageAsync(AsyncOperationListener<? extends ImageLoader> listener, String url, double width, double height, boolean preserveRatio, boolean smooth)
|
| private void | |
| private static ImageLoader | |
| pack-priv void | |
| pack-priv boolean | |
| private Image. | |
| public final ReadOnlyDoubleProperty | |
| private ReadOnlyDoubleWrapper | |
| private void | |
| private void | |
| pack-priv void | |
| private void | setPlatformImageWH(final PlatformImage newPlatformImage, final double newWidth, final double newHeight)
|
| pack-priv final void | |
| private void | |
| private static InputStream | |
| private static String | |
| public final ReadOnlyDoubleProperty | |
| private Image. |