JavaFX 8 HiDPI Support - java

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?

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 “fooImage#2x.png” 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:
JavaFX Ultra HD (4K) video
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
Kynosarges discussion of JavaFX DPI Scaling.
The JavaFX team load wrote a blog on JavaFX on retina Macs (this is a little dated now, as JavaFX now supports retina Macs).
Randahl's perspective on JavaFX: Designing for Multiple Resolutions.
Apple have some nice advice on optimizing applications for high resolution devices, it's not JavaFX specific and some of the advice does not apply to JavaFX, but there are still some useful general principals and techniques there.
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).

I faced the same problem. I built javaFX application in my surface pro 3, which did not scale but the fxml apps did. then I installed jre8u221 withiout replacing the updated version it suggested. It fixed the problem

Related

Swing looks zoomed after upgrading java

I have a old application running a Swing client. I upgraded from Java 7 to 8 a long time ago. The upgrade went fine. Ever since I have wanted to upgrade to Java 9, then 11 and now 13. However this is where I meet problems.
The UI looks fine and as intended with Java 8, but any newer Java version will somehow change the font sizes and maybe components for some reason. I do not have any pictures to show because the application is maintained on a network without internet access because of security reasons. The client looks like most thing is multiplied with a factor of 2.0 or something. It appears as zoomed or maybe something you would create with universal design in mind. Even the splash screen is doubled in size for some reason and that is an image. There are some exeptions. The title text in the windows is the same. Also text on buttons and lables seems to be equal. Icons, images (splash) and text inside textfields/editors is much larger.
I have tried to find a solution for a long time but have not found anything. I am hoping someone else have experienced this problem or at least know what the problem could be.
Holgers comment about OS scaling was the issue. I have a 4K screen with 150% scaling set in the OS. Putting it back to 100% made the Java Swing Client look the same as when running Java 8.
To override add -Dsun.java2d.uiScale=1
Se also https://superuser.com/questions/988379/how-do-i-run-java-apps-upscaled-on-a-high-dpi-display

JavaFX Text not scaling properly on high DPI

I'm working on a Java application that involves JavaFX. When I run the program on laptops or desktops, it looks fine. But when I run the program on a MS Surface with high DPI the font sizes are not properly scaled.
I have an instance of javafx.scene.text.Text added as a child to an instance of javafx.scene.Group. The Text is very big in comparison to the rest of the visuals.
The problem is that Java reports itself as being DPI aware, so Windows doesn't scale it, when in fact it won't scale properly by itself. You can turn off this behaviour and force Windows to scale java GUI's, using this answer I found on superuser.
https://superuser.com/a/1207925
This works perfectly for me.

How to set the DPI of Java Swing apps on Windows/Linux?

