Open gerolfscherr opened 7 months ago
A related problem is examined in issue #324. If I understand correctly, Color
is OK, but UIManager
is not. Is public static final Paint DEFAULT_BACKGROUND_PAINT = Color.GRAY
any better?
it's java.awt.Color
vs javax.swing.UIManager
. The latter initializes swing whereas the former doesn't.
A suggested fix would be:
public static final Paint DEFAULT_BACKGROUND_PAINT =
GraphicsEnvironment.isHeadless() ? new Color(238,238,238) : UIManager.getColor("Panel.background");
Why 238,238,238 instead of Color.GRAY? This is the color value returned by the (default)UIManager (on my linux machine). So the default behaviour would be practially the same in the headless case so but without the performance penalty.
Similar shades of light gray are common defaults for the Panel.background
key in various L&Fs; I see it in com.apple.laf.AquaLookAndFeel
and javax.swing.plaf.metal.MetalLookAndFeel
too. At a guess, it's an accessibility choice related to contrast.
I am generating some .PNGs with some nice charts on a server, so JFreeChart is running with a headless JDK. (openjdk-8-jdk-headless) When rendering an image, I get the error message shown below.
The root cause is this: https://github.com/jfree/jfreechart/blob/e2d6788d594c51941ddae337ae666fda5c52ad9f/src/main/java/org/jfree/chart/JFreeChart.java#L141
which references UIManager and thus tries to load and initialize Swing, which is pointless on a headless JDK.
A workaround is to set the following system property at startup:
-Djavax.accessibility.assistive_technologies=" "
(notice the blank space), but Swing still gets loaded and initialized on the server. On my current development machine the difference is about 150ms but the target platform is a tiny old embedded machine. When removing the reference to UIManager on the line managed above, the program runs about 2 sec(!) faster.Suggested fix: if headless, set it to some reasonable default without accessing the Swing UIManager, e.g.:
public static final Paint DEFAULT_BACKGROUND_PAINT = GraphicsEnvironment.isHeadless() ? Color.GRAY : UIManager.getColor("Panel.background");
Exception: