在前面《 代理模式Image Proxies(一、最朴素实现) 》中,代理类如下:
package com.oozinoz.imaging;
/*
* Copyright (c) 2001, 2005. Steven J. Metsker.
*
* Steve Metsker makes no representations or warranties about
* the fitness of this software for any particular purpose,
* including the implied warranty of merchantability.
*
* Please use this software as you wish with the sole
* restriction that you may not claim that you wrote it.
*/
import java.awt.*;
import javax.swing.*;
/**
* This class acts as a proxy for another ImageIcon. In "The Design Patterns
* in Java" we wind up tearing out this class, preferring the techniques
* used in the ImageIconLoader class.
* @author Steven J. Metsker
* @see LoadingImageIcon
*/
public class ImageIconProxy extends ImageIcon implements Runnable {
static final ImageIcon ABSENT = new ImageIcon(ClassLoader.getSystemResource("images/absent.jpg"));
static final ImageIcon LOADING = new ImageIcon(ClassLoader.getSystemResource("images/loading.jpg"));
ImageIcon current = ABSENT;
protected String filename;
protected JFrame callbackFrame;
/**
* Construct an ImageIconProxy that will (on demand) load the image in the
* provided file.
* @param filename the name of a file to load
*/
public ImageIconProxy(String filename) {
super(ABSENT.getImage());
this.filename = filename;
}
/**
* Load the desired image and call back the provided frame when done.
* @param JFrame the frame to repaint when the image is loaded
*/
public void load(JFrame callbackFrame) {
this.callbackFrame = callbackFrame;
current = LOADING;
callbackFrame.repaint();
new Thread(this).start();
}
/**
* Load the desired image (presumably in a separate thread).
*/
public void run() {
current = new ImageIcon(ClassLoader.getSystemResource(filename));
callbackFrame.pack();
}
/**
* @return the height of the Icon
*/
public int getIconHeight() {
return current.getIconHeight();
}
/**
* @return the width of the Icon
*/
public int getIconWidth() {
return current.getIconWidth();
}
/**
* Paint the Icon
*/
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
current.paintIcon(c, g, x, y);
System.out.println("paintIcon()");
}
}
其中,后面三个方法getIconHeight()、getIconWidth()、paintIcon(...)都是在覆盖父类ImageIcon的方法,下面的改进主要目的在于:
“不把绘图请求转发给父类ImageIcon对象,而是直接操作ImageIcon中的Image对象,更加简便”。(注意到一个ImageIcon对象内部封装了一个Image对象,在ImageIcon构造函数就可以看出,另外ImageIcon类还有setImage()方法)
改进后有两个类:ShowLoader和LoadingImageIcon,其中后者是代理。当前者在内部调用loader.load(frame);时,后者会更换自己内部的图片,重新显示窗口。具体代码见附件。