01: import java.awt.*;
02: import javax.swing.*;
03: 
04: /**
05:    A proxy for delayed loading of image icons.
06: */
07: public class ImageProxy implements Icon
08: {
09:    /**
10:       Constructs a proxy for delayed loading of an image file.
11:       @param name the file name
12:    */
13:    public ImageProxy(String name)
14:    {
15:       this.name = name;
16:       image = null;
17:    }
18: 
19:    public void paintIcon(Component c, Graphics g, int x, int y)
20:    {
21:       ensureImageLoaded();
22:       image.paintIcon(c, g, x, y);
23:    }
24: 
25:    public int getIconWidth()
26:    {
27:       ensureImageLoaded();
28:       return image.getIconWidth();
29:    }
30: 
31:    public int getIconHeight()
32:    {
33:       ensureImageLoaded();
34:       return image.getIconHeight();
35:    }
36: 
37:    /**
38:       Loads the image if it hasn't been loaded yet. Prints
39:       a message when the image is loaded.
40:    */
41:    private void ensureImageLoaded()
42:    {
43:       if (image == null)
44:       {
45:          System.out.println("Loading " + name);
46:          image = new ImageIcon(name);
47:       }
48:    }
49: 
50:    private String name;
51:    private ImageIcon image;
52: }