您的位置:首页 > 运维架构

DVD刻录机的使用与维护

2012-07-09 09:23 183 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: