// VirtualProxyTest.java
import java.lang.reflect.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// 该类测试一个代理。该代理将图形的显示延迟到第一次刷新图片的时候
public class VirtualProxyTest extends JFrame {
   private static String IMAGE_NAME = "hands.jpg";
   private static final int IMAGE_WIDTH = 256, IMAGE_HEIGHT = 256,
                                SPACING = 5,        FRAME_X = 150,
                                FRAME_Y = 200, FRAME_WIDTH = 530, 
                           FRAME_HEIGHT = 286;
   private ImageIcon imageIcon = null;
   private Icon imageIconProxy = null;
   static public void main(String args[]) {
      VirtualProxyTest app = new VirtualProxyTest();
      app.show();
   }
   public VirtualProxyTest() {
      super("ImageIcon测试");
      // 创建一个ImageIcon对象的和ImageIcon代理
      imageIcon = new ImageIcon(IMAGE_NAME);
      imageIconProxy = (Icon)Proxy.newProxyInstance(
                        imageIcon.getClass().getClassLoader(),
                        imageIcon.getClass().getInterfaces(),
                        new ImageIconInvocationHandler(IMAGE_NAME,
                                    IMAGE_WIDTH, IMAGE_HEIGHT));
      // 设定窗口大小和关闭窗口的操作
      // close operation.
      setBounds(FRAME_X, FRAME_Y, FRAME_WIDTH, FRAME_HEIGHT);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   public void paint(Graphics g) {
      super.paint(g);
      Insets insets = getInsets();
      imageIcon.paintIcon(this, g, insets.left, insets.top);
      imageIconProxy.paintIcon(this, g, 
               insets.left + IMAGE_WIDTH + SPACING, // 宽
               insets.top); // 高
   }
}
// ImageIconInvocationHandler.java
import java.lang.reflect.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// ImageIconInvocationHandler类是一个ImageIcon的代理。它将图形的
// 显示延迟到图形第一次被绘制的时候。当图形还没有被绘制以前, 
// 代理在界面上显示"加载图片…"的信息
class ImageIconInvocationHandler implements InvocationHandler {
   private static final String PAINT_ICON_METHOD="paintIcon",
                               GET_WIDTH_METHOD ="getIconWidth",
                               GET_HEIGHT_METHOD="getIconHeight";
   private ImageIcon realIcon = null;
   private boolean isIconCreated = false;
   private String   imageName = null;
   private int width, height;
   public ImageIconInvocationHandler(String imageName,
                                      int width, int height) {
      this.width = width;
      this.height = height;
      this.imageName = imageName;
   }
   public Object invoke(Object proxy, Method m, Object[] args) {
      Object result = null;
      if(PAINT_ICON_METHOD.equals(m.getName())) {
         final Component c = (Component)args[0];
         final Graphics g = (Graphics)args[1];
         final int x = ((Integer)args[2]).intValue(),
                   y = ((Integer)args[3]).intValue();
         if(isIconCreated) {
              realIcon.paintIcon(c, g, x, y);
         }
         else {
              g.drawRect(x, y, width-1, height-1);
            g.drawString("Loading image...", x+20, y+20);
            // 在单独的线程中创建图形对象
            SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                  try {
                     Thread.currentThread().sleep(2000);
                     realIcon = new ImageIcon(imageName);
                     isIconCreated = true;
                  }
                  catch(InterruptedException ex) {
                     ex.printStackTrace();
                  }
                  c.repaint();
               }
            });
         }
      }
      else if(GET_WIDTH_METHOD.equals(m.getName())) {
         return isIconCreated ?  new Integer(height) : 
                new Integer(realIcon.getIconHeight()); 
      }
      else if(GET_HEIGHT_METHOD.equals(m.getName())) {
         return isIconCreated ?  new Integer(width) : 
                new Integer(realIcon.getIconWidth()); 
      }
      return null;
   }
}
  |