65a66,71 > /********************************************************************* > * > * Basic methods for image/icon retrieval. > * > ********************************************************************/ > 67,70c73,74 < * Loads all registered images. < * If component is null, the component supplied in the < * constructor will be used. < * @see #registerImage --- > * Just gets the image pointed to by the URL and doesn't store > * it in cache. 72,91c76,77 < public void loadRegisteredImages(Component component) { < if (fRegisteredImages.size() == 0) < return; < < if (component == null) { < component = fComponent; < } < < MediaTracker tracker = new MediaTracker(component); < // register images with MediaTracker < Iterator iter = fRegisteredImages.iterator(); < while (iter.hasNext()) { < URL url = (URL)iter.next(); < if (basicGetImageURL(url) == null) { < tracker.addImage(loadImageURL(url), ID); < } < } < fRegisteredImages.clear(); < < // block until all images are loaded --- > public Image loadImageUncachedURL(URL url) { > Toolkit toolkit = Toolkit.getDefaultToolkit(); 93c79 < tracker.waitForAll(); --- > return toolkit.createImage((ImageProducer) url.getContent()); 95,96c81,82 < catch (Exception e) { < // ignore: do nothing --- > catch (Exception ex) { > return null; 101,103c87 < * Registers a URL that is then loaded together with < * the other registered images by loadRegisteredImages. < * @see #loadRegisteredImages --- > * Just gets the file but doesn't store it in cache. 105,106c89,90 < public void registerImageURL(URL url) { < fRegisteredImages.add(url); --- > public Image loadImageUncached(String fileName) { > return loadImageUncachedURL(getResourceURL(fileName)); 110,111c94,95 < * Registers the URL for the image resource < * @see #registerImageURL --- > * Loads an image URL with the given name, caches it, and > * optionally waits for it to finish loading. 113,120c97 < public void registerImage(String fileName) { < registerImageURL(getResourceURL(fileName)); < } < < /** < * Loads an image URL with the given name. < */ < public Image loadImageURL(URL url) { --- > public Image loadImageURL(URL url, boolean waitForLoad) { 124c101,103 < Image image = loadImageResourceURL(url); --- > > Image image = loadImageUncachedURL(url); > 126a106,108 > if (waitForLoad) { > waitForLoadedImage(image); > } 127a110 > 132c115,130 < * Loads an image with the given name. --- > * Loads an image file with the given name, caches it, and > * optionally waits for it to finish loading. > */ > public Image loadImage(String fileName, boolean waitForLoad) { > return loadImageURL(getResourceURL(fileName), waitForLoad); > } > > /** > * Loads an image URL with the given name and caches it > */ > public Image loadImageURL(URL url) { > return loadImageURL(url, false); > } > > /** > * Loads an image with the given fileName and caches it. 135c133 < return loadImageURL(getResourceURL(fileName)); --- > return loadImageURL(getResourceURL(fileName), false); 138,141c136,144 < public Image loadImageResourceURL(URL url) { < Toolkit toolkit = Toolkit.getDefaultToolkit(); < try { < return toolkit.createImage((ImageProducer) url.getContent()); --- > /** > * Blocks while image loads and returns a completely loaded > * version of image. > */ > public Image waitForLoadedImage(Image image) { > if (image!=null) { > ImageIcon icon = new ImageIcon(image); > // icon.getImage forces the wait to happen > image = icon.getImage(); 143,144c146 < catch (Exception ex) { < return null; --- > return image; 145a148,153 > > /** > * To translate between a resource and a URL > */ > private URL getResourceURL(String resourceName) { > return getClass().getResource(resourceName); 148,149c156,162 < public Image loadImageResource(String fileName) { < return loadImageResourceURL(getResourceURL(fileName)); --- > /** > * Registers a URL that is then loaded together with > * the other registered images by loadRegisteredImages. > * @see #loadRegisteredImages > */ > public void registerImageURL(URL url) { > fRegisteredImages.add(url); 153,155c166,167 < * Registers and loads an image. < * If component is null, the component supplied in the < * constructor will be used. --- > * Registers the URL for the image resource > * @see #registerImageURL 157,160c169,170 < public Image registerAndLoadImageURL(Component component, URL url) { < registerImageURL(url); < loadRegisteredImages(component); < return getImageURL(url); --- > public void registerImage(String fileName) { > registerImageURL(getResourceURL(fileName)); 161a172 > 163c174 < * Registers and loads an image. --- > * Loads all registered images. 165a177,178 > * @see #registerImage > * @see #registerImageURL 167,168c180,185 < public Image registerAndLoadImage(Component component, String fileName) { < return registerAndLoadImageURL(component, getResourceURL(fileName)); --- > public void loadRegisteredImages(Component component) { > if (fRegisteredImages.size() == 0) > return; > > if (component == null) { > component = fComponent; 171,175c188,194 < public Image loadImageURL(URL url, boolean waitForLoad) { < Image image = loadImageURL(url); < if (image!=null && waitForLoad) { < ImageIcon icon = new ImageIcon(image); < image = icon.getImage(); //this forces the wait to happen --- > MediaTracker tracker = new MediaTracker(component); > // register images with MediaTracker > Iterator iter = fRegisteredImages.iterator(); > while (iter.hasNext()) { > URL url = (URL)iter.next(); > if (! fMap.containsKey(url)) { > tracker.addImage(loadImageURL(url), ID); 177d195 < return image; 178a197 > fRegisteredImages.clear(); 180,181c199,201 < public Image loadImage(String fileName, boolean waitForLoad) { < return loadImageURL(getResourceURL(fileName), waitForLoad); --- > // block until all images are loaded > try { > tracker.waitForAll(); 183,192c203,204 < < /** < * Gets the image with the given URL. If the image can't be < * found it tries it again after registering the image and < * loading all the registered images. < */ < public Image getImageURL(URL url) { < Image image = basicGetImageURL(url); < if (image != null) { < return image; --- > catch (Exception e) { > // ignore: do nothing 194,198d205 < registerImageURL(url); < // load registered images and try again < loadRegisteredImages(fComponent); < // try again < return basicGetImageURL(url); 199a207,213 > > /********************************************************************* > * > * Deprecated methods > * > ********************************************************************/ > 203a218,219 > * > * @deprecated use loadImage instead 206c222 < return getImageURL(getResourceURL(fileName)); --- > return loadImage(fileName, true); 209,210c225,235 < private URL getResourceURL(String resourceName) { < return getClass().getResource(resourceName); --- > /** > * Registers and loads an image. > * If component is null, the component supplied in the > * constructor will be used. > * > * @deprecated use loadImage instead > */ > public Image registerAndLoadImage(Component component, String fileName) { > registerImage(fileName); > loadRegisteredImages(component); > return loadImage(fileName, true); 213,217c238,244 < private Image basicGetImageURL(URL url) { < if (fMap.containsKey(url)) { < return (Image) fMap.get(url); < } < return null; --- > /** > * Loads an image but does not put in in the cache. > * > * @deprecated use loadImageUncached instead > */ > public Image loadImageResource(String fileName) { > return loadImageUncached(fileName); 219a247 >