Exemplo n.º 1
0
  /**
   * Get the image associated with the given location string. If the image has already been loaded,
   * it simply will return the image, otherwise it will load it from the specified location.
   *
   * <p>The imageLocation argument must be a valid resource string pointing to either (a) a valid
   * URL, (b) a file on the classpath, or (c) a file on the local filesystem. The location will be
   * resolved in that order.
   *
   * @param imageLocation the image location as a resource string.
   * @return the corresponding image, if available
   */
  public Image getImage(String imageLocation) {
    Image image = (Image) imageCache.get(imageLocation);
    if (image == null && !loadMap.containsKey(imageLocation)) {
      URL imageURL = IOLib.urlFromString(imageLocation);
      if (imageURL == null) {
        System.err.println("Null image: " + imageLocation);
        return null;
      }
      image = Toolkit.getDefaultToolkit().createImage(imageURL);

      // if set for synchronous mode, block for image to load.
      if (!m_asynch) {
        waitForImage(image);
        addImage(imageLocation, image);
      } else {
        int id = ++nextTrackerID;
        tracker.addImage(image, id);
        loadMap.put(imageLocation, new LoadMapEntry(id, image));
      }
    } else if (image == null && loadMap.containsKey(imageLocation)) {
      LoadMapEntry entry = (LoadMapEntry) loadMap.get(imageLocation);
      if (tracker.checkID(entry.id, true)) {
        addImage(imageLocation, entry.image);
        loadMap.remove(imageLocation);
        tracker.removeImage(entry.image, entry.id);
      }
    } else {
      return image;
    }
    return (Image) imageCache.get(imageLocation);
  }