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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Design Pattern » Proxy Pattern 




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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.