public class ImageProxy implements Icon
1: import java.awt.*;
2: import javax.swing.*;
4: /**
5: A proxy for delayed loading of image icons.
6: */
7: public class ImageProxy implements Icon
8: {
9: /**
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: }
19: public void paintIcon(Component c, Graphics g, int x, int y)
20: {
21: ensureImageLoaded();
22: image.paintIcon(c, g, x, y);
23: }
25: public int getIconWidth()
26: {
27: ensureImageLoaded();
28: return image.getIconWidth();
29: }
31: public int getIconHeight()
32: {
33: ensureImageLoaded();
34: return image.getIconHeight();
35: }
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: }
50: private String name;
51: private ImageIcon image;
52: }