Tino APCS

Displaying Images

This page accompanies JavaFX Lab 7.3 Minesweeper GUI Template

How to display an image (option one)

Displaying an image in JavaFX takes two steps:

  1. Create an Image object. See the Image API for JavaFX
    Note 1: You should create an Image only ONCE for each type of image needed in the game. Don't keep loading the same Image over an over. Keep variables for each image.
    Note 2: To load an Image file, the syntax is new Image("file:folder/imagename.extension")
    Note 3: Eclipse always looks in your project's root folder when you don't specify a full path name. For example, saying "new Image("file:happy.jpg") would expect happy.jpg to exist in your project's root folder. And saying new Image("file:images/happy.jpg") would expect happy.jpg to exist in a subfolder named "images" within your project's root folder. Using subfolders is best for organization.

  2. Create an ImageView object, which is a Node that displays an Image.

Fun fact: Displaying images JavaFX uses the MVC pattern. In JavaFX, an Image is the raw data (Model) and an ImageView is the presentation of that data (View). The controller would be any event handlers you assign the the ImageView.

Display Images (Option 2)

If you want to be able to make your minesweeper app into an executable jar file, you will have to access your images differently. Follow these steps to access resources in a way that will work even if your code is running from inside a jar file.

Sprite sheet

You can either draw your own images, copy them from an existing game, or download this Minesweeper Sprite Sheet. These are all 32x32 tiles and you should keep them in a subfolder inside your Eclipse project's root folder.

Dark Mode

Outline