隨筆 - 117  文章 - 72  trackbacks - 0

          聲明:原創作品(標有[原]字樣)轉載時請注明出處,謝謝。

          常用鏈接

          常用設置
          常用軟件
          常用命令
           

          訂閱

          訂閱

          留言簿(7)

          隨筆分類(130)

          隨筆檔案(123)

          搜索

          •  

          積分與排名

          • 積分 - 156280
          • 排名 - 390

          最新評論

          [關鍵字]:java,design pattern,設計模式,《Java與模式》學習,Proxy Pattern,代理模式
          [環境]:StarUML5.0 + JDK6
          [作者]:Winty (wintys@gmail.com) http://wintys.blogjava.net
          [正文]:

          虛擬代理:

          package pattern.proxy.virtual;

          import java.awt.Graphics;
          import java.awt.Component;
          import java.awt.Insets;
          import java.awt.Container;
          import javax.swing.JPanel;
          import javax.swing.JOptionPane;
          import javax.swing.ImageIcon;
          import javax.swing.SwingUtilities;
          import javax.swing.JFrame;
          import javax.swing.Icon;
          /**
           * 虛擬代理
           *
           * 使用代理加載圖片
           *
           * @version 2009-6-29
           * @author Winty(wintys@gmail.com) http://wintys.blogjava.net
           */
          public class VirtualProxyTest extends JFrame{
              private final int WIDTH = 500;
              private final int HEIGHT = 500;
              

              public VirtualProxyTest(){
                  super("虛擬代理");
                  
                  setContentPane(new MyPanel());
                  setSize(WIDTH , HEIGHT);
                  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  setVisible(true);
              }

              public static void main(String[] args){
                  VirtualProxyTest test = new VirtualProxyTest();
              }
          }

          class MyPanel extends JPanel{
              private IconProxy iconProxy;

              public MyPanel(){
                  iconProxy = new IconProxy("sample.jpg" , 200 , 20);
              }

              @Override
              public void paintComponent(Graphics g){
                  super.paintComponent(g);
                  
                  iconProxy.paintIcon(this , g , 0 , 0);
              }
          }

          class IconProxy implements Icon{
              private Icon icon;
              private String fileName;//Icon的文件名
              private boolean done;//Icon是否加載完成
              private final int STR_X;
              private final int STR_Y;
              
              /**
               * @param fileName 圖片文件
               * @param str_x 字符串輸出的x位置
               * @param str_y 字符串輸出的y位置
               */
              public IconProxy(String fileName , int str_x , int str_y){
                  icon = null;
                  this.fileName = fileName;
                  done = false;
                  STR_X = str_x;
                  STR_Y = str_y;
                  
              }

              @Override
              public int getIconHeight(){
                  return icon.getIconHeight();
              }

              @Override
              public int getIconWidth(){
                  return icon.getIconWidth();
              }

              @Override
              public void paintIcon(final Component c, Graphics g, int x, int y){
                  //因為JFrame的標題欄和邊框占據了空間,
                  //而paintIcon是從容器的(0,0)坐標開始繪制,所以要計算邊框所占的空間。
                  Insets inset = new Insets(0,0,0,0);
                  if(c instanceof Container)
                      inset = ((Container)c).getInsets();

                  if(!done){//未加載完成
                      g.drawString("Loading icon...",
                                          inset.right + STR_X ,
                                          inset.top + STR_Y);
                  
                      synchronized(this){
                          SwingUtilities.invokeLater(
                              new Runnable(){
                                  public void run(){
                                      //延時
                                      try{
                                          Thread.sleep(5*1000);
                                      }catch(InterruptedException e){
                                          JOptionPane.showMessageDialog(c , e.getMessage());
                                      }
                                      
                                      icon = new ImageIcon(fileName);
                                      done = true;
                                      c.repaint();
                                  }
                              }
                          );
                      }//end of synchronized
                  }
                  else{//加載完成
                      
                      g.drawString("Loaded successfully.",
                                          inset.right + STR_X ,
                                          inset.top + STR_Y);
                      //空出長度為STR_Y的空間給String
                      icon.paintIcon(c , g , x + inset.right , y + inset.top + STR_Y + 5);
                  }
              }
          }

          運行結果:
          Loading icon...


          Loaded successfully:

          posted on 2009-06-29 22:41 天堂露珠 閱讀(1299) 評論(0)  編輯  收藏 所屬分類: Pattern
          主站蜘蛛池模板: 永仁县| 灌南县| 枞阳县| 旬阳县| 峨眉山市| 余庆县| 临清市| 保定市| 宿松县| 兖州市| 大足县| 龙里县| 巴楚县| 喀喇沁旗| 苍山县| 柘荣县| 龙州县| 肥西县| 依安县| 抚远县| 宝坻区| 太白县| 平顶山市| 郓城县| 泉州市| 肃南| 旌德县| 盐山县| 名山县| 昔阳县| 县级市| 岢岚县| 南丰县| 宝兴县| 拜城县| 土默特右旗| 仲巴县| 潼关县| 美姑县| 普兰县| 娱乐|