package class10_2_VirtualProxy; import javax.imageio.ImageIO; import javax.swing.Icon; import javax.swing.ImageIcon; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Image; import java.io.File; import java.io.IOException; import java.net.URL; // This class implements a virtual proxy, which temporarily substitutes // a "cheap" resource in place of one that takes longer to create public class ImageProxy implements Icon { // TODO: Coding private Icon realImage; private Image tempImage; // the cheap resource private URL imageURL; // the location of the real resource private boolean isRetrieving = false; // indicates whether the thread is getting the real resource // constructor // @throws IOException if an error occurs public ImageProxy(URL url) throws IOException { this.imageURL = url; // TODO: Stuff tempImage = ImageIO.read(new File("se2811/class10_2_VirtualProxy/happy.jpg"));// load the temporary resource } @Override // required accessor public int getIconHeight() { int height = tempImage.getHeight(null); if(realImage!=null) { height = realImage.getIconHeight(); } return height; } @Override // required accessor public int getIconWidth() { int width = tempImage.getWidth(null); if(realImage!=null) { width = realImage.getIconWidth(); } return width; } @Override // Called by the Swing framework whenever this icon needs to paint itself public void paintIcon(final Component c, Graphics g, int x, int y) { if(realImage!=null) { // we already have the real image loaded realImage.paintIcon(c, g, x, y); } else { // the real image has not been loaded yet // display the temporary image for now... g.drawImage(tempImage, 0, 0, tempImage.getWidth(null), tempImage.getHeight(null), null); g.setColor(Color.BLACK); g.drawString("Loading image, please wait...", x+50, y+50); System.out.println("DEBUG: Loading image...."); //...and load the real image // TODO: How to not load the image every time we call this? Thread thread = new Thread(new Runnable() { @Override public void run() { System.out.println("Debug: starting to load remote image"); try { Thread.sleep(3000); // simulate a long load time } catch (InterruptedException e) { e.printStackTrace(); // TODO: Something more meaningful } // the following may take some time to execute // if the imageURL is on a slow remote server realImage = new ImageIcon(imageURL); // TODO: Why doesn't it paint? realImage.paintIcon(c, g, x, y); } }); thread.start(); } } } //private ImageIcon image; //private URL imageURL; //private Thread retrievalThread; //private boolean retrieving = false; // //public ImageProxy(URL url) { // imageURL = url; //} // //@Override //public int getIconHeight() { // int height = ImageComponent.MAX_HEIGHT; // if(image!=null) { // height = image.getIconHeight(); // } // return height; //} // //@Override //public int getIconWidth() { // int width = ImageComponent.MAX_WIDTH; // if(image!=null) { // width = image.getIconWidth(); // } // return width; //} // //@Override //public void paintIcon(final Component component, Graphics graphics, int x, int y) { // if(image!=null) { // image.paintIcon(component, graphics, x, y); // } else { // graphics.drawString("Loading image, please wait...", x+50, y+100); // if(!retrieving) { // retrieving = true; // retrievalThread = new Thread(new Runnable() { // public void run() { // try { // Thread.sleep(200); // image = new ImageIcon(imageURL, "Image"); // component.repaint(); // } catch(Exception e) { // e.printStackTrace(); // } // } // }); // retrievalThread.start(); // } // } //}