Files
narya/src/java/com/threerings/media/image/ImageManager.java
T
Walter Korman 2572535f95 General updating per initial code review. Created EditableSceneView
to separate scene display from editing functionality.  Load and
initialize managers in a fashion that more appropriately hides the
implementation details.  Tidied comments up a bit.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@103 542714f4-19e9-0310-aa3c-eee0fc999fb1
2001-07-23 18:52:51 +00:00

101 lines
2.5 KiB
Java

//
// $Id: ImageManager.java,v 1.3 2001/07/23 18:52:51 shaper Exp $
package com.threerings.media;
import com.threerings.miso.Log;
import com.threerings.resource.ResourceManager;
import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.util.Hashtable;
/**
* The ImageManager class provides a single point of access for image
* retrieval and caching.
*
* <p> <b>Note:</b> The ImageManager must be constructed with a root
* AWT component, in the interest of allowing for proper preparation
* of images for optimal storage and eventual display.
*/
public class ImageManager
{
/**
* Construct an ImageManager object with the ResourceManager from
* which it will obtain its data, and a root component to which
* images will be rendered.
*/
public ImageManager (ResourceManager rmgr, Component root)
{
_rmgr = rmgr;
_root = root;
_tk = root.getToolkit();
}
/**
* Load the image from the specified filename and cache it for
* faster retrieval henceforth.
*/
public Image getImage (String fname)
{
if (_root == null) {
Log.warning("Attempt to get image without valid root component.");
return null;
}
Image img = (Image)_imgs.get(fname);
if (img != null) {
Log.info("Retrieved image from cache [fname=" + fname + "].");
return img;
}
Log.info("Loading image into cache [fname=" + fname + "].");
try {
byte[] data = _rmgr.getResourceAsBytes(fname);
img = _tk.createImage(data);
MediaTracker tracker = new MediaTracker(_root);
tracker.addImage(img, 0);
tracker.waitForID(0);
if (tracker.isErrorAny()) {
Log.warning("Error loading image [fname=" + fname + "].");
return null;
} else {
_imgs.put(fname, img);
}
} catch (IOException ioe) {
Log.warning("Exception loading image [ioe=" + ioe + "].");
} catch (InterruptedException ie) {
Log.warning("Interrupted loading image [ie=" + ie + "].");
}
return img;
}
/**
* Creates a new image representing the specified rectangular
* section cropped from the specified full image object.
*/
public Image getImageCropped (Image fullImg, int x, int y,
int width, int height)
{
BufferedImage img =
new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.drawImage(fullImg, -x, -y, null);
return img;
}
protected ResourceManager _rmgr;
protected Component _root;
protected Hashtable _imgs = new Hashtable();
protected Toolkit _tk = Toolkit.getDefaultToolkit();
}