package e4s.tutorial; import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageWriter; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import e4s.html.E4CgiParams; import e4s.html.E4DocumentBase; import e4s.html.E4Method; import e4s.html.E4MethodSilent; import e4s.html.HTML; import e4s.servlet.E4ModuleImplementation; import e4s.servlet.E4ServletImplementation_Servlet; /** * Generate graphic's on the fly by your servlet. * * This example lines out how images can be produces by a servlet and how these images can be integrated dynamically * into your HTML output. It is based on HttpServletResponse. There is another example showing this together with * an Ajax refresh mechanism in the tutorial. * * {@tutorial Example_Grafic_Generation} * */ public class Example_Grafic_Generation extends E4ModuleImplementation { public static E4Method start = null; public static E4MethodSilent cross = null; public static E4MethodSilent circle = null; public void start( HTML html ) { html.IMG(cross.constructUrl(getServlet()),E4DocumentBase.EMPTY); html.IMG(circle.constructUrl(getServlet()),E4DocumentBase.EMPTY); } public void cross( HTML html, E4CgiParams params ) { // try // { // BufferedImage bufferedImage = new BufferedImage(50,50,BufferedImage.TYPE_BYTE_INDEXED); // // Graphics g = bufferedImage.getGraphics(); // g.setColor(Color.white); // g.fillRect(0,0,50,50); // g.setColor(Color.blue); // for( int i = -3; i <= +3; i++ ) // { // g.drawLine(i,0,50 + i,50); // g.drawLine(i,50,50 + i,0); // } // // g.dispose(); // // E4ServletImplementation_Servlet servlet = (E4ServletImplementation_Servlet)getServlet(); // servlet.writePng(bufferedImage); // } // catch( IOException ioe ) // { // TRACE(ioe); // html.SystemError(ioe); // } } public void circle( HTML html, E4CgiParams params ) { circle2(html,params); circle1(html,params); } public void circle1( HTML html, E4CgiParams params ) { try { BufferedImage bufferedImage = new BufferedImage(50,50,BufferedImage.TYPE_INT_RGB); Graphics g = bufferedImage.getGraphics(); g.setColor(Color.white); g.fillRect(0,0,50,50); g.setColor(Color.red); g.fillOval(0,0,50,50); g.setColor(Color.white); g.fillOval(5,5,40,40); g.dispose(); File f = new File("e:/temp"); ImageIO.setCacheDirectory(f); E4ServletImplementation_Servlet servlet = (E4ServletImplementation_Servlet)getServlet(); HttpServletResponse response = servlet.getResponse(); TRACE(ImageIO.getCacheDirectory()); TRACE(ImageIO.getUseCache()); x("jpg"); x("png"); x("jpeg"); x("gif"); ServletOutputStream stream = servlet.getOutputStream(); TRACE(stream); //ImageIO.write(bufferedImage,"jpg",stream); X_ImageIO.write(bufferedImage,"jpg",stream); response.setContentType("image/jpg"); } catch( IOException ioe ) { TRACE(ioe); html.SystemError(ioe); } } public void circle2( HTML html, E4CgiParams params ) { try { BufferedImage bufferedImage = new BufferedImage(50,50,BufferedImage.TYPE_INT_RGB); Graphics g = bufferedImage.getGraphics(); g.setColor(Color.white); g.fillRect(0,0,50,50); g.setColor(Color.red); g.fillOval(0,0,50,50); g.setColor(Color.white); g.fillOval(5,5,40,40); g.dispose(); FileOutputStream fstream = new FileOutputStream("e:/temp/circle.jpg"); X_ImageIO.write(bufferedImage,"jpg",fstream); TRACE("*** done ***"); fstream.close(); } catch( IOException ioe ) { TRACE(ioe); html.SystemError(ioe); } } private void x(String string) { TRACE(string); try { Iterator it = ImageIO.getImageWritersByFormatName(string); if (it != null) { while(it.hasNext()) { ImageWriter x = it.next(); if (x == null) break; TRACE(x); } } } catch(Throwable t) { TRACE(t); } } }