We briefly discussed the java.awt.Image
class in Chapter 6: Java : Images.
There we showed how to create an instance of the Image
class by loading an image file.
The java.awt.Image
class is actually abstract. However, methods such as getImage(URL)
in the Applet
class return an instance of a concrete subclass provided by the
particular JVM implementation. The details of that subclass are
not important since you invoke only methods listed in the
Image class, many of which are overridden by the subclass.
The BufferedImage
subclass of Image,
on the other hand, is accessible to the user and it offers many
features not available with Image.
We discuss BufferedImage
in the Chapter
11: Supplements section on Java2D image processing techniques.
The Image
class provides only minimal access to information about the image.
It does include the methods
int
getWidth (ImageObserver)
int getHeight (ImageObserver)
that return the dimensions of an image.
You can draw on an image just as you would
draw on a component by obtaining the graphics
context object via the Image
class method
Graphics
getGraphics ()
You then invoke the usual drawing
methods in the Graphics
object to draw on the image.
You can create an image just to draw on it
using the createImage()
method from the Component
class as in the following snippet:
...
Image image = createImage (width, height);
Graphics g = image.getGraphics ();
paint (g); // Your paint() method does the usual drawing operations
//
but on the image rather than on a component.
Such an off-screen image has the advantage
of fast execution. For example, all of the operations needed to
draw a frame in an animation can first be peformed on an off-screen
image and then the image is sent to the visible component in one
operation. This double-buffering technique is discussed further
in Chapter
11: Supplements: AWT Flicker where it is used to reducing the
flickering in animations with the AWT components. (Swing components
use double-buffering by default.)
We discuss in Pixel Handling
how to access the individual pixels in an Image
object. We also look at how to create Image
objects from pixel arrays.
References & Web Resources
Last update: Dec. 3, 2004
|