     package org.anser.proj.timc.prog.croft.app.randomgif;

     import java.awt.AWTException;
     import java.io.IOException;
     import javax.servlet.*;
     import javax.servlet.http.*;
     import org.anser.proj.timc.prog.croft.graphics.GIFEncoder;

     /*********************************************************************
     * <P>
     * This servlet generates a random 24-bit color 16-by-16 pixel GIF.<BR>
     * It is useful to dynamically generate web page backgrounds.<BR>
     * <P>
     * This class imports my version (v0.91) of GIFEncoder, a slightly
     * modified derivative from the original source code (v0.9) by
     * <A HREF="http://www.cs.brown.edu/people/amd/">Adam Doppelt</A>.
     * <P>
     * Note that GIFEncoder produces GIFs that can have up to a maximum
     * of 256 different colors.  By keeping the randomized GIFs to
     * 16x16 = 256 pixels, we stay within that bound no matter what colors
     * are randomly generated.
     * <P>
     * An interesting future extension of this concept could be to "learn"
     * an optimal web page background coloring pattern using a genetic
     * algorithm.  By slightly mutating the color pattern of the GIFs and
     * killing off those patterns which tend to garner less click-throughs
     * on the web page (less attractive), one might be able to evolve over
     * time an "optimal" background pattern given the preferences of that
     * web page's consumer base.
     * <P>
     * CopyLeft 1997 David Wallace Croft
     * <P>
     * @author
     *   <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A>
     * @version
     *   1.0 1997-09-29
     *********************************************************************/

     public class  RandomGIFServlet extends HttpServlet {
     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     /*********************************************************************
     * Services a single request from a client.
     *********************************************************************/
     public void  doGet (
       HttpServletRequest   req,
       HttpServletResponse  res )
       throws ServletException, IOException {
     //////////////////////////////////////////////////////////////////////
       short  w = 16;
       short  h = 16;
       int [ ]  values = new int [ w * h ];
       int  i = 0;
       for ( short  x = 0; x < w; x++ ) {
         for ( short  y = 0; y < h; y++ ) {
           values [ i++ ] = ( int ) ( Math.random ( ) * 0xFFFFFF );
         }
       }
       try {
         GIFEncoder  gifEncoder = new GIFEncoder ( w, h, values );
         res.setContentType ( "image/gif" );
         ServletOutputStream  out = res.getOutputStream ( );
         gifEncoder.Write ( out );
       } catch ( AWTException  e ) { }
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     }
