@DefaultProperty(value="image") 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 HelloMenu 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:
Modifier and Type | Class and Description |
---|---|
private static class |
ImageView.StyleableProperties
Super-lazy instantiation pattern from Bill Pugh.
|
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 ObjectProperty<Image> |
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 ObjectProperty<Rectangle2D> |
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. |
BASELINE_OFFSET_SAME_AS_HEIGHT
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 |
---|---|
DoubleProperty |
fitHeightProperty() |
DoubleProperty |
fitWidthProperty() |
static java.util.List<CssMetaData<? extends Styleable,?>> |
getClassCssMetaData() |
java.util.List<CssMetaData<? extends Styleable,?>> |
getCssMetaData()
This method should delegate to
Node.getClassCssMetaData() so that
a Node's CssMetaData can be accessed without the need for reflection. |
double |
getFitHeight() |
double |
getFitWidth() |
Image |
getImage() |
Rectangle2D |
getViewport() |
double |
getX() |
double |
getY() |
ObjectProperty<Image> |
imageProperty() |
private StringProperty |
imageUrlProperty()
The imageUrl property is set from CSS and then the image property is
set from the invalidated method.
|
protected boolean |
impl_computeContains(double localX,
double localY)
Deprecated.
This is an internal API that is not intended for use and will be removed in the next version
|
BaseBounds |
impl_computeGeomBounds(BaseBounds bounds,
BaseTransform tx)
Deprecated.
This is an internal API that is not intended for use and will be removed in the next version
|
protected NGNode |
impl_createPeer()
Deprecated.
This is an internal API that is not intended for use and will be removed in the next version
|
java.lang.Object |
impl_processMXNode(MXNodeAlgorithm alg,
MXNodeAlgorithmContext ctx)
Deprecated.
This is an internal API that is not intended for use and will be removed in the next version
|
void |
impl_updatePeer()
Deprecated.
This is an internal API that is not intended for use and will be removed in the next version
|
private void |
invalidateWidthHeight() |
boolean |
isPreserveRatio() |
boolean |
isSmooth() |
BooleanProperty |
preserveRatioProperty() |
private void |
recomputeWidthHeight() |
void |
setFitHeight(double value) |
void |
setFitWidth(double value) |
void |
setImage(Image value) |
void |
setPreserveRatio(boolean value) |
void |
setSmooth(boolean value) |
void |
setViewport(Rectangle2D value) |
void |
setX(double value) |
void |
setY(double value) |
BooleanProperty |
smoothProperty() |
(package private) void |
updateViewport() |
ObjectProperty<Rectangle2D> |
viewportProperty() |
DoubleProperty |
xProperty() |
DoubleProperty |
yProperty() |
accessibleHelpProperty, accessibleRoleDescriptionProperty, accessibleRoleProperty, accessibleTextProperty, addEventFilter, addEventHandler, applyCss, autosize, blendModeProperty, boundsInLocalProperty, boundsInParentProperty, buildEventDispatchChain, cacheHintProperty, cacheProperty, clipProperty, computeAreaInScreen, contains, contains, containsBounds, cursorProperty, depthTestProperty, disabledProperty, disableProperty, effectiveNodeOrientationProperty, effectProperty, eventDispatcherProperty, executeAccessibleAction, fireEvent, focusedProperty, focusTraversableProperty, getAccessibleHelp, getAccessibleRole, getAccessibleRoleDescription, getAccessibleText, getBaselineOffset, getBlendMode, getBoundsInLocal, getBoundsInParent, getCacheHint, getClip, getContentBias, getCursor, getDepthTest, getEffect, getEffectiveNodeOrientation, getEventDispatcher, getId, getInputMethodRequests, getLayoutBounds, getLayoutX, getLayoutY, getLocalToParentTransform, getLocalToSceneTransform, getNodeOrientation, getOnContextMenuRequested, getOnDragDetected, getOnDragDone, getOnDragDropped, getOnDragEntered, getOnDragExited, getOnDragOver, getOnInputMethodTextChanged, getOnKeyPressed, getOnKeyReleased, getOnKeyTyped, getOnMouseClicked, getOnMouseDragEntered, getOnMouseDragExited, getOnMouseDragged, getOnMouseDragOver, getOnMouseDragReleased, getOnMouseEntered, getOnMouseExited, getOnMouseMoved, getOnMousePressed, getOnMouseReleased, getOnRotate, getOnRotationFinished, getOnRotationStarted, getOnScroll, getOnScrollFinished, getOnScrollStarted, getOnSwipeDown, getOnSwipeLeft, getOnSwipeRight, getOnSwipeUp, getOnTouchMoved, getOnTouchPressed, getOnTouchReleased, getOnTouchStationary, getOnZoom, getOnZoomFinished, getOnZoomStarted, getOpacity, getParent, getProperties, getPseudoClassStates, getRotate, getRotationAxis, getScaleX, getScaleY, getScaleZ, getScene, getStyle, getStyleableParent, getStyleClass, getTransforms, getTranslateX, getTranslateY, getTranslateZ, getTypeSelector, getUserData, hasProperties, hoverProperty, idProperty, impl_clearDirty, impl_computeIntersects, impl_computeLayoutBounds, impl_cssGetCursorInitialValue, impl_cssGetFocusTraversableInitialValue, impl_findStyles, impl_geomChanged, impl_getLeafTransform, impl_getMatchingStyles, impl_getPeer, impl_getPivotX, impl_getPivotY, impl_getPivotZ, impl_getStyleMap, impl_hasTransforms, impl_intersects, impl_intersectsBounds, impl_isDirty, impl_isDirtyEmpty, impl_isShowMnemonics, impl_isTreeVisible, impl_layoutBoundsChanged, impl_markDirty, impl_notifyLayoutBoundsChanged, impl_pickNode, impl_pickNodeLocal, impl_processCSS, impl_processCSS, impl_reapplyCSS, impl_setShowMnemonics, impl_setStyleMap, impl_showMnemonicsProperty, impl_syncPeer, impl_transformsChanged, impl_traverse, impl_treeVisibleProperty, inputMethodRequestsProperty, intersects, intersects, isCache, isDisable, isDisabled, isFocused, isFocusTraversable, isHover, isManaged, isMouseTransparent, isPickOnBounds, isPressed, isResizable, isVisible, layoutBoundsProperty, layoutXProperty, layoutYProperty, localToParent, localToParent, localToParent, localToParent, localToParent, localToParentTransformProperty, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToScene, localToSceneTransformProperty, localToScreen, localToScreen, localToScreen, localToScreen, localToScreen, lookup, lookupAll, managedProperty, maxHeight, maxWidth, minHeight, minWidth, mouseTransparentProperty, nodeOrientationProperty, notifyAccessibleAttributeChanged, onContextMenuRequestedProperty, onDragDetectedProperty, onDragDoneProperty, onDragDroppedProperty, onDragEnteredProperty, onDragExitedProperty, onDragOverProperty, onInputMethodTextChangedProperty, onKeyPressedProperty, onKeyReleasedProperty, onKeyTypedProperty, onMouseClickedProperty, onMouseDragEnteredProperty, onMouseDragExitedProperty, onMouseDraggedProperty, onMouseDragOverProperty, onMouseDragReleasedProperty, onMouseEnteredProperty, onMouseExitedProperty, onMouseMovedProperty, onMousePressedProperty, onMouseReleasedProperty, onRotateProperty, onRotationFinishedProperty, onRotationStartedProperty, onScrollFinishedProperty, onScrollProperty, onScrollStartedProperty, onSwipeDownProperty, onSwipeLeftProperty, onSwipeRightProperty, onSwipeUpProperty, onTouchMovedProperty, onTouchPressedProperty, onTouchReleasedProperty, onTouchStationaryProperty, onZoomFinishedProperty, onZoomProperty, onZoomStartedProperty, opacityProperty, parentProperty, parentToLocal, parentToLocal, parentToLocal, parentToLocal, parentToLocal, pickOnBoundsProperty, prefHeight, prefWidth, pressedProperty, pseudoClassStateChanged, queryAccessibleAttribute, relocate, removeEventFilter, removeEventHandler, requestFocus, resize, resizeRelocate, rotateProperty, rotationAxisProperty, scaleXProperty, scaleYProperty, scaleZProperty, sceneProperty, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, sceneToLocal, screenToLocal, screenToLocal, screenToLocal, setAccessibleHelp, setAccessibleRole, setAccessibleRoleDescription, setAccessibleText, setBlendMode, setCache, setCacheHint, setClip, setCursor, setDepthTest, setDisable, setDisabled, setEffect, setEventDispatcher, setEventHandler, setFocused, setFocusTraversable, setHover, setId, setInputMethodRequests, setLayoutX, setLayoutY, setManaged, setMouseTransparent, setNodeOrientation, setOnContextMenuRequested, setOnDragDetected, setOnDragDone, setOnDragDropped, setOnDragEntered, setOnDragExited, setOnDragOver, setOnInputMethodTextChanged, setOnKeyPressed, setOnKeyReleased, setOnKeyTyped, setOnMouseClicked, setOnMouseDragEntered, setOnMouseDragExited, setOnMouseDragged, setOnMouseDragOver, setOnMouseDragReleased, setOnMouseEntered, setOnMouseExited, setOnMouseMoved, setOnMousePressed, setOnMouseReleased, setOnRotate, setOnRotationFinished, setOnRotationStarted, setOnScroll, setOnScrollFinished, setOnScrollStarted, setOnSwipeDown, setOnSwipeLeft, setOnSwipeRight, setOnSwipeUp, setOnTouchMoved, setOnTouchPressed, setOnTouchReleased, setOnTouchStationary, setOnZoom, setOnZoomFinished, setOnZoomStarted, setOpacity, setPickOnBounds, setPressed, setRotate, setRotationAxis, setScaleX, setScaleY, setScaleZ, setStyle, setTranslateX, setTranslateY, setTranslateZ, setUserData, setVisible, snapshot, snapshot, startDragAndDrop, startFullDrag, styleProperty, toBack, toFront, toString, translateXProperty, translateYProperty, translateZProperty, usesMirroring, visibleProperty
private ObjectProperty<Image> 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
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 ObjectProperty<Rectangle2D> 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 ObjectProperty<Image> imageProperty()
private StringProperty imageUrlProperty()
public final void setX(double value)
public final double getX()
public final DoubleProperty xProperty()
public final void setY(double value)
public final double getY()
public final DoubleProperty yProperty()
public final void setFitWidth(double value)
public final double getFitWidth()
public final DoubleProperty fitWidthProperty()
public final void setFitHeight(double value)
public final double getFitHeight()
public final DoubleProperty fitHeightProperty()
public final void setPreserveRatio(boolean value)
public final boolean isPreserveRatio()
public final BooleanProperty preserveRatioProperty()
public final void setSmooth(boolean value)
public final boolean isSmooth()
public final BooleanProperty smoothProperty()
public final void setViewport(Rectangle2D value)
public final Rectangle2D getViewport()
public final ObjectProperty<Rectangle2D> viewportProperty()
@Deprecated protected NGNode impl_createPeer()
impl_createPeer
in class Node
@Deprecated public BaseBounds impl_computeGeomBounds(BaseBounds bounds, BaseTransform tx)
Node
impl_computeGeomBounds
in class Node
private void invalidateWidthHeight()
private void recomputeWidthHeight()
@Deprecated protected boolean impl_computeContains(double localX, double localY)
impl_computeContains
in class Node
public static java.util.List<CssMetaData<? extends Styleable,?>> getClassCssMetaData()
public java.util.List<CssMetaData<? extends Styleable,?>> getCssMetaData()
Node.getClassCssMetaData()
so that
a Node's CssMetaData can be accessed without the need for reflection.getCssMetaData
in interface Styleable
getCssMetaData
in class Node
void updateViewport()
@Deprecated public void impl_updatePeer()
Node
impl_updatePeer
in class Node
@Deprecated public java.lang.Object impl_processMXNode(MXNodeAlgorithm alg, MXNodeAlgorithmContext ctx)
Node
impl_processMXNode
in class Node
alg
- current algorithm to process this nodectx
- current context