Top Description Inners Fields Constructors Methods
javafx.scene.image

public Class Image

extends Object
Class Inheritance
Known Direct Subclasses
javafx.scene.image.WritableImage
Imports
java.io.InputStream, java.lang.ref.WeakReference, java.net.MalformedURLException, .URL, java.nio.Buffer, .ByteBuffer, .IntBuffer, java.util.LinkedList, .Queue, java.util.concurrent.CancellationException, java.util.regex.Pattern, javafx.animation.KeyFrame, .Timeline, .Interpolator, .KeyValue, javafx.application.Platform, javafx.beans.NamedArg, javafx.beans.property.ReadOnlyBooleanProperty, .ReadOnlyBooleanWrapper, .ReadOnlyDoubleProperty, .ReadOnlyDoublePropertyBase, .ReadOnlyDoubleWrapper, .ReadOnlyObjectProperty, .ReadOnlyObjectPropertyBase, .ReadOnlyObjectWrapper, .SimpleIntegerProperty, javafx.scene.paint.Color, javafx.util.Duration, com.sun.javafx.runtime.async.AsyncOperation, .AsyncOperationListener, com.sun.javafx.tk.ImageLoader, .PlatformImage, .Toolkit, com.sun.javafx.util.DataURI

The 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 the Java 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:

  1. the name of a resource that can be resolved by the context ClassLoader for this thread
  2. a file path that can be resolved by java.io.File
  3. a URL that can be resolved by 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);

Since
JavaFX 2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private class
private class
private class

Field Summary

Modifier and TypeField and Description
private Image.Animation
private PlatformImage[]
private final boolean
backgroundLoading

Indicates whether the image is being loaded in the background.

private Image.ImageTask
private ReadOnlyBooleanWrapper
error

Indicates whether an error was detected while loading an image.

private ReadOnlyObjectWrapper<Exception>
exception

The exception which caused image loading to fail.

private Image.DoublePropertyImpl
height

The image height or 0 if the image loading fails.

private final InputStream
private volatile boolean
private static final int
private static final Queue<Image.ImageTask>
private Image.ObjectPropertyImpl<PlatformImage>
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 width and height.

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 width and height.

private final String
url

The string representing the URL to use in fetching the pixel data.

private static final Pattern
private Image.DoublePropertyImpl
width

The image width or 0 if the image loading fails.

Constructor Summary

AccessConstructor and Description
public
Image(String
a resource path, file path, or URL
url
)

Constructs an Image with content loaded from the specified URL.

public
Image(String
a resource path, file path, or URL
url
,
boolean
indicates whether the image is being loaded in the background
backgroundLoading
)

Constructs an Image with content loaded from the specified URL using the specified parameters.

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 Image with content loaded from the specified URL using the specified parameters.

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 Image with content loaded from the specified URL using the specified parameters.

public
Image(InputStream
the stream from which to load the image
is
)

Constructs an Image with content loaded from the specified input stream.

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 Image with the specified parameters.

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 WritableImage.

pack-priv
Image(PixelBuffer<T extends Buffer>
the PixelBuffer used to construct this image.
pixelBuffer
)

Package private internal constructor used only by WritableImage.

private
Image(Object externalImage)

private
Image(String url, InputStream is, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean smooth, boolean backgroundLoading)

Method Summary

Modifier and TypeMethod and Description
pack-priv final ReadOnlyObjectProperty<PlatformImage>
public void
cancel()

Cancels the background loading of this image.

private static String
constructDetailedExceptionMessage(final String mainMessage, final Throwable cause)

private void
pack-priv void
public final ReadOnlyBooleanProperty
private ReadOnlyBooleanWrapper
public final ReadOnlyObjectProperty<Exception>
private ReadOnlyObjectWrapper<Exception>
private void
private void
pack-priv static Image
public final Exception
public final double
pack-priv final InputStream
public final PixelReader

Returns:

the PixelReader for reading the pixel data of the image
getPixelReader
()

This method returns a PixelReader that provides access to read the pixels of the image, if the image is readable.

pack-priv final Object
public final double
public final double

Returns:

The requested height
getRequestedHeight
()

Gets the height of the bounding box within which the source image is resized as necessary to fit.

public final double

Returns:

The requested width
getRequestedWidth
()

Gets 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.
getUrl
()

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.DoublePropertyImpl
private void
initialize(Object externalImage)

private void
pack-priv boolean
isAnimation()

Indicates whether image is animated.

public final boolean

Returns:

true if the image is loaded in the background
isBackgroundLoading
()

Indicates 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 by width and height.
isPreserveRatio
()

Indicates whether to preserve the aspect ratio of the original image when scaling to fit the image within the bounding box provided by width and height.

public final boolean

Returns:

true if a better quality (but slower) filtering algorithm is used for scaling to fit within the bounding box provided by width and height.
isSmooth
()

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 width and height.

private static ImageLoader
loadImage(String url, double width, double height, boolean preserveRatio, boolean smooth)

private static ImageLoader
loadImage(InputStream stream, double width, double height, boolean preserveRatio, boolean smooth)

private static AsyncOperation
loadImageAsync(AsyncOperationListener<? extends ImageLoader> listener, String url, double width, double height, boolean preserveRatio, boolean smooth)

private void
private static ImageLoader
loadPlatformImage(Object platformImage)

pack-priv void
pack-priv boolean
private Image.ObjectPropertyImpl<PlatformImage>
public final ReadOnlyDoubleProperty
private ReadOnlyDoubleWrapper
private void
setError(boolean value)

private void
pack-priv void
setPlatformImage(PlatformImage newPlatformImage)

private void
setPlatformImageWH(final PlatformImage newPlatformImage, final double newWidth, final double newHeight)

pack-priv final void
setProgress(double
should be 0-1.
value
)

This is package private *only* for the sake of testing.

private void
storePlatformImageWH(final PlatformImage platformImage, final double width, final double height)

private static InputStream
validateInputStream(final InputStream inputStream)

private static String
validateUrl(final String url)

public final ReadOnlyDoubleProperty
private Image.DoublePropertyImpl
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait