Java Reference
In-Depth Information
5.
6. public class ImagePanelFrame extends SimpleFrame
7. {
8. // Adjust the following path if necessary
9. private final String picturePath = "./its/TestData/";
10.
11. public ImagePanelFrame()
12.
{
13.
this .setSize(900,600);
14.
ImagePanel ip = new ImagePanel();
15.
this .getContentPane().add(ip,BorderLayout.CENTER);
16.
ip.addImage(picturePath+"strawberry.png");
17.
ip.addImage(picturePath+"banana.png");
18.
ip.addImage(picturePath+"lime.png");
19.
ip.addImage(picturePath+"plum.png");
20.
ip.addImage(picturePath+"lemon.png");
21.
ip.addImage(picturePath+"apple.png");
22.
ip.addImage(picturePath+"grapes.png");
23. }
24. public static void main(String[] args)
25. {
26.
ImagePanelFrame ipf = new ImagePanelFrame();
27.
ipf.showIt("Loading Images");
28. }
29.
30.
31. }
Depending on the version of the SDK, the operation system and the hardware,
one might observe slightly different behaviour. It might happen that only the yellow
background of the panel is visible or only some of the images are displayed. The
reason for this is the asynchronous loading. Let us look at three lines of code used
in the addImage method of ImagePanel :
Image im = Toolkit.getDefaultToolkit().getImage(filename);
images.add(im);
repaint();
They suggest that the image stored in filename is first loaded, then added to
the vector images and finally a re-drawing is initiated. As the image is in the vector
it should be displayed. The real time line is, however, different. The first command
initiates the loading process, which then runs in parallel to the main application.
This means that the two other statements of the above code fragment might be
executed while the image is being loaded. Thus the image added to the vector
might be incomplete or even empty and repaint would display it only partly or
not at all. If one resizes the frame later, when all images are loaded, the automatic
repainting will make them appear. The time needed to load a picture depends on
Search WWH ::




Custom Search