Esempio 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);
  }
Esempio n. 2
0
  /**
   * Shows the image export dialog and processes the results.
   *
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public void actionPerformed(ActionEvent evt) {
    // lazy initialization
    if (chooser == null) init();

    // open image save dialog
    File f = null;
    scaler.setImage(display.getOffscreenBuffer());
    int returnVal = chooser.showSaveDialog(display);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      f = chooser.getSelectedFile();
    } else {
      return;
    }
    String format = ((SimpleFileFilter) chooser.getFileFilter()).getExtension();
    String ext = IOLib.getExtension(f);
    if (!format.equals(ext)) {
      f = new File(f.toString() + "." + format);
    }

    double scale = scaler.getScale();

    // save image
    boolean success = false;
    try {
      OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
      System.out.print("Saving image " + f.getName() + ", " + format + " format...");
      success = display.saveImage(out, format, scale);
      out.flush();
      out.close();
      System.out.println("\tDONE");
    } catch (Exception e) {
      success = false;
    }
    // show result dialog on failure
    if (!success) {
      JOptionPane.showMessageDialog(
          display, "Error Saving Image!", "Image Save Error", JOptionPane.ERROR_MESSAGE);
    }
  }