/** * Load the image located at URL. * * @param url URL where the image file is located. * @return loaded image at path or url * @see java.io.File#toURI() */ public static synchronized Image loadImage(URL url) { Image image = null; image = Toolkit.getDefaultToolkit().getImage(url); if (image != null) { s_tracker.addImage(image, 0); try { s_tracker.waitForAll(); } catch (InterruptedException e) { log.severe("Url= " + url + " - " + e.getMessage()); s_tracker.removeImage(image); image = null; } finally { if (image != null) s_tracker.removeImage(image); if (s_tracker.isErrorAny()) { log.severe("Tracker: " + s_tracker.getErrorsAny()[0]); image = null; } if (image != null) { if (image.getWidth(null) < 0 || image.getHeight(null) < 0) { log.severe("Image=0"); image = null; } } } } return image; } // loadImage
public static void ensureLoaded(Image img) throws Exception { // System.err.println("In ensureloaded"); mediatracker.addImage(img, 0); try { mediatracker.waitForAll(); if (mediatracker.getErrorsAny() != null) { mediatracker.removeImage(img); throw new Exception("Error loading image"); } } catch (InterruptedException e) { e.printStackTrace(); } mediatracker.removeImage(img); // System.err.println("Out ensureloaded"); }