Image objects are either "mutable" (you can draw on them) or "immutable" (you cannot draw on them).
You can find out which kind of image you have, using the isMutable() method. If this returns true, then getGraphics() will return a Graphics object. If false, then getGraphics() will throw an IllegalStateException.
Images created from a resource file are always immutable. You can check the JavaDocs for the kind of Image returned from each method.
The only way to create a mutable Image is to use createImage(width,height). This creates an image, in which all the pixels are white.
You can:
Code:
public static Image createMutableImage(String filename) throws IOException {
Image imgResource = Image.createImage(filename);
Image imgToDrawOn = Image.createImage(imgResource.getWidth(), imgResource.getHeight());
Graphics g = imgToDrawOn.getGraphics();
g.drawImage(imgResource, 0, 0, Graphics.TOP | Graphics.LEFT);
return imgToDrawOn;
}
However, since the mutable image starts with all white pixels, if the resource-image you draw on it contains any transparent pixels, the original white pixels will show through. As a result, you cannot create a mutable image in which any pixels are transparent.
(You can create a mutable image with transparent pixels by using the Nokia UI API, but you will be unable to use your code on non-Nokia devices.)
Graham.