jfree / jfreechart

A 2D chart library for Java applications (JavaFX, Swing or server-side).
http://www.jfree.org/jfreechart/
GNU Lesser General Public License v2.1
1.22k stars 481 forks source link

Unnecessary reference to Swing in headless mode causes considerable delay #398

Open gerolfscherr opened 7 months ago

gerolfscherr commented 7 months ago

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:

Exception in thread "main" java.awt.AWTError: Assistive Technology not found: org.GNOME.Accessibility.AtkWrapper
    at java.awt.Toolkit.loadAssistiveTechnologies(Toolkit.java:807)
    at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:886)
    at sun.swing.SwingUtilities2.getSystemMnemonicKeyMask(SwingUtilities2.java:2032)
    at javax.swing.plaf.basic.BasicLookAndFeel.initComponentDefaults(BasicLookAndFeel.java:1158)
    at javax.swing.plaf.metal.MetalLookAndFeel.initComponentDefaults(MetalLookAndFeel.java:431)
    at javax.swing.plaf.basic.BasicLookAndFeel.getDefaults(BasicLookAndFeel.java:148)
    at javax.swing.plaf.metal.MetalLookAndFeel.getDefaults(MetalLookAndFeel.java:1577)
    at javax.swing.UIManager.setLookAndFeel(UIManager.java:539)
    at javax.swing.UIManager.setLookAndFeel(UIManager.java:579)
    at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1349)
    at javax.swing.UIManager.initialize(UIManager.java:1459)
    at javax.swing.UIManager.maybeInitialize(UIManager.java:1426)
    at javax.swing.UIManager.getDefaults(UIManager.java:659)
    at javax.swing.UIManager.getColor(UIManager.java:701)
    at org.jfree.chart.JFreeChart.<clinit>(JFreeChart.java:144)
    at org.jfree.chart.ChartFactory.createStackedBarChart(ChartFactory.java:895)
    at org.jfree.chart.ChartFactory.createStackedBarChart(ChartFactory.java:847)
...
trashgod commented 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?

gerolfscherr commented 7 months ago

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.

trashgod commented 7 months ago

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.