Java Reference
In-Depth Information
45.
g.drawImage(im,10,10, this );
// (A)
46.
g.drawImage(im,420,10,50,400, this );
// (B)
47.
g.drawImage(im,580,10,-100,400, this );
// (C)
48.
g.drawImage(im,10,420,400,50, this );
// (D)
49.
g.drawImage(im,10,580,400,-100, this );
// (E)
50.
g.drawImage(im,420,420,580,580,250,130,290,150, this ); // (F)
51.
g.setColor(Color.white);
52.
g.drawRect(260,140,40,20);
// (G)
53. }
54. }// internal class
55. }// class
The resizing done in the previous paragraphs affected only the displayed image,
not the one contained in the Image variable. We now describe how an image
can be physically resized. Such a resizing can, for example, be used to generate
thumbnails for digitized photos. Think of an application that has to display an
overview of a number of digitized photos. Such photos need several megabytes of
memory each when stored as an instance of class Image . Using the appropriate
drawImage method to display a miniaturized version does not solve the memory
problem because only the display is resized while the picture in the Image variable
maintains its full size. Then a few photos fill the main memory and the application
is slowed down owing to swapping.
We now describe how smaller (or larger) copies of pictures can be generated. To
overcome the aforementioned memory problem one can load the photos one at a
time, generate small copies and keep just those. Only the copies are maintained in
the memory. The programmer has to make sure that references to the original big
image are destroyed (e.g. by use of local variables) so that the automatic garbage
collection of Java releases the memory.
We use the following method of class Image to create a resized copy of an image:
Image getScaledInstance( int width, int height, int hints)
Arguments width and height specify the width and height of the resulting new
image. The third argument hints determines which technique is used to enlarge
or shrink the image. For an enlargement additional pixels have to be generated, for
a miniaturization several pixels have to be combined. The quality of the resulting
picture depends on the way this is done. A better quality usually requires a longer
conversion time. We list the constants for hints which are defined in class Image
together with the qualities achieved by the corresponding conversion techniques.
SCALE_DEFAULT Good trade-off between speed and quality
SCALE_FAST Fast conversion and moderate quality
SCALE_SMOOTH Reasonably good quality
SCALE_REPLICATE Good quality
SCALE_AREA_AVERAGING Good quality
Search WWH ::




Custom Search