Java Reference
In-Depth Information
used depends on the third hints parameter. For example, the constant
Image.SCALE - AREA - AVERAGING indicates that averaging around each point
in the image should be used for the scaling. The Image class offers five options
for scaling (see the class specification for details).
11.5 Creating images
Images can be created in Java in several ways. We can, for example, use the
createImage() method from the Component class as in the following
snippet:
...
Image image = createImage (width, height);
Graphics g = image.getGraphics ();
paint (g);
...
Here we created an Image object and obtained its graphics context. We then
passed the context to our paint() method, which draws on the image rather
than on a component. Such an off-screen image can be used for double buffer-
ing to speed up the graphics. Rather than sending lots of individual draw oper-
ations to the display device, a frame is first drawn on the off-screen image
and then the image is sent to the display in a single operation. This saves a signif-
icant amount of time and is very useful for eliminating flickering in animations
on AWT components. Swing components, however, provide double buffering by
default.
The BufferedImage class provides a constructor in which you pass the
desired dimensions and a parameter indicating the type of object:
BufferedImage buffered - image =
new BufferedImage (width, height,
BufferedImage.TYPE - INT - RGB);
g = buffered - image.getGraphics ();
paint (g);
The overridden getGraphics() method for BufferedImage returns a
Graphics2D object typed as its superclass Graphics type and so must be
cast to Graphics2D to use its methods. This method is present for backward
compatibility. The preferred alternative is to use createGraphics() ,which
explicitly returns a Graphics2D type.
The Java 2D API offers many image tools that work primarily with the
BufferedImage class. You might encounter a situation where you get an Image
object but you want to apply Java 2D operations to the image. You can obtain a
BufferedImage object from an Image object by drawing the image onto the
graphics context of the BufferedImage :
Graphics2D buf - image - graphics = buffered - image.createGraphics ();
buf - image - graphics.drawImage (image, 0, 0, null);
Search WWH ::




Custom Search