Class Image
- All Implemented Interfaces:
HasHeightProperty
,HasWidthProperty
- Direct Known Subclasses:
WritableImage
Image
class represents graphical images and is used for loading
images from a specified URL.
Supported image formats are:
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 or not to preserve the original image's aspect ratio.
All URLs supported by
invalid @link
URL
Use ImageView
for displaying images loaded with this
class. The same Image
instance can be displayed by multiple
ImageView
s.
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
-
Property Summary
TypePropertyDescriptionfinal DoubleProperty
The approximate percentage of image's loading that has been completed. -
Constructor Summary
ConstructorDescriptionImage
(String url, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean smooth) Image
(String url, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean smooth, boolean backgroundLoading) Construct a newImage
with the specified parameters. -
Method Summary
Modifier and TypeMethodDescriptionfinal double
Gets the value of theprogress
property.final double
Gets the height of the bounding box within which the source image is resized as necessary to fit.final double
Gets the width of the bounding box within which the source image is resized as necessary to fit.getUrl()
final boolean
Indicates whether the image is being loaded in the background.boolean
final boolean
Indicates whether to preserve the aspect ratio of the original image when scaling to fit the image within the bounding box provided bywidth
andheight
.final boolean
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 bywidth
andheight
.final DoubleProperty
The approximate percentage of image's loading that has been completed.void
setPeerCanvas
(Object peerCanvas) void
setPeerCanvasDirty
(boolean peerCanvasDirty) void
setPeerImage
(Object peerImage) void
setPeerImageData
(Object peerImageData) void
setPeerUrl
(String peerUrl) void
setPixelReader
(PixelReader pixelReader) void
setPixelReaderFactory
(Supplier<PixelReader> pixelReaderFactory) final void
setProgress
(double value) This is package private *only* for the sake of testing.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface dev.webfx.kit.mapper.peers.javafxgraphics.markers.HasHeightProperty
getHeight, setHeight
Methods inherited from interface dev.webfx.kit.mapper.peers.javafxgraphics.markers.HasWidthProperty
getWidth, setWidth
-
Property Details
-
width
- Specified by:
widthProperty
in interfaceHasWidthProperty
- Returns:
- the
width
property - See Also:
-
height
- Specified by:
heightProperty
in interfaceHasHeightProperty
- Returns:
- the
height
property - See Also:
-
progress
The approximate percentage of image's loading that has been completed. A positive value between 0 and 1 where 0 is 0% and 1 is 100%.- Default value:
- 0
- See Also:
-
-
Constructor Details
-
Image
-
Image
-
Image
public Image(String url, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean smooth) -
Image
public Image(String url, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean smooth, boolean backgroundLoading) Construct a newImage
with the specified parameters.- Parameters:
url
- the string representing the URL to use in fetching the pixel datarequestedWidth
- the image's bounding box widthrequestedHeight
- the image's bounding box heightpreserveRatio
- indicates whether to preserve the aspect ratio of the original image when scaling to fit the image within the specified bounding boxsmooth
- indicates whether to use a better quality filtering algorithm or a faster one when scaling this image to fit within the specified bounding box- Throws:
NullPointerException
- if URL is nullIllegalArgumentException
- if URL is invalid or unsupported
-
-
Method Details
-
getUrl
-
getRequestedWidth
public final double getRequestedWidth()Gets the width of the bounding box within which the source image is resized as necessary to fit. If set to a value<= 0
, then the intrinsic width of the image will be used. SeepreserveRatio
for information on interaction between image'srequestedWidth
,requestedHeight
andpreserveRatio
attributes.- Returns:
- The requested width
-
getRequestedHeight
public final double getRequestedHeight()Gets the height of the bounding box within which the source image is resized as necessary to fit. If set to a value<= 0
, then the intrinsic height of the image will be used. SeepreserveRatio
for information on interaction between image'srequestedWidth
,requestedHeight
andpreserveRatio
attributes.- Returns:
- The requested height
-
isPreserveRatio
public final boolean isPreserveRatio()Indicates whether to preserve the aspect ratio of the original image when scaling to fit the image within the bounding box provided bywidth
andheight
. If set totrue
, it affects the dimensions of thisImage
in the following way:- If only
width
is set, height is scaled to preserve ratio - If only
height
is set, width is scaled to preserve ratio - If both are set, they both may be scaled to get the best fit in a width by height rectangle while preserving the original aspect ratio
width
andheight
may be different from the initially set values if they needed to be adjusted to preserve aspect ratio. If unset or set tofalse
, it affects the dimensions of thisImageView
in the following way:- If only
width
is set, the image's width is scaled to match and height is unchanged; - If only
height
is set, the image's height is scaled to match and height is unchanged; - If both are set, the image is scaled to match both.
- 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
andheight
.
- If only
-
isSmooth
public final boolean 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 bywidth
andheight
.If not initialized or set to
true
a better quality filtering will be used, otherwise a faster but lesser quality filtering will be used.- Returns:
- true if a better quality (but slower) filtering algorithm
is used for scaling to fit within the
bounding box provided by
width
andheight
.
-
isBackgroundLoading
public final boolean isBackgroundLoading()Indicates whether the image is being loaded in the background.- Returns:
- true if the image is loaded in the background
-
widthProperty
- Specified by:
widthProperty
in interfaceHasWidthProperty
- Returns:
- the
width
property - See Also:
-
heightProperty
- Specified by:
heightProperty
in interfaceHasHeightProperty
- Returns:
- the
height
property - See Also:
-
setProgress
public final void setProgress(double value) This is package private *only* for the sake of testing. We need a way to feed fake progress values. It would be better if Image were refactored to be testable (for example, by allowing the test code to provide its own implementation of background loading), but this is a simpler and safer change for now.- Parameters:
value
- should be 0-1.
-
getProgress
public final double getProgress()Gets the value of theprogress
property.- Property description:
- The approximate percentage of image's loading that has been completed. A positive value between 0 and 1 where 0 is 0% and 1 is 100%.
- Default value:
- 0
- Returns:
- the value of the
progress
property - See Also:
-
progressProperty
The approximate percentage of image's loading that has been completed. A positive value between 0 and 1 where 0 is 0% and 1 is 100%.- Default value:
- 0
- Returns:
- the
progress
property - See Also:
-
getPeerImage
-
setPeerImage
-
getPeerImageData
-
getPeerUrl
-
setPeerUrl
-
setPeerImageData
-
getPeerCanvas
-
setPeerCanvas
-
isPeerCanvasDirty
public boolean isPeerCanvasDirty() -
setPeerCanvasDirty
public void setPeerCanvasDirty(boolean peerCanvasDirty) -
getPixelReader
-
setPixelReader
-
setPixelReaderFactory
-