The ImageLoader Class



The loadImageStrip and loadCell methods are basically wrapped within a class that saves a copy of the original image so that as many cells can be loaded as required without having to reconstruct the parent image. The work of loading image strips and image cells can be offset from your applet class and delegated elsewhere. All the ImageLoader class needs is the name of the image to load from file and a reference to the applet that is capable of loading the images. The ImageLoader class will do all of the maintenance work for you.

Here is the code for the ImageLoader class. Keep in mind that if you have any problem with the code, just think of it as the loadImageStrip and loadCell methods wrapped into a formal class structure.

Example:

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
public class ImageLoader extends Object
{
// an Applet to load and observe loading images
protected Applet applet;
// an Image, along with its width and height
protected Image image;
protected int imageWidth;
protected int imageHeight;
// a buffer to render images to immediately after they are loaded
protected static BufferedImage buffer = new BufferedImage(200, 200,
BufferedImage.TYPE_INT_RGB);
public ImageLoader(
Applet a, // creates and observes loading images
String filename, // name of image to load on disk
boolean wait // if true, add to a MediaTracker object and wait to
// finish loading
)
{
applet = a;
image = applet.getImage(applet.getDocumentBase(), filename);
if(wait)
{
// create a new MediaTracker object for this image
MediaTracker mt = new MediaTracker(applet);
// load the strip image
mt.addImage(image, 0);
try
{
// wait for our main image to load
mt.waitForID(0);
}
catch(InterruptedException e) { /* do nothing */ }
}
// get the width and height of the image
imageWidth = image.getWidth(applet);
imageHeight = image.getHeight(applet);
}
public int getImageWidth()
{
return imageWidth;
}
public int getImageHeight()
{
return imageHeight;
}
public Image getImage()
{
return image;
}
// extracts a cell from the image using an image filter
public Image extractCell(int x, int y, int width, int height)
{
// get the ImageProducer source of our main image
ImageProducer sourceProducer = image.getSource();
Image cell = applet.createImage(new FilteredImageSource(sourceProducer,
new CropImageFilter(x, y, width, height)));
// draw the cell to the off-screen buffer
buffer.getGraphics().drawImage(cell, 0, 0, applet);
return cell;
}
// extracts a cell from the image and scales it to the sent width and height
public Image extractCellScaled(int x, int y, int width, int height,
int sw, int sh)
{
// get the ImageProducer source of our main image
ImageProducer sourceProducer = image.getSource();
Image cell = applet.createImage(new FilteredImageSource(sourceProducer,
new CropImageFilter(x, y, width, height)));
// draw the cell to the off-screen buffer
buffer.getGraphics().drawImage(cell, 0, 0, applet);
return cell.getScaledInstance(sw, sh, Image.SCALE_SMOOTH);
}
} // ImageLoader