The Image and ImageView Classes

The Image class represents a graphical image and the ImageView class can be used to display an image. The javafx.scene.image.Image class represents a graphical image and is used for loading an image from a specified filename or a URL. For example, new …


This content originally appeared on DEV Community and was authored by Paul Ngugi

The Image class represents a graphical image and the ImageView class can be used to display an image. The javafx.scene.image.Image class represents a graphical image and is used for loading an image from a specified filename or a URL. For example, new Image("image/us.gif") creates an Image object for the image file us.gif under the directory image in the Java class directory and new Image("http://www.cs.armstrong.edu/liang/image/us.gif") creates an Image object for the image file in the URL on the Web.

The javafx.scene.image.ImageView is a node for displaying an image. An ImageView can be created from an Image object. For example, the following code creates an ImageView from an image file:

Image image = new Image("image/us.gif");
ImageView imageView = new ImageView(image);

Alternatively, you can create an ImageView directly from a file or a URL as follows:

ImageView imageView = new ImageView("image/us.gif");

The UML diagrams for the Image and ImageView classes are illustrated in Figures below.

Image description

Image description

Image description

Image description

The program creates an HBox (line 16). An HBox is a pane that places all nodes horizontally in one row. The program creates an Image, and then an ImageView for displaying the image, and places the ImageView in the HBox (line 19).

The program creates the second ImageView (line 21), sets its fitHeight and fitWidth properties (lines 22–23) and places the ImageView into the HBox (line 24). The program creates the third ImageView (line 26), rotates it 90 degrees (line 27), and places it into the HBox (line 28). The setRotate method is defined in the Node class and can be used for any node. Note that an Image object can be shared by multiple nodes. In this case, it is shared by three ImageView. However, a node such as ImageView cannot be shared. You cannot place an ImageView multiple times into a pane or scene.

Note that you must place the image file in the same directory as the class file, as shown in the following figure.

Image description

If you use the URL to locate the image file, the URL protocal http:// must be present. So the following code is wrong.

new Image("www.cs.armstrong.edu/liang/image/us.gif");

It must be replaced by

new Image("http://www.cs.armstrong.edu/liang/image/us.gif");


This content originally appeared on DEV Community and was authored by Paul Ngugi


Print Share Comment Cite Upload Translate Updates
APA

Paul Ngugi | Sciencx (2024-06-20T16:46:52+00:00) The Image and ImageView Classes. Retrieved from https://www.scien.cx/2024/06/20/the-image-and-imageview-classes/

MLA
" » The Image and ImageView Classes." Paul Ngugi | Sciencx - Thursday June 20, 2024, https://www.scien.cx/2024/06/20/the-image-and-imageview-classes/
HARVARD
Paul Ngugi | Sciencx Thursday June 20, 2024 » The Image and ImageView Classes., viewed ,<https://www.scien.cx/2024/06/20/the-image-and-imageview-classes/>
VANCOUVER
Paul Ngugi | Sciencx - » The Image and ImageView Classes. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/20/the-image-and-imageview-classes/
CHICAGO
" » The Image and ImageView Classes." Paul Ngugi | Sciencx - Accessed . https://www.scien.cx/2024/06/20/the-image-and-imageview-classes/
IEEE
" » The Image and ImageView Classes." Paul Ngugi | Sciencx [Online]. Available: https://www.scien.cx/2024/06/20/the-image-and-imageview-classes/. [Accessed: ]
rf:citation
» The Image and ImageView Classes | Paul Ngugi | Sciencx | https://www.scien.cx/2024/06/20/the-image-and-imageview-classes/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.