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
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
相关文章推荐
- 企业内维护windows使用Ghost
- TextInputLayout 的使用以及style 样式简化代码的编写和后续的修改与维护
- 使用ind间接性文件运行维护任务
- Windows+OpenLDAP+MySQL配置及使用详解(三)——LDAP数据的维护
- QTP测试脚本的维护 - 使用Update运行模式和Maintenance运行模式
- gitlab基本维护和使用
- Redrain个人维护并使用的DuiLib和UiLib库源码下载地址
- 微软私有云分享(R2)27维护窗口的使用
- 【视频】配置信息管理 的 使用方法(四):功能节点维护
- 第十章——维护索引(7)——使用索引视图提高性能
- 中索引使用及维护
- 使用Git维护你的网站
- 关于SQL Server中索引使用及维护简介
- 使用脚本对数据库进行备份维护
- CMOS使用及维护须知
- SQL Server中索引使用及维护
- (维护)使用壳牌防冻液,暖风现在必须打一档,而且不能打到底!
- 使用SQL Server维护计划实现数据库定时自动备份
- 扫描线求矩形面积并,使用线段树维护(感觉非常详细)
- Qt使用一个事件队列对所有发出的事件进行维护(QObject的event()函数相当于dispatch函数),用EventLabel 继承QLabel作为例子(简单明了) good