If you have an monitor with a DPI over 150 (such as Macbook Pro), you may also find the problem: the font on the Java Swing app is too small for high DPI monitor, and I cannot change the font size at all ( It ignores the Windows DPI directly, only displaying the very original DPI-->96 ). I can do nothing but changing the screen resolution, which could absolutely make everything blurry on LCD.
Yes, I have a laptop with a high DPI monitor, 15.6' with 1920x1080 resolution, some Java desktop apps look very small on my laptop, such as Matlab, Burpsuite etc. I have been searching the Internet for a very very long time, but still cannot find a method for the problem. I know I can change the JRE fonts through JRE_HOME/lib/font/fontconfig.properties.src, but I cannot find any place to set the default font size or DPI for Java desktop fonts.
Does the problem have no solution? Do you have a high DPI monitor? How do you do with such apps? Does Swing give up high DPI users?
I'm currently investigating this issue on Windows. Here's what I found:
Most Swing Look & Feels don't support high DPI at all, not even Nimbus even though it's supposed to be scalable. I found some old blog posts saying that Nimbus might eventually offer high DPI scaling, but apparently that never happened.
The one exception is System LAF but its default font is ~10% smaller than the actual system font size, at all DPI settings. Moreover, System must be selected explicitly as described here:
http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
There's no single scaling factor that you could set in Swing, either. The specific LAF has to provide code to handle scaling. So the best you can do is select System and hope it's good enough.
However, JavaFX does correctly and automatically scale all the way up to 150% on my system. If at all possible, I suggest you use JavaFX to build your GUI.
edit: I made a couple small test programs and took comparison screenshots for various GUI frameworks, Swing themes, and DPI settings. This might be informative for people reading this question: http://kynosarges.org/GuiDpiScaling.html
Short answer: You need to run it on JRE 9+.
Long answer:
This is because the Java runtime declared itself to be "DPI-aware" but didn't really supported it for AWT and Swing. Java applications were sized and rendered based on pixels rather than being properly scaled, this included HiDPI displays.
Anyways, this has been recently solved.
See the issue JEP 263: HiDPI Graphics on Windows and Linux
and the upgrade.
So, increasing the font size does not work (because it does not increase the rest of the things); the jvm argument -Dsun.java2d.dpiaware=false does not work (because it is not really supported); and the manifest file + registry edit (for Windows) just does not work.
Solution: You need to run it on JRE 9+ because it really supports this feature.
This answer solved the issue on my 4K-screen Ubuntu laptop.
In the application's config file, I added the -Dsun.java2d.uiScale=2.5 Java parameter and now, upon executing, the application is scaled well and is usable.
It appears that on Linux (Gtk) you can work around this by scaling the Apps DPI using the following before starting the app.
export GDK_SCALE=2
(You can also reduce you monitor resolution but that's no solution at all).
On Linux, it doesn't seem possible to do scaling with any Swing look and feel, according to my experiments. However, it is possible to do it using a hack involving VNC, vncdesk. Here is my configuration for running gradle --gui under vncdesk:
.vncdesk/1/settings.ini:
[desktop]
width = 1050
height = 650
[window]
title = gradle
name = gradle in vncdesk
class = GradleInVncdesk
scale_factor = 2
.vncdesk/1/startup:
#!/bin/sh
cd "$INVOCATION_DIR"
matchbox-window-manager&
exec gradle --gui "$#"
I managed to solve it following these instructions: Link.
It's in German, but I will translate the important stuff.
Create this registry-key:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\SideBySide]
"PreferExternalManifest"=dword:00000001
Create a manifest file with this content:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*">
</assemblyIdentity>
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="amd64" publicKeyToken="1fc8b3b9a1e18e3b">
</assemblyIdentity>
</dependentAssembly>
</dependency>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<ms_windowsSettings:dpiAware xmlns:ms_windowsSettings="http://schemas.microsoft.com/SMI/2005/WindowsSettings">false</ms_windowsSettings:dpiAware>
</asmv3:windowsSettings>
</asmv3:application>
</assembly>
Copy it into the bin directory where the java.exe and javaw.exe is inside and named it java.exe.manifest and javaw.exe.manifest (You will have two files with the same content but different names).
HTH.
Bernd
As #demented hedgehog said setting the GDK_SCALE=2 environment variable does work under Linux.
If you are using JetBrains IntelliJ you can set Enviroment variables just for your project under Edit configurations > Build and Run > Enviroment variables
You can see below where to put the GDK_SCALE=2.

Is it possible to run Java3D applications on Nvidia 3D Vision hardware in windowed mode?

Thanks to JohnnyO it was already answered if it is possible to run Java3D application in conjunction with nVidia 3D vision:
Is it possible to run Java3D applications on Nvidia 3D Vision hardware?
His approach also worked fine for me. But it is restricted to full screen mode.
His thread is already a few months old, and now during the last few weeks nVidia improved its drivers for 3D Vision: Now it is possible to run many applications also in windowed mode. If you are using professional hardware like the nVidia Quadro cards or ATI Fire GL, you know and appreciate the windowed mode already for a very long time.
Now, with the latest updates of the nVidia drivers (I have currently installed 295.73) it is also possible to run many applications like the nVidia Picture Viewer, the Stereoscopic Player etc. in windowed mode.
http://nvidia.custhelp.com/app/answers/detail/a_id/2311
Normally DirectX 9 applications should run in windowed mode, as you can read on the previously mentioned nVidia page. And I think Java 3D is a DirectX 9 application. If I use:
-Dj3d.debug=true
Then the console says:
[j3d] Using DirectX D3D 9.0 or higher.
(And of course no one seems to be working on the improvement of the DirectX implementation of Java 3D these says.)
Did somebody meanwhile find out the trick how to run Java3D in Stereo windowed mode? I tried it, but I failed so long. I think this will be a very important argument pro Java 3D, if we solve this issue!
One thing I noticed when I played around with this was that the 3D vision drivers weren't kicking in, even when I thought I had everything set up correctly.
One hack we found was that the nVidia drivers were actually only configured to support specific video games on their whitelist, but the whitelist matching was just based on exe names.
After lots of problems and frustration, we renamed our Java wrapper from myprogram.exe to something from the whitelist, things worked much better. I think for a while, or exe was named BatmanAC.exe in testing or something like that. This seemed to make the nVidia drivers much more permissive to what we wanted to do.

