差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| study:java:design_pattern:proxy [2008/10/13 14:55] – banana | study:java:design_pattern:proxy [2021/05/12 00:27] (現在) – [ImageProxy Class] banana | ||
|---|---|---|---|
| 行 2: | 行 2: | ||
| 원격 프록시(Remote Proxy)는 일반적인 프록시 패턴(Proxy Pattern)을 구현하는 방법 가운데 하나입니다. | 원격 프록시(Remote Proxy)는 일반적인 프록시 패턴(Proxy Pattern)을 구현하는 방법 가운데 하나입니다. | ||
| 이 외에도 몇 가지 변형된 방법이 있는데, 잠시 후에 알아보도록 하겠습니다. 일단 지금은 일반 프록시 패턴 | 이 외에도 몇 가지 변형된 방법이 있는데, 잠시 후에 알아보도록 하겠습니다. 일단 지금은 일반 프록시 패턴 | ||
| - | 에 대해서 살표보도록 하겠습니다. | + | 에 대해서 살펴보도록 하겠습니다. |
| 프록시 패턴은 다음과 같이 정의됩니다. | 프록시 패턴은 다음과 같이 정의됩니다. | ||
| 行 86: | 行 86: | ||
| - **이미지를 가져오는 동안에는 " | - **이미지를 가져오는 동안에는 " | ||
| - **이미지 로딩이 끝나면 paintIcon(), | - **이미지 로딩이 끝나면 paintIcon(), | ||
| - | - **사용자가 새로운 이미지를 요청하면 프록시를 새로 만들고 위이 과자을 새로 진행합니다.** | + | - **사용자가 새로운 이미지를 요청하면 프록시를 새로 만들고 위의 과정을 새로 진행합니다.** |
| - | ===== snippet of ImageProxy Class ===== | + | |
| + | |||
| + | ===== ImageProxy Class ===== | ||
| + | <code java> | ||
| + | /** | ||
| + | * 네트워크를 통해서 이미지를 가져오는 동안 대신 실제객체를 대신하는 Proxy클래스입니다.< | ||
| + | * imageIcon의 인스턴스가 만들어진 후에 화면을 표시하게 되면 paintIcon()메소드가 호출되면서 로딩중이라는 | ||
| + | * 메시지가 아닌 실제 이미지가 화면에 표시됩니다. | ||
| + | * | ||
| + | * @author Administrator | ||
| + | * | ||
| + | */ | ||
| + | public class ImageProxy implements Icon { | ||
| + | ImageIcon imageIcon; | ||
| + | URL imageURL; | ||
| + | Thread retrievalThread; | ||
| + | boolean retrieving = false; | ||
| + | |||
| + | |||
| + | public ImageProxy(URL imageURL) { | ||
| + | super(); | ||
| + | this.imageURL = imageURL; | ||
| + | } | ||
| + | |||
| + | /* (non-Javadoc) | ||
| + | * @see javax.swing.Icon# | ||
| + | */ | ||
| + | public int getIconHeight() { | ||
| + | if (imageIcon != null) return imageIcon.getIconHeight(); | ||
| + | else return 600; | ||
| + | } | ||
| + | |||
| + | /* (non-Javadoc) | ||
| + | * @see javax.swing.Icon# | ||
| + | */ | ||
| + | public int getIconWidth() { | ||
| + | if (imageIcon != null) return imageIcon.getIconWidth(); | ||
| + | else return 800; | ||
| + | } | ||
| + | |||
| + | /* (non-Javadoc) | ||
| + | * @see javax.swing.Icon# | ||
| + | */ | ||
| + | public void paintIcon(final Component c, Graphics g, int x, int y) { | ||
| + | if (imageIcon != null) { | ||
| + | // | ||
| + | imageIcon.paintIcon(c, | ||
| + | } else { | ||
| + | g.drawString(" | ||
| + | // | ||
| + | if (!retrieving) { | ||
| + | // | ||
| + | //할 수 있습니다. | ||
| + | retrieving = true; | ||
| + | |||
| + | // | ||
| + | retrievalThread = new Thread(new Runnable(){ | ||
| + | public void run(){ | ||
| + | try { | ||
| + | // | ||
| + | imageIcon = new ImageIcon(imageURL, | ||
| + | // | ||
| + | c.repaint(); | ||
| + | } catch (Exception e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | }); | ||
| + | |||
| + | retrievalThread.start(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== ImageComponent Class ===== | ||
| + | <code java> | ||
| + | /** | ||
| + | * @author Administrator | ||
| + | * | ||
| + | */ | ||
| + | public class ImageComponent extends JComponent { | ||
| + | private Icon icon; | ||
| + | |||
| + | public ImageComponent(Icon icon) { | ||
| + | super(); | ||
| + | this.icon = icon; | ||
| + | } | ||
| + | |||
| + | public void setIcon(Icon icon) { | ||
| + | this.icon = icon; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | protected 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, | ||
| + | } | ||
| + | |||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Test of CD Cover Viewer ===== | ||
| + | 드디어 새로 배운 가상 프록시를 테스트해 볼 때가 되었습니다. 윈도우를 만들고 프레임을 준비하고 메뉴를 설치하고 | ||
| + | 프록시를 생성해주는 %%ImageProxyTestDrive%%클래스를 미리 준비해 뒀습니다. 테스트용 클래스 코드는 다음과 | ||
| + | 같습니다. | ||
| + | |||
| + | <code java> | ||
| + | public class ImageProxyTestDrive { | ||
| + | ImageComponent imageComponent; | ||
| + | JFrame frame = new JFrame(" | ||
| + | JMenuBar menuBar; | ||
| + | JMenu menu; | ||
| + | Hashtable cds = new Hashtable(); | ||
| + | /** | ||
| + | * @param args | ||
| + | */ | ||
| + | public static void main(String[] args) throws Exception{ | ||
| + | ImageProxyTestDrive testDrive = new ImageProxyTestDrive(); | ||
| + | |||
| + | } | ||
| + | |||
| + | public ImageProxyTestDrive() throws Exception{ | ||
| + | cds.put(" | ||
| + | cds.put(" | ||
| + | cds.put(" | ||
| + | cds.put(" | ||
| + | cds.put(" | ||
| + | cds.put(" | ||
| + | cds.put(" | ||
| + | cds.put(" | ||
| + | |||
| + | URL initialURL = new URL((String)cds.get(" | ||
| + | menuBar = new JMenuBar(); | ||
| + | menu = new JMenu(" | ||
| + | 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, | ||
| + | frame.setVisible(true); | ||
| + | } | ||
| + | |||
| + | URL getCDUrl(String name){ | ||
| + | try { | ||
| + | return new URL((String)cds.get(name)); | ||
| + | } catch (MalformedURLException e) { | ||
| + | e.printStackTrace(); | ||
| + | return null; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | **테스트 해 볼 것...** | ||
| + | - **메뉴를 이용하여 다른 CD 커버를 불러옵니다. 이미지 로딩이 완료될 때까지 프록시에서 로딩중이라는 메세지를 보여주는 것을 확인해 봐야 되겠죠? | ||
| + | - **로딩중이라는 메시지가 화면에 표시된 상태에서 윈도우 크기를 조절해 봅시다. 프록시에서 이미지를 로딩하고 있을 때도 스윙 윈도우가 멈추거나 하진 않는지 확인해 봅시다.** | ||
| + | - **%%ImageProxyTestDrive%%에 여러분이 가지고 있는 CD를 추가해 봅시다.** | ||
| + | 다음은 로딩중일 때의 화면입니다. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | 로딩이 완료되면 이런 윈도우가 만들어집니다. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ===== reference ===== | ||
| + | *[[http:// | ||
| + | ]] | ||