package com.vistech.net;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.net.*;
import java.util.*;

public class SimpleServletApplet extends Applet implements Runnable{
   static int viewerWid = 400;
   static int viewerHt = 300;
   protected String[] fileList;
   protected String curDir = ".";
   protected char separator;
   protected ImageCanvas imCanvas;
   protected Thread appletThread;
 
   public void init() {
       String param = getParameter("WIDTH");
       if(param != null) viewerWid = Integer.valueOf(param).intValue();
       param = getParameter("HEIGHT");
       if(param != null) viewerHt = Integer.valueOf(param).intValue();
       initApp();
   }

   public void initApp() {
      this.setSize(viewerWid, viewerHt);
      enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK |
                          AWTEvent.MOUSE_EVENT_MASK);
   }

   public void start(){
       createUI();
       appletThread = new Thread(this);
       appletThread.start();
   }

   public void run(){
      load();
   }
   public void createUI (){
       imCanvas = (ImageCanvas)createImageCanvas();
       setLayout(new GridLayout(1,1));
       add(imCanvas);
   }

   protected Panel createImageCanvas() {
       return new ImageCanvas();
   }

   protected void load() {
      try {
        URL servlet = new URL("http://localhost:8080/examples/servlet/SimpleImageLoaderServlet/lena.gif");
        ServletInvoker si = new ServletInvoker(servlet);
        InputStream is = si.performGet(null);
        byte[] imageBuf = new byte[1024];
        ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
        for(;;){
          int bytesread = is.read(imageBuf);
          if (bytesread <0) {break;}
          boutstream.write(imageBuf,0,bytesread);
        }
        byte[] buffer =boutstream.toByteArray();
        Image img = Toolkit.getDefaultToolkit().createImage(buffer);
        imCanvas.setImage(img);
        imCanvas.repaint();
      } catch (Exception e) {e.printStackTrace();}
   }

/*
class ServletInvoker {
   protected URL servletURL;

   public ServletInvoker(URL servlet) {
      servletURL = servlet;
   }

   public InputStream performGet(Properties params){
      try {
      StringBuffer qs= new StringBuffer();
      if(params != null) {
      for (Enumeration e= params.propertyNames(); e.hasMoreElements(); ){
           String key = (String)e.nextElement();
           String value = params.getProperty(key);
           qs.append(URLEncoder.encode(key) + "="+
                     URLEncoder.encode(value)+((e.hasMoreElements())?"&":""));
      }
      }
      URL servUrl = new URL(servletURL.toExternalForm()+qs);
      URLConnection uc = servUrl.openConnection();
      uc.setUseCaches(false);
      return uc.getInputStream();
      } catch (Exception e) { return null;}

   }

}
 */
public class ImageCanvas extends Panel{
   protected Image image;
   protected int viewerWid = 400;
   protected int viewerHt = 600;

   public void setImage(Image img){ image = img;}

   public void paint(Graphics g){
      Rectangle rect = this.getBounds();
      g.setColor(Color.black);
      if((viewerWid != rect.width) || (viewerHt != rect.height)){
          viewerWid = rect.width; viewerHt = rect.height ;
          repaint(); return;
      }
      if(image != null){
         g.drawImage(image,0,0, this);
      }
   }
}
}