Is it possible to run Java3D applications on Nvidia 3D Vision hardware?

Is is possible to run a Java3D application on Nvidia 3D Vision hardware?
I've got an existing Java3D application that can run in stereoscopic 3D. In the past, I've always run the application on Quadro cards using the OpenGL renderer and quad buffered stereo.
I now have access to a laptop with the nVidia 3D Vision system (with a GeForce GTX 460M). From the documentation, it seems like it should be possible to run my application in stereo if I use the DirectX bindings and let the nVidia drivers take care of the stereo, however, this does not seem to be the case.
If I run a Java3D application with j3d.rend=d3d, the nVidia 3D Vision API doesn't seem to recognize it as a DirectX application.
How can I get the nVidia 3D Vision drivers to detect a Java3D application and render in stereoscopic 3D?
I'm disappointed that so far everyone has dismissed this as impossible right out of the gate. I've been working on this for the past several days, and made some headway. So far, I've found that it is possible, but with a few caveats:
You need the latest drivers from nVidia (280.29 as of this writing)
You need to use the Direct3D renderer, not the OpenGL renderer (-Dj3d.rend=d3d)
You need to force Direct3D to run in fullscreen mode. (-Dj3d.fullscreen=REQUIRED)
You need to run using a ConfiguredUniverse in fullscreen mode (I'm using the provided j3d-1x1.cfg that is available in the Java3D documentation)
In order to get it working on a 64-bit machine, I had to use the 64-bit dlls for Java3D, which someone has helpfully provided on the Java3D forums (http://home.java.net/node/705510) This may or may not be required on a 32-bit system.
Future releases of the nVidia drivers promise improved support for windowed-mode stereo, so its possible that this will become easier in the future (possibly eliminating the need for option 3 and/or 4 above)
I'll also investigate if rebuilding the Java3D source with some additional nVidia bindings can eliminate the fullscreen requirement, and repost with the results.
Java3D uses something called "Quadbuffer Stereo", i.e. two distinct doublebuffered framebuffers, one for the left and one for the right eye. NVidia considers this thing "professional grade", although it is impossible to to proper stereoscopy without accurate control of the content of both eyes.
3D Vision however inserts some shaders to create an in-situ stereo separation. This however only works with programs giving it some hints through the 3D Vision API. Java3D doesn't.
Let's just hope that Microsoft will indeed push Quadbuffered Stereo as a core requirement of DirectX 12 Graphics, so that NVidia has no longer lame excuses to keep this from "consumer grade" OpenGL.
java3D does not support the 3d vision api but it may work with any direct x program
This is may outdated, but I have the binary DirectX libs for Java3D 64. It is the j3dcore-d3d.dll .
If someone needs it, send a message to me.
From readme.txt file :
======
README
Java3D DirectX 9.0 Renderer for 64bits
January, 2010
Hi
The companion j3dcore-d3d.dll is a Java3D renderer
for Microsoft DirectX D3D in 64bit. This uses exactly
same source code from 32bits, but compiled with Visual
studio 2005 for Windows 64bits.
It was tested with Windows XP Pro. 64bit Edition,
Vista 64 and Windows-7 64. In both nVidia and ATI drivers.
Usage
Drop the j3dcore-d3d.dll in the same folder you have j3dcore-ogl.dll.
Dependency
Use it with a 64bit JRE.
It requires Java3D for Windows AMD 64, available at
https://java3d.dev.java.net/binary-builds.html
It also needs a DirectX driver update
You can get it http:\www.microsoft.com\directx
Current full link for DirectX Web Setup :
http://www.microsoft.com/downloads/details.aspx?familyid=2DA43D38-DB71-4...
This last link points to a very small web updater for DirectX runtime.
Disclaimer
This is for testing purposes only. Use it at your own risk.
This IS NOT endorsed or supported by Sun, Oracle or any other company.

Categories

Resources