日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

代理模式(Proxy Pattern)(四):HeadFirst中CD封面虚拟代理

發布時間:2023/12/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 代理模式(Proxy Pattern)(四):HeadFirst中CD封面虚拟代理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、問題描述


當我們在網絡上加載一些圖像時,限于連接寬帶和網絡負載,下載可能需要一些時間,所以在等待圖像加載的時候,就應該顯示一些東西。我們也不希望在等待圖像時,整個應用程序被掛起。一旦圖像加載完成,剛才顯示的東西就應該消失,圖像顯示出來。


二、類圖





三、實現代碼


1.ImageComponent

class ImageComponent extends JComponent {private Icon icon;public ImageComponent(Icon icon) {this.icon = icon;}public void setIcon(Icon icon) {this.icon = icon;}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);} }
2.代理類ImageProxy

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+190);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();}}} }
3.測試

public class ImageProxyTestDrive {ImageComponent imageComponent;JFrame frame = new JFrame("CD Cover Viewer");JMenuBar menuBar;JMenu menu;Hashtable cds = new Hashtable();public static void main(String[] args) throws Exception{ImageProxyTestDrive testDrive = new ImageProxyTestDrive();}public ImageProxyTestDrive() throws Exception{cds.put("Ambient: Music for Airports","http://images.amazon.com/images/P/B000003S2K.01.LZZZZZZZ.jpg");cds.put("Buddha Bar","http://images.amazon.com/images/P/B00009XBYK.01.LZZZZZZZ.jpg");cds.put("Ima","http://images.amazon.com/images/P/B000005IRM.01.LZZZZZZZ.jpg");cds.put("Karma","http://images.amazon.com/images/P/B000005DCB.01.LZZZZZZZ.gif");cds.put("MCMXC A.D.","http://images.amazon.com/images/P/B000002URV.01.LZZZZZZZ.jpg");cds.put("Northern Exposure","http://images.amazon.com/images/P/B000003SFN.01.LZZZZZZZ.jpg");cds.put("Selected Ambient Works, Vol. 2","http://images.amazon.com/images/P/B000002MNZ.01.LZZZZZZZ.jpg");URL initialURL = new URL((String) cds.get("Selected Ambient Works, Vol. 2"));menuBar = new JMenuBar();menu = new JMenu("Favorite CDs");menuBar.add(menu);frame.setJMenuBar(menuBar);for (Enumeration 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();}});}//建立框架和菜單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;}} }
運行窗口:

(加載圖片時顯示"Loading CD cover, please wait...",反應太快,就不截圖了)



轉載請注明出處:http://blog.csdn.net/jialinqiang/article/details/8974694

總結

以上是生活随笔為你收集整理的代理模式(Proxy Pattern)(四):HeadFirst中CD封面虚拟代理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。