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; /********************************************************************* *
* This servlet generates a random 24-bit color 16-by-16 pixel GIF.
* It is useful to dynamically generate web page backgrounds.
*
* This class imports my version (v0.91) of GIFEncoder, a slightly * modified derivative from the original source code (v0.9) by * Adam Doppelt. *
* 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. *
* 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. *
* CopyLeft 1997 David Wallace Croft *
* @author * David W. Croft * @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 ) { } } ////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////// }