注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 Exchange服务器系列课程之..
 帮助

代理模式


2008-03-17 23:02:49
 标签:代理 模式    [推送到技术圈]

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://sophi.blog.51cto.com/340461/66350
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
* 代理模式
* 为另一个对象提供一个替身或占为符以访问这个对象.
*/

public class ProxyPattern {

    public static void main(String[] args) {
  try {
      ImageProxyTestDrive imageProxyTestDrive = new ImageProxyTestDrive();
  } catch (Exception e) {
      e.printStackTrace();
  }
    }

}
//Icon是javax.swing的一个接口类,ImageIcon也是javax.swing中实现Icon的类
//ImageProxy代理实现Icon接口类的方法.
class ImageProxy implements Icon{
    ImageIcon imageIcon;
    URL imageURL;
    Thread retrievalThread;
    boolean retrieving = false;
  
    public ImageProxy(URL url) {
  imageURL = url;
    }
  
    public int getIconWidth() {
  if (imageIcon != null) {
      return imageIcon.getIconWidth();
  } else {
      return 800;
  }
    }
  
    public int getIconHeight() {
  if (imageIcon != null) {
      return imageIcon.getIconHeight();  
  } else {
      return 600;
  }
    }
  
    public void paintIcon(final Component c,Graphics g,int x,int y) {
  if (imageIcon != null) {
      imageIcon.paintIcon(c, g, x, y);
  } else {
      g.drawString("Loading CD cover,please wait...", x+300, y+300);
      if (!retrieving) {
    retrieving = true;
    
    retrievalThread = new Thread(new Runnable(){
        public void run() {
      try{
          imageIcon = new ImageIcon(imageURL,"CD Cover");
          c.repaint();
      } catch(Exception e){
          e.printStackTrace();
      }
        }
    });
    retrievalThread.start();
      }
  }
    }
}

//存放Icon的容器
class ImageComponent extends JComponent{
    private Icon icon;
  
    public ImageComponent(Icon icon) {
  this.icon = icon;
    }
  
    public void setIcon(Icon iocn) {
  this.icon = iocn;
    }
  
    public void paintComponent(Graphics g) {
  super.paintComponent(g);
  int w = icon.getIconWidth();
  int h = icon.getIconHeight();
  int x = (800 - w)/2;
  int y = (600 - h)/2;
  icon.paintIcon(this, g, x, y);
    }
}

//窗口类
class ImageProxyTestDrive{
    ImageComponent imageComponent;
    JFrame frame = new JFrame("CD Cover Viewer");
    JMenuBar menuBar;
    JMenu menu;
    Hashtable<String, String> cds = new Hashtable<String, String>();
  
    public ImageProxyTestDrive() throws Exception {
  //有需要的话可以添加更多的选项
  cds.put("1","http://images.amazon.com/images/P/B000003SFN.01.LZZZZZZZ.jpg");
  
  URL initialURL = new URL((String)cds.get("1"));
  menuBar = new JMenuBar();
  menu = new JMenu("Favorite CDs");
  menuBar.add(menu);
  frame.setJMenuBar(menuBar);
  
  for (Enumeration<String> e = cds.keys();e.hasMoreElements();) {
      String name = (String)e.nextElement();
      JMenuItem menuItem = new JMenuItem(name);
      menu.add(menuItem);
      menuItem.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event) {
        imageComponent.setIcon(new ImageProxy(getCDUrl(event.getActionCommand())));
        frame.repaint();
    }
      });
  }
  
  //从这里可以看出ImageProxy是如何代理生成Icon的
  Icon icon = new ImageProxy(initialURL);
  
  imageComponent = new ImageComponent(icon);
  frame.getContentPane().add(imageComponent);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(800,600);
  frame.setVisible(true);
    }
  
    URL getCDUrl(String name){
  try {
      return new URL((String)cds.get(name));
  } catch (MalformedURLException e) {
      e.printStackTrace();
      return null;
  }
    }
}

本文出自 ““技”忆” 博客,请务必保留此出处http://sophi.blog.51cto.com/340461/66350



上一篇 迭代模式  下一篇 MVC模式



    文章评论
 
 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: