Java Reference
In-Depth Information
To determine the width or height of an image we use the following two methods
!
of class Image which return, respectively, these two values in pixels. Note that
these values refer to the size of the picture in the Image variable. The actual
display might be enlarged or shrunk with techniques described later on.
int getWidth(ImageObserver imo)
int getHeight(ImageObserver imo)
An ImagePanel uses a vector images to store the images to be displayed. In
method paintComponent this vector is traversed. For every image in the vector its
width and height are determined for an adequate placement. This is done every
time the panel is repainted. Thus the pictures are always arranged to fit the width
of the panel, unless the panel is too small to fit a single picture in which case
the picture is partly invisible. In ImagePanel we define three methods to add a
picture:
addImage(Image picture)
addImage(String filename)
addImageAndTrack(String filename)
The first adds a picture that is already stored as an instance of class Image .
The second one loads a picture from a file. The third one also loads a picture
from a file but in a slightly different way that we shall explain later. In any case
a re-drawing of the panel is initiated after a picture is added by calling repaint .
The following listing contains the classes ImagePanel and ImagePanelFrame . The
latter one creates an ImagePanel and adds some pictures to it. The result is shown
in Figure 15.2.
Figure 15.2 ImagePanelFrame . Resizing the frame might change the arrangement of the
pictures
Search WWH ::




Custom Search