To display an image on a component, we have used the
Graphics class method
boolean drawImage
(Image img, int x, int y, ImageObserver io)
Here the position of the top left corner of
the image is specified by the x and y values. The width and height
of the drawn image go according to the image's dimensions. If this
is less than the drawing area, the background color of the component
is visible in the uncovered regions. If the image is larger than
the component area, then the portion of the image outside the component
won't be seen.
The x and y values can actually be negative, in effect
moving the top left corner of the image up and/or to the left of
the drawing area. In such a case, only the region of the image within
the component area will be visible.
Scaled images can be drawn with
boolean drawImage(Image img, int x, int y,
int
width, int height, ImageObserver observer)
where the width and height and be larger or smaller
than the given size of the image, thus expanding or shrinking the
image.
The method
boolean drawImage
(Image img,
int
dx1, int dy1,
int dx2, int dy2,
int sx1, int sy1,
int
sx2, int sy2,
Color
bg_color, ImageObserver io)
specifies that the rectangle between the corners sx1,sy1
and sx2,sy2
of the source image is drawn into the rectangle specified by the
corners dx1,dy1
and dx2,dy2
of the destination image. The areas not covered in the destination
component are painted with the color specified by the bg_color
parameter.
Sections of an image can be copied into other parts of the
screen method (useful for tiling)
void
copyArea(int x, int y,
int width, int height,
int destDx, int destDy)
You can also obtain a new Image
object that represents a scaled version of an image with the getScaledInstance()
method as in
..
Image img = getImage (url);
Image scaled_img = getScaledInstance (width, height, hints);
Here the new image referenced by scaled_img will have the width
and height as given in the first two parameters. The type of scaling
algorithm 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).
References & Web Resources
Latest update: Dec.1, 2004
|