Use the following as a guide:
1. Get the image (path below must be absolute):
public static Image getImage(String filename) throws ScanException {
Image img = null;
// Read paths from properties file
StringBuffer buf = new StringBuffer("\\");
buf.append(ScanUtility.getPathImages());
buf.append("\\");
buf.append(filename);
buf.append(".jpg");
File dataFile = new File(buf.toString());
if(!dataFile.exists())
throw new ScanException("Cannot locate the file:\n" + dataFile.getAbsolutePath());
img = Toolkit.getDefaultToolkit().createImage(buf.toString());
return img;
}
2. Create a class to display the image (pass in the image, above):
ImagePanel class source:
import java.awt.*;
import java.awt.image.*;
public class ImagePanel extends Panel {
private Image image = null;
private Image scaledImage = null;
private boolean imageComplete = false;
public ImagePanel(Image img) {
image = img;
scaledImage = image.getScaledInstance(200, -1, Image.SCALE_DEFAULT);
prepareImage(scaledImage, this);
}
// Override the paint method, to render the image
public void paint(Graphics g) {
if(imageComplete) {
g.drawImage(scaledImage, 0, 0, this);
}
}
// Override imageUpdate method, to prevent repaint() from trying
// to do anything until the image is scaled
public boolean imageUpdate(Image img, int infoFlags, int x, int y, int w, int h) {
if(infoFlags == ImageObserver.ALLBITS) {
imageComplete = true;
repaint();
}
return super.imageUpdate(img, infoFlags, x, y, w, h);
}
public void flush() {
image.flush();
scaledImage.flush();
image = null;
scaledImage = null;
}
}