From f4606cfec305f216329059af64e3f75af3ebaa91 Mon Sep 17 00:00:00 2001 From: weisj Date: Sat, 23 Jan 2021 15:15:08 +0100 Subject: [PATCH] Use component as image observer if possible. --- .../weisj/darklaf/icons/DerivableImageIcon.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/property-loader/src/main/java/com/github/weisj/darklaf/icons/DerivableImageIcon.java b/property-loader/src/main/java/com/github/weisj/darklaf/icons/DerivableImageIcon.java index 2666af27..d091207a 100644 --- a/property-loader/src/main/java/com/github/weisj/darklaf/icons/DerivableImageIcon.java +++ b/property-loader/src/main/java/com/github/weisj/darklaf/icons/DerivableImageIcon.java @@ -251,19 +251,24 @@ public class DerivableImageIcon implements DerivableIcon, Im public void paintIcon(final Component c, final Graphics g, final int x, final int y) { Image img = image.get(); if (img != null) { - if (ensureImageLoaded(img, c)) { - g.drawImage(img, x, y, width, height, null); + if (c == null) { + // Component is null and can't be used as an image observer for painting. + // To ensure the image is painted anyway we force load it. + if (!ensureImageLoaded(img)) { + LOGGER.warning("Image could not be loaded in time for painting."); + return; + } } + g.drawImage(img, x, y, width, height, c); } } - private boolean ensureImageLoaded(final Image img, final Component c) { - MediaTracker tracker = new MediaTracker(c != null ? c : new Component() {}); + private boolean ensureImageLoaded(final Image img) { + MediaTracker tracker = new MediaTracker(new Component() {}); tracker.addImage(img, 0); try { tracker.waitForAll(); } catch (final InterruptedException ex) { - LOGGER.warning("Image could not be loaded in time for painting."); return false; } return true;