Java Reference
In-Depth Information
(0,0)
(0,0)
sl
sr
tl
tr
st
tt
tb
sb
Image
Panel
Source rectangle
Target rectangle
(a)
(b)
Figure 15.3 How to use method drawImage to display part of a picture. (a) A source
rectangle is specified by its left, right, top and bottom coordinates. (b) A target rectangle is
specified in the same way. The content of the source rectangle is stretched or shrunk to fit
into the target rectangle and displayed there
Rotating an image is not supported by the Graphics class. As the pixels of
images are normally organized in a row-wise fashion, rotations require substantial
computational effort.
We illustrate the use of drawImage in application ImageCutAndMirrorFrame .
The frame contains CutAndMirrorPanel as an internal class. In the constructor of
the frame, the digitalized photo orange.png is loaded and passed to the panel. The
photo is 400
400 pixels. The paintComponent method of the panel contains six
drawImage commands which are described below. The left and top parameters
are chosen such that there are always 10 pixels between any two images. The
different drawImage commands are labelled A - G in the listing.
×
(A) draws the original image of size 400
×
400 at position (10
,
10) of the panel.
(B) draws an image to the right of the original one at position (420
10). The height
is the original one (400) but the width is only 50, i.e. the image is horizontally
shrunk to one-eighth.
,
(C) draws the image even further right in original height (400) but only 100 pixels
wide and vertically reflected. The reflection is achieved by letting width be neg-
ative (
10)
therefore denotes the right upper corner of the displayed image which is the
left upper corner of the original.
100). As mentioned above, this swaps left and right. Position (580
,
(D) draws an image below the original one at position (10
420). The width is the
original one (400) but the height is only 50, i.e. the image is vertically shrunk
to one-eighth.
,
(E) draws the image even further down in original width (400) but only 100 pixels
high and horizontally reflected. The reflection is achieved by letting height
Search WWH ::




Custom Search