Design Patterns Java™ Workbook

(Michael S) #1
Chapter 11. Proxy

Figure 11.1. Three screen shots show a mini-application before, during, and after loading
a large image. (Photo courtesy of Corbis, Inc.)

The user interface displays one of three images: one indicating that loading has not begun,
one indicating that the real image is loading, or the real image. When the application starts, it
shows Absent, a JPEG image that you have built in an image-processing tool. When the user
clicks Load, the image changes almost instantly to another prebuilt image, Loading.... After
a few moments, the desired image appears.


An easy way to display a image saved in, say, a JPEG file is to use an ImageIcon object as
an argument to a "label" that will show the image:


ImageIcon i = new ImageIcon("Fest.jpg");
JLabel l = new JLabel(i);


In the application that you are building, you want to pass into JLabel a proxy that will
forward painting requests to Absent, Loading..., or the desired image. The message flow
might look like the sequence diagram in Figure 11.2.

Free download pdf