Java Reference
In-Depth Information
11.4 Image display
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.
The class contains several other overloaded versions of drawImage() .For
example, the method
boolean drawImage (Image img, int x, int y, int width, int
height, ImageObserver io)
draws the image with the top left corner at the specified origin but scales the
image such that its width and height fit the given values.
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 between 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.
While not specific to images, a section of a drawing area can be copied into
other parts of the area using
void copyArea (int x, int y,
int width, int height,
int dx, int dy)
This can be used, for example, to tile an image.
Yo u 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
Search WWH ::




Custom Search