public class ImageView
extends Node
ImageView
is a Node
used for painting images loaded with
Image
class.
This class allows resizing the displayed image (with or without preserving
the original aspect ratio) and specifying a viewport into the source image
for restricting the pixels displayed by this ImageView
.
Example code for displaying images
import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class HelloImageView extends Application {
@Override public void start(Stage stage) {
// load the image
Image image = new Image("flower.png");
// simple displays ImageView the image as is
ImageView iv1 = new ImageView();
iv1.setImage(image);
// resizes the image to have width of 100 while preserving the ratio and using
// higher quality filtering method; this ImageView is also cached to
// improve performance
ImageView iv2 = new ImageView();
iv2.setImage(image);
iv2.setFitWidth(100);
iv2.setPreserveRatio(true);
iv2.setSmooth(true);
iv2.setCache(true);
// defines a viewport into the source image (achieving a "zoom" effect) and
// displays it rotated
ImageView iv3 = new ImageView();
iv3.setImage(image);
Rectangle2D viewportRect = new Rectangle2D(40, 35, 110, 110);
iv3.setViewport(viewportRect);
iv3.setRotate(90);
Group root = new Group();
Scene scene = new Scene(root);
scene.setFill(Color.BLACK);
HBox box = new HBox();
box.getChildren().add(iv1);
box.getChildren().add(iv2);
box.getChildren().add(iv3);
root.getChildren().add(box);
stage.setTitle("ImageView");
stage.setWidth(415);
stage.setHeight(200);
stage.setScene(scene);
stage.sizeToScene();
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
The code above produces the following:
Type | Property and Description |
---|---|
DoubleProperty |
fitHeight
The height of the bounding box within which the source image is resized
as necessary to fit.
|
DoubleProperty |
fitWidth
The width of the bounding box within which the source image is resized as
necessary to fit.
|
<any> |
image
The
Image to be painted by this ImageView . |
private StringProperty |
imageUrl
The imageUrl property is set from CSS and then the image property is
set from the invalidated method.
|
BooleanProperty |
preserveRatio
Indicates whether to preserve the aspect ratio of the source image when
scaling to fit the image within the fitting bounding box.
|
BooleanProperty |
smooth
Indicates whether to use a better quality filtering algorithm or a faster
one when transforming or scaling the source image to fit within the
bounding box provided by
fitWidth and fitHeight . |
<any> |
viewport
The rectangular viewport into the image.
|
DoubleProperty |
x
The current x coordinate of the
ImageView origin. |
DoubleProperty |
y
The current y coordinate of the
ImageView origin. |
Modifier and Type | Class and Description |
---|---|
private static class |
ImageView.StyleableProperties |
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
DEFAULT_STYLE_CLASS
* Stylesheet Handling * *
|
private double |
destHeight |
private double |
destWidth |
private DoubleProperty |
fitHeight
The height of the bounding box within which the source image is resized
as necessary to fit.
|
private DoubleProperty |
fitWidth
The width of the bounding box within which the source image is resized as
necessary to fit.
|
private <any> |
image
The
Image to be painted by this ImageView . |
private StringProperty |
imageUrl |
private Image |
oldImage |
private AbstractNotifyListener |
platformImageChangeListener |
private BooleanProperty |
preserveRatio
Indicates whether to preserve the aspect ratio of the source image when
scaling to fit the image within the fitting bounding box.
|
private BooleanProperty |
smooth
Indicates whether to use a better quality filtering algorithm or a faster
one when transforming or scaling the source image to fit within the
bounding box provided by
fitWidth and fitHeight . |
static boolean |
SMOOTH_DEFAULT
Platform-dependent default value of the
smooth property. |
private boolean |
validWH |
private <any> |
viewport
The rectangular viewport into the image.
|
private DoubleProperty |
x
The current x coordinate of the
ImageView origin. |
private DoubleProperty |
y
The current y coordinate of the
ImageView origin. |
Constructor and Description |
---|
ImageView()
Allocates a new ImageView object.
|
ImageView(Image image)
Allocates a new ImageView object using the given image.
|
ImageView(java.lang.String url)
Allocates a new ImageView object with image loaded from the specified
URL.
|
Modifier and Type | Method and Description |
---|---|
private boolean |
doComputeContains(double localX,
double localY) |
private BaseBounds |
doComputeGeomBounds(BaseBounds bounds,
BaseTransform tx) |
private NGNode |
doCreatePeer() |
private void |
doUpdatePeer() |
DoubleProperty |
fitHeightProperty()
The height of the bounding box within which the source image is resized
as necessary to fit.
|
DoubleProperty |
fitWidthProperty()
The width of the bounding box within which the source image is resized as
necessary to fit.
|
static java.util.List<<any>> |
getClassCssMetaData() |
java.util.List<<any>> |
getCssMetaData() |
double |
getFitHeight()
Gets the value of the property fitHeight.
|
double |
getFitWidth()
Gets the value of the property fitWidth.
|
Image |
getImage()
Gets the value of the property image.
|
Rectangle2D |
getViewport()
Gets the value of the property viewport.
|
double |
getX()
Gets the value of the property x.
|
double |
getY()
Gets the value of the property y.
|
<any> |
imageProperty()
The
Image to be painted by this ImageView . |
private StringProperty |
imageUrlProperty()
The imageUrl property is set from CSS and then the image property is
set from the invalidated method.
|
private void |
invalidateWidthHeight() |
boolean |
isPreserveRatio()
Gets the value of the property preserveRatio.
|
boolean |
isSmooth()
Gets the value of the property smooth.
|
BooleanProperty |
preserveRatioProperty()
Indicates whether to preserve the aspect ratio of the source image when
scaling to fit the image within the fitting bounding box.
|
private void |
recomputeWidthHeight() |
void |
setFitHeight(double value)
Sets the value of the property fitHeight.
|
void |
setFitWidth(double value)
Sets the value of the property fitWidth.
|
void |
setImage(Image value)
Sets the value of the property image.
|
void |
setPreserveRatio(boolean value)
Sets the value of the property preserveRatio.
|
void |
setSmooth(boolean value)
Sets the value of the property smooth.
|
void |
setViewport(Rectangle2D value)
Sets the value of the property viewport.
|
void |
setX(double value)
Sets the value of the property x.
|
void |
setY(double value)
Sets the value of the property y.
|
BooleanProperty |
smoothProperty()
Indicates whether to use a better quality filtering algorithm or a faster
one when transforming or scaling the source image to fit within the
bounding box provided by
fitWidth and fitHeight . |
(package private) void |
updateViewport() |
<any> |
viewportProperty()
The rectangular viewport into the image.
|
DoubleProperty |
xProperty()
The current x coordinate of the
ImageView origin. |
DoubleProperty |
yProperty()
The current y coordinate of the
ImageView origin. |
public final <any> imageProperty
Image
to be painted by this ImageView
.getImage()
,
setImage(Image)
private StringProperty imageUrlProperty
public final DoubleProperty xProperty
ImageView
origin.getX()
,
setX(double)
public final DoubleProperty yProperty
ImageView
origin.getY()
,
setY(double)
public final DoubleProperty fitWidthProperty
fitWidth
.
See preserveRatio
for information on interaction between image
view's fitWidth
, fitHeight
and preserveRatio
attributes.
getFitWidth()
,
setFitWidth(double)
public final DoubleProperty fitHeightProperty
fitHeight
.
See preserveRatio
for information on interaction between image
view's fitWidth
, fitHeight
and preserveRatio
attributes.
getFitHeight()
,
setFitHeight(double)
public final BooleanProperty preserveRatioProperty
If set to true
, it affects the dimensions of this
ImageView
in the following way
fitWidth
is set, height is scaled to preserve ratio
fitHeight
is set, width is scaled to preserve ratio
false
, it affects the dimensions of this
ImageView
in the following way
fitWidth
is set, image's view width is scaled to
match and height is unchanged;
fitHeight
is set, image's view height is scaled to
match and height is unchanged;
fitWidth x fitHeight
bonding box.isPreserveRatio()
,
setPreserveRatio(boolean)
public final BooleanProperty smoothProperty
fitWidth
and fitHeight
.
If set to true
a better quality filtering will be used, if set to
false
a faster but lesser quality filtering will be used.
The default value depends on platform configuration.
isSmooth()
,
setSmooth(boolean)
public final <any> viewportProperty
If viewport
is null
, the entire image is displayed. If
viewport
is non-null
, only the portion of the image which
falls within the viewport will be displayed. If the image does not fully
cover the viewport then any remaining area of the viewport will be empty.
getViewport()
,
setViewport(Rectangle2D)
private <any> image
Image
to be painted by this ImageView
.private Image oldImage
private StringProperty imageUrl
private final AbstractNotifyListener platformImageChangeListener
private DoubleProperty x
ImageView
origin.private DoubleProperty y
ImageView
origin.private DoubleProperty fitWidth
fitWidth
.
See preserveRatio
for information on interaction between image
view's fitWidth
, fitHeight
and preserveRatio
attributes.
private DoubleProperty fitHeight
fitHeight
.
See preserveRatio
for information on interaction between image
view's fitWidth
, fitHeight
and preserveRatio
attributes.
private BooleanProperty preserveRatio
If set to true
, it affects the dimensions of this
ImageView
in the following way
fitWidth
is set, height is scaled to preserve ratio
fitHeight
is set, width is scaled to preserve ratio
false
, it affects the dimensions of this
ImageView
in the following way
fitWidth
is set, image's view width is scaled to
match and height is unchanged;
fitHeight
is set, image's view height is scaled to
match and height is unchanged;
fitWidth x fitHeight
bonding box.private BooleanProperty smooth
fitWidth
and fitHeight
.
If set to true
a better quality filtering will be used, if set to
false
a faster but lesser quality filtering will be used.
The default value depends on platform configuration.
public static final boolean SMOOTH_DEFAULT
smooth
property.private <any> viewport
If viewport
is null
, the entire image is displayed. If
viewport
is non-null
, only the portion of the image which
falls within the viewport will be displayed. If the image does not fully
cover the viewport then any remaining area of the viewport will be empty.
private double destWidth
private double destHeight
private boolean validWH
private static final java.lang.String DEFAULT_STYLE_CLASS
public ImageView()
public ImageView(java.lang.String url)
The new ImageView(url)
has the same effect as
new ImageView(new Image(url))
.
url
- the string representing the URL from which to load the imagejava.lang.NullPointerException
- if URL is nulljava.lang.IllegalArgumentException
- if URL is invalid or unsupportedpublic ImageView(Image image)
image
- Image that this ImageView usespublic final void setImage(Image value)
public final Image getImage()
public final <any> imageProperty()
Image
to be painted by this ImageView
.getImage()
,
setImage(Image)
private StringProperty imageUrlProperty()
public final void setX(double value)
public final double getX()
public final DoubleProperty xProperty()
ImageView
origin.getX()
,
setX(double)
public final void setY(double value)
public final double getY()
public final DoubleProperty yProperty()
ImageView
origin.getY()
,
setY(double)
public final void setFitWidth(double value)
public final double getFitWidth()
public final DoubleProperty fitWidthProperty()
fitWidth
.
See preserveRatio
for information on interaction between image
view's fitWidth
, fitHeight
and preserveRatio
attributes.
getFitWidth()
,
setFitWidth(double)
public final void setFitHeight(double value)
public final double getFitHeight()
public final DoubleProperty fitHeightProperty()
fitHeight
.
See preserveRatio
for information on interaction between image
view's fitWidth
, fitHeight
and preserveRatio
attributes.
getFitHeight()
,
setFitHeight(double)
public final void setPreserveRatio(boolean value)
public final boolean isPreserveRatio()
public final BooleanProperty preserveRatioProperty()
If set to true
, it affects the dimensions of this
ImageView
in the following way
fitWidth
is set, height is scaled to preserve ratio
fitHeight
is set, width is scaled to preserve ratio
false
, it affects the dimensions of this
ImageView
in the following way
fitWidth
is set, image's view width is scaled to
match and height is unchanged;
fitHeight
is set, image's view height is scaled to
match and height is unchanged;
fitWidth x fitHeight
bonding box.isPreserveRatio()
,
setPreserveRatio(boolean)
public final void setSmooth(boolean value)
public final boolean isSmooth()
public final BooleanProperty smoothProperty()
fitWidth
and fitHeight
.
If set to true
a better quality filtering will be used, if set to
false
a faster but lesser quality filtering will be used.
The default value depends on platform configuration.
isSmooth()
,
setSmooth(boolean)
public final void setViewport(Rectangle2D value)
public final Rectangle2D getViewport()
public final <any> viewportProperty()
If viewport
is null
, the entire image is displayed. If
viewport
is non-null
, only the portion of the image which
falls within the viewport will be displayed. If the image does not fully
cover the viewport then any remaining area of the viewport will be empty.
getViewport()
,
setViewport(Rectangle2D)
private NGNode doCreatePeer()
private BaseBounds doComputeGeomBounds(BaseBounds bounds, BaseTransform tx)
private void invalidateWidthHeight()
private void recomputeWidthHeight()
private boolean doComputeContains(double localX, double localY)
public static java.util.List<<any>> getClassCssMetaData()
public java.util.List<<any>> getCssMetaData()
void updateViewport()
private void doUpdatePeer()