예제 #1
0
  /**
   * 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
예제 #2
0
 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");
 }
예제 #3
0
파일: Tile.java 프로젝트: heroyuy/systar
  /**
   * Returns a scaled instance of the tile image. Using a MediaTracker instance, this function waits
   * until the scaling operation is done.
   *
   * <p>Internally it caches the scaled image in order to optimize the common case, where the same
   * scale is requested as the last time.
   *
   * @param zo
   * @return Image
   */
  public Image getScaledImage(double zo) {
    Image scaledImage = null;
    if (zo == 1.0) {
      return getImage();
    } else if (zo == zoom && scaledImage != null) {
      return scaledImage;
    } else {
      Image img = getImage();
      //            System.out.println(getWidth());
      if (img != null) {
        scaledImage =
            img.getScaledInstance(
                (int) (getWidth() * zo), (int) (getHeight() * zo), BufferedImage.SCALE_SMOOTH);

        MediaTracker mediaTracker = new MediaTracker(new Canvas());
        mediaTracker.addImage(scaledImage, 0);
        try {
          mediaTracker.waitForID(0);
        } catch (InterruptedException ie) {
          System.err.println(ie);
        }
        mediaTracker.removeImage(scaledImage);
        zoom = zo;
        return scaledImage;
      }
    }

    return null;
  }
예제 #4
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);
  }
예제 #5
0
  public void updateScreen() {
    boolean olay = false;

    int imageSize = 0;
    URL url = null;
    BufferedInputStream bs = null;
    DataInputStream ds = null;
    int i = 0;

    delay(1000);

    String imageName = new String();
    imageName = "initial.jpg";
    diskWidth = 512;
    diskHeight = 512;
    x = 0;
    y = 0;

    if (screenMode != 5) {
      olay = true;
      read_Overlay(0);
    } else {
      eraseAll();
      olay = false;
    }

    imageSize = diskWidth * diskHeight;

    try {
      url = new URL(codebase, imageName);
    } catch (MalformedURLException e1) {
      System.out.println("URL Error");
    }

    if (screenMode == 6) // Spectrum
    read_Overlay(1);
    else if (screenMode == 7) // X-Ray Map
    getJpegImage(url, x, y, diskWidth, diskHeight);
    else if (screenMode != 9) // All else
    readJpegImage(url, x, y, diskWidth, diskHeight);

    if (screenMode != 7) // Combine Mem and Overlay except for X-Ray Map
    combine_Mem_Olay(screenMode, olay);

    MemoryImageSource mis = new MemoryImageSource(512, 512, pixel, 0, 512);
    image = createImage(mis);
    tracker.addImage(image, 0, 512, 512);

    try {
      tracker.waitForAll();
    } catch (InterruptedException e) {
    }

    repaint();

    tracker.removeImage(image, 0);
  }
예제 #6
0
 /** Waits for given image to load. Use before querying image height/width/colors. */
 private static void waitForImage(Image image) {
   try {
     tracker.addImage(image, 0);
     tracker.waitForID(0);
     tracker.removeImage(image, 0);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }
예제 #7
0
 private synchronized void waitForImage(java.awt.Image image) {
   if (mediaTracker == null) mediaTracker = new MediaTracker(new PdfGraphics2D.FakeComponent());
   mediaTracker.addImage(image, 0);
   try {
     mediaTracker.waitForID(0);
   } catch (InterruptedException e) {
     // empty on purpose
   }
   mediaTracker.removeImage(image);
 }
예제 #8
0
 /**
  * Wait for an image to load.
  *
  * @param image the image to wait for
  */
 protected void waitForImage(Image image) {
   int id = ++nextTrackerID;
   tracker.addImage(image, id);
   try {
     tracker.waitForID(id, 0);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   tracker.removeImage(image, id);
 }
예제 #9
0
  public void readJpegImage(URL url, int x, int y, int diskWidth, int diskHeight) {
    Image img = null;
    PixelGrabber pg = null;
    int jpgpix[] = null;
    int i, ret;
    int index;
    boolean results = false;

    byte[] image;
    byte[] realImage;
    DataInputStream gifImage = null;
    int count = 0;

    Toolkit tk = Toolkit.getDefaultToolkit();
    img = tk.getImage(url);

    image = new byte[80 * 1024];
    try {
      gifImage = new DataInputStream(url.openStream());
    } catch (IOException e) {
      System.out.println("readJpegImage error1");
      return;
    }
    try {
      count = gifImage.read(image);
    } catch (IOException e) {
      System.out.println("readJpegImage error2");
    }

    realImage = new byte[count];
    System.arraycopy(image, 0, realImage, 0, count);
    img = tk.createImage(realImage);
    realImage = null;
    count = 0;

    tracker.addImage(img, 0);

    try {
      tracker.waitForAll();
    } catch (InterruptedException e) {
      System.out.println("Error wait for image load");
    }

    jpgpix = new int[diskWidth * diskHeight];
    pg = new PixelGrabber(img, 0, 0, diskWidth, diskHeight, jpgpix, 0, diskWidth);

    try {
      results = pg.grabPixels();
    } catch (InterruptedException e) {
      System.out.println("Grabbing interrupted " + e);
    } catch (Exception e1) {
      System.out.println("General exception grabbing " + e1);
    }

    for (i = 0; i < diskWidth * diskHeight; i++) {
      if (diskHeight == 256) index = 512 * y + x + i / 256 * 512 + i % 256;
      else index = 512 * y + i;
      membuf[index] = jpgpix[i];
      pixel[index] = jpgpix[i]; // added 7/15/99
    }
    tracker.removeImage(img, 0);
    try {
      gifImage.close();
    } catch (IOException e) {
      System.out.println("Can't Close gifImage");
    }
  }