JavaFX 8 HiDPI Support

tryzor picture tryzor · Oct 3, 2014 · Viewed 14.8k times · Source

I just tried out the JavaFX Hello World Example on a 4k screen on Arch Linux, but unfortunately the GUI does not scale.

The documentation says

Hi-DPI support. JavaFX 8 now supports Hi-DPI displays.

So how can I make my application dpi aware?

Answer

jewelsea picture jewelsea · Oct 3, 2014

Hi-DPI support on various devices

For OS X Macs with retina display it should "just work" - JavaFX is aware of Hi-DPI Macs and will scale the UI appropriately. If you set the spacing in a VBox to 8, then that is a device independent unit; on a non-retina display mac it will take up 8 pixels, on a retina display which has double the resolution, the spacing will take up 16 pixels. Because the retina display also has twice the DPI as well as twice the resolution of the non-retina display, the physical screen measurement of the space will be the same regardless of device.

For Windows and Linux devices, your results may be less satisfactory as JavaFX 8u20 does not currently by default work out arbitrary DPI resolutions on such devices and scale to them appropriately. What you could do is perform most of your measurements in css as em units (which are based on the point size of the scene's root's default font) and similarly for fxml, and then set the point size of the scene root's default font appropriately depending on what you determine from querying the screen's DPI resolution. See the discussion in this answer for further information and sample code: javafx automatic resizing and button padding.

Specific to Gnome

Gnome 3 has a setting for the scaling factor which can be controlled by this command:

gsettings set org.gnome.desktop.interface scaling-factor 2

You can query this scaling factor by reading the user's gnome profile settings and use this in conjunction with querying the screen DPI to work out how an appropriate scaling factor then apply the scaling using the techniques described above.

Just a personal anecdote - when I tried using Gnome 3 scaling (CentOS 7 and also a recent Fedora release) on a Hi-DPI display a couple of days back, I found the overall support for Hi-DPI across applications running under Linux to be pretty spotty. Certainly, the support was much improved from CentOS 6 when I attempted that, but there was still quite a way to go to achieve quality Hi-DPI support across windowing toolkit, standard apps and third party apps. For this reason, I believe that running HiDPI Gnome desktops is still quite a bleeding edge thing which is definitely not for everyone - I am sure that this situation will change over time.

Bitmapped Images

From a JavaFX team lead blog entry on Hi-DPI:

In Apple’s applications (starting with the iPhone and iPad with their retina displays), the solution to the problem is for the application developer to supply two images instead of one for each image asset. For example, the splash screen will be supplied with two images, one at normal resolution and one at 2x the resolution. The files are named the same but the 2x one is named according to some convention, such that at runtime the platform will lookup the 2x version on retina behind the scenes. In such a way, your application says “fooImage.png” but “[email protected]” is looked up instead when on a machine with a retina display.

I do not know whether this bitmapped image choosing functionality for Hi-DPI displays is currently in Java 8u20 or not - you might have to implement it yourself by querying the screen with screen.getDpi(), then loading the appropriate bitmap.

4K Devices

4K is a lot of pixels to push. JavaFX will by default use hardware a accelerated graphics pipeline when such a graphics pipeline is available. Some graphics hardware may not be fully optimized for 4K display (e.g. not enough video ram), which might lead to an application which either does not work or performs poorly. I also don't believe that currently a lot of effort has gone into investigating JavaFX performance on various 4K devices - it might "just work", but it might not either. You will need to test your application on the target hardware to determine the current capabilities of JavaFX applications when running on that hardware. You might also need to tweak the application according to some of the suggestions above.

A user has reported an issue with JavaFX 8u20 when attempting to display a 4K video using JavaFX:

Background

Supporting Hi-DPI under OS X was (likely) simpler than Windows/Linux devices as the target devices are either retina or non-retina display with one being an exact 2x scale of the other and direct support from the OS X system can be leveraged to help achieve the retina scaling. With Windows/Linux, probably what is required is an ability to scale at factors other than just 2x, and that is covered by the (currently outstanding and scheduled) feature request RT-32521 Support global coordinate scaling with DPI-based default. Scaling by an integral amount usually gives the best visible results.

Additional Resources

A complete guide to coding for Hi-DPI devices is outside the scope of this particular answer - you can google various web resources to get more information.

If you have further questions on Hi-DPI support for JavaFX, I suggest you ask them on the openjfx-dev JavaFX developer mailing list.

Wiki Answer

This answer may have some possible inconsistencies or errors and may date over time. I have made the answer community wiki. If you are aware of specific corrections, device and OS limitations or support model support for Hi-DPI on JavaFX, please feel free to edit this answer or move it to the OpenJFX wiki (where it probably belongs anyway).