diff --git a/src/dorkbox/util/ActionHandler.java b/src/dorkbox/util/ActionHandler.java deleted file mode 100644 index 41d2a80..0000000 --- a/src/dorkbox/util/ActionHandler.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2015 dorkbox, llc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package dorkbox.util; - -public -interface ActionHandler { - void handle(T value); -} diff --git a/src/dorkbox/util/ActionHandlerLong.java b/src/dorkbox/util/ActionHandlerLong.java deleted file mode 100644 index b79f549..0000000 --- a/src/dorkbox/util/ActionHandlerLong.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2015 dorkbox, llc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package dorkbox.util; - -public -interface ActionHandlerLong { - void handle(long value); -} diff --git a/src/dorkbox/util/ImageUtil.java b/src/dorkbox/util/ImageUtil.java index 37c9b91..d8d3d67 100644 --- a/src/dorkbox/util/ImageUtil.java +++ b/src/dorkbox/util/ImageUtil.java @@ -17,9 +17,11 @@ package dorkbox.util; import java.awt.AlphaComposite; import java.awt.Color; +import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Image; +import java.awt.MediaTracker; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; @@ -29,6 +31,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Iterator; +import java.util.concurrent.atomic.AtomicInteger; import javax.imageio.ImageIO; import javax.imageio.ImageReader; @@ -36,6 +39,8 @@ import javax.imageio.stream.ImageInputStream; import javax.swing.Icon; import javax.swing.ImageIcon; +import org.slf4j.LoggerFactory; + import dorkbox.os.OS; @SuppressWarnings("WeakerAccess") @@ -161,7 +166,7 @@ class ImageUtil { image = new ImageIcon(systemResource).getImage(); } - image = ImageUtil.getImageImmediate(image); + ImageUtil.waitForImageLoad(image); // make whatever dirs we need to. boolean mkdirs = newFile.getParentFile() @@ -379,22 +384,41 @@ class ImageUtil { throw new IOException("Unable to read file inputStream for image size data."); } + + private static final Object mediaTrackerLock = new Object(); + private static final AtomicInteger imageTrackerIndex = new AtomicInteger(0); + private static MediaTracker tracker = null; + /** - * Because of the way image loading works in Java, if one wants to IMMEDIATELY get a fully loaded image, one must resort to "hacks" - * by loading the image twice. + * Wait until the image is fully loaded and then init the graphics. * * @param image the image you want load immediately - * - * @return a fully loaded image */ public static - Image getImageImmediate(final Image image) { - // have to do this twice, so that it will finish loading the image (weird callback stuff is required if we don't do this) - image.flush(); + void waitForImageLoad(final Image image) { + int imageId = imageTrackerIndex.getAndIncrement(); - final Image loadedImage = new ImageIcon(image).getImage(); - loadedImage.flush(); + // make sure the image if fully loaded + synchronized (mediaTrackerLock) { + if (tracker == null) { + tracker = new MediaTracker(new Component() {}); + } - return loadedImage; + tracker.addImage(image, imageId); + } + + try { + tracker.waitForID(imageId); + if (tracker.isErrorID(imageId)) { + LoggerFactory.getLogger(ImageUtil.class).error("Error loading image!"); + } + } + catch (InterruptedException ignored) { + + } finally { + synchronized (mediaTrackerLock) { + tracker.removeImage(image); + } + } } }