Proxy pattern in Java : Proxy Pattern « Design Pattern « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Design Pattern » Proxy PatternScreenshots 
Proxy pattern in Java
Proxy pattern in Java

/*
The Design Patterns Java Companion

Copyright (C) 1998, by James W. Cooper

IBM Thomas J. Watson Research Center

*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class ProxyDisplay extends JFrame {
  public ProxyDisplay() {
    super("Display proxied image");
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    JPanel p = new JPanel();
    getContentPane().add(p);
    p.setLayout(new BorderLayout());
    ImageProxy image = new ImageProxy("elliott.jpg"321271);
    p.add("Center", image);
    p.add("North"new Label("    "));
    p.add("West"new Label("  "));
    setSize(370350);
    setVisible(true);
  }

  
  static public void main(String[] argv) {
    new ProxyDisplay();
  }
}
//==================================

class ImageProxy extends JPanel implements Runnable {
  int height, width;

  MediaTracker tracker;

  Image img;

  JFrame frame;

  Thread imageCheck; //to monitor loading
  

  public ImageProxy(String filename, int w, int h) {
    height = h;
    width = w;

    tracker = new MediaTracker(this);
    img = Toolkit.getDefaultToolkit().getImage(filename);
    tracker.addImage(img, 0)//watch for image loading

    imageCheck = new Thread(this);
    imageCheck.start()//start 2nd thread monitor

    //this begins actual image loading
    try {
      tracker.waitForID(01);
    catch (InterruptedException e) {
    }
  }

  
  public void paint(Graphics g) {
    if (tracker.checkID(0)) {
      height = img.getHeight(frame)//get height
      width = img.getWidth(frame)//and width

      g.setColor(Color.lightGray)//erase box
      g.fillRect(00, width, height);
      g.drawImage(img, 00this)//draw loaded image
    else {
      //draw box outlining image if not loaded yet
      g.setColor(Color.black);
      g.drawRect(11, width - 2, height - 2);
    }
  }

  
  public Dimension getPreferredSize() {
    return new Dimension(width, height);
  }

  //public int getWidth() {return width;}
  //public int getHeight(){return height;}
  
  public void run() {
    //this thread monitors image loading
    //and repaints when done
    //the 1000 msec is artifically long
    //to allow demo to display with delay
    try {
      Thread.sleep(1000);
      while (!tracker.checkID(0))
        Thread.sleep(1000);
    catch (Exception e) {
    }
    repaint();
  }
}

           
       
Related examples in the same category
1. Another Proxy Pattern
2. Proxy Pattern 2Proxy Pattern 2
w__w__w___.__j___a___v__a2__s_._co___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.