/* ******************************************************************** CJ's Servlet Graphics Package ******************************************************************** February 20, 2000. CJ's Servlet Graphics Package. This servlet was created February 20, 2000 by Erica Andrews. The servlet makes images on the fly using the Acme GifEncoder class library, lists all of the fonts available on the server, and even has a draw-mode/preview feature for creating images. CJ's Servlet Graphics Package Copyright 2000 by Erica Andrews (cylikon@hotmail.com). All rights reserved. This servlet may NOT be used for any sort of profit either directly or indirectly without the express, written consent of the author of this servlet. This servlet may NOT under any conditions, be used in any commercial web sites. This servlet is free for private, non-commercial use, but may NOT be modified in anyway. Doing so would constitute copyright infringements. In particular, the comments must remain in-tact and be left as-is. For more great Java stuff visit: ./ButchWhipAppeal ./IcedPinky/ IMPORTANT: This servlet REQUIRES the Acme GIFEncoder class libraries for creating servlet images on the fly. AWT alone will not do. It is assumed that you either already have these libraries or can find them on your own. (Do a search on Altavista for "acme AND gifencoder". It should come up.) HELP/USAGE: This servlet contains 3 useful utilities built into one class: 1. A method for creating images on the fly (can be used as an image server). 2. A Draw-mode utility for creating and preview images to be created by this servlet. 3. A system Font Listing utility, which lists all the fonts available on the server on which this servlet is running. Proper installation of the servlet is your responsibility and depends on your particular Java server. For this "help" file, we will call the servlet "graphics". ** Using the FONT LISTING Utility: If you were to name this servlet "graphics" you could get a list of all the fonts available on your server/computer by typing the following into any web browser: http://www.yourhost.com/graphics?list=yes The Font List utility also has a link back to Draw Mode, which will open in a new window. ** Using the DRAW MODE Utility: If you were to name this servlet "graphics" you could enter Draw-Mode by typing the following into any web browser: http://www.yourhost.com/graphics?drawmode=yes Draw Mode is perhaps the most useful utility. Draw Mode displays an HTML form which will allow you to create and preview image "buttons". You can adjust fonts, colors, image sizes, etc. The HTML form then places the code for that particular image in a textarea on your left-hand side. To use that image in one of your pages, just copy and paste that code wherever you need it. The people using your page will only see the image you created, not any of the other utilities, such as Draw Mode or the Font Lister. It's THAT simple. Draw Mode also has a handy link to the Font List utility, which will open in a new window, to help make image creation faster. ** Creating Images On The Fly: Simply copy and paste the tag created in the textarea of the Draw Mode utility into your HTML pages. Visit your page. You'll see that the servlet is now acting as an image server. Alternately, you can fool around with the parameters in the URL string; however, this is doing things the hard way. ** IMPORTANT TIPS: This servlet uses an init() method to load the fonts on your computer/server. So, the best thing to do is this: If your Java server allows you to load all servlets or one particular servlet when the server starts, you should make sure that THIS servlet is one of the servlets loaded at start-up. (It's your responsibility to find out how to do this on your particular Java server.) When your Java server starts, you will see it stall for a minute while it loads this servlet. This is because the java GrapicsEnvironment is slow loading the list of fonts. However, this Font list only needs to be loaded ONCE. So, it's best to load the list when the server starts, rather than having a visitor to your site have to wait while the font list loads, as this would definitely slow down image loading in their browser, making them impatient. (A visitor would only experience this stall if they are the *first* visitor to your servlet since it was loaded by your server.) If you do not load this servlet when your java server starts, your first visit to this servlet will always be very slow. The Font List loaded at startup is good for the entire life of the servlet, so if you happen to add new fonts while this servlet is loaded and running, you will NOT see them in the list until you reload this servlet. For some people, this may require a complete restart of their Java server. ** OTHER INFO: This servlet uses the SingleThreadModel, which means it is thread-safe because it does not use more than one thread at a time. This means that the servlet may be slow handling multiple simultaneous requests for images. For sites with many visitors, this could create a performance issue. However, this servlet was created for my personal use on a private server. One thread was enough for me, so that's what I created. A future release *may* support multi-threading, whenever I get around to it. ** COPYRIGHT: Free for private, non-commercial use provided NO modifications to this servlet are made and these comments are left as-is. This servlet may not be used for any sort of profit, either directly or indirectly, and may not be used in any commercial websites or intranets. Any other use of this servlet constitutes copyright infringements and fraudulent misuse of this software, punishable in a court of law. Copyright February 2000 by Li'L CJ aka Erica Andrews (cylikon@hotmail.com). All rights reserved. */ import java.io.*; import java.awt.*; import javax.servlet.*; import javax.servlet.http.*; import Acme.JPM.Encoders.GifEncoder; import java.awt.Font; import java.awt.FontMetrics; import java.lang.Integer; import java.util.Vector; public class GraphicsFactory extends HttpServlet implements SingleThreadModel { protected String allfonts[]; public Frame frame = null; public Graphics g = null; public ServletOutputStream out; protected GraphicsEnvironment GE; private String FontList; public Vector v = new Vector(); Image realimage; Image image; Graphics g2; Image ErrorImage; Graphics g3; public void init(ServletConfig config) throws ServletException { super.init(config); // --- loading the whole list of available fonts during server start-up rather than runtime GE = GraphicsEnvironment.getLocalGraphicsEnvironment(); allfonts = GE.getAvailableFontFamilyNames(); this.GE = GE; this.allfonts = allfonts; // storing the font list into a vector than a string to be returned with the "list" command FontList = " "; String newline = "
"; for (int n=0; nTotal fonts: "+allfonts.length+"    Go Into Draw-Mode

"); out.flush(); // send this portion to the client now out.print(FontList); out.flush(); // credits out.print("

"); out.print("CJ's Servlet Graphics Package
Copyright 2000 by Erica Andrews (cylikon@hotmail.com). All rights reserved. This servlet may NOT be used for any sort of profit either directly or indirectly without the express, written consent of the author of this servlet. For more cool Java stuff, visit: ./ButchWhipAppeal/ and ./IcedPinky/."); out.print("
"); } // ************************* // Beginning of Draw-Mode utility /* This is another small utility that allows the servlet user to preview images to be used in pages by adjust the colors, font, font size, and image size through forms. The utility is good for helping you choose the right image and color scheme. I can enter draw-mode by typing "drawmode=yes" into parameters of this servlet's query string. Otherwise, this feature remains completely invisible to the end user and just serves as a means of creating images on the fly. If the user asks to go into draw-mode, a form will be printed out, allowing them to edit the query string parameters that create the image. */ else if (drawmode.equals(yes)) { // in order to show the image, we need to remove the "drawmode=yes" out of the query // string that will be used to re-call the servlet and show the image. We can just // remove the drawmode=yes substring. int yesfind = QueryString.indexOf("drawmode=yes"); StringBuffer sb = new StringBuffer(QueryString); String querysub = sb.delete(yesfind,yesfind+12).toString(); String query = ""; if (!QueryString.equals("")) { query = querysub; } else {query=""; } res.setContentType("text/html"); out.print("
CJ's Draw-Mode Utility
"); out.flush(); // start sending data to client while we wait //a copy of the updated image out.print("
Dimensions: "); out.print(width+" X "+height); out.flush(); // start sending data to client while we wait out.print("
Font Face: "+f.getFontName()+", Font Family: "+f.getFamily()); out.print("
"); // showing the query-string/URL needed for this image (for copy-paste purposes) out.print("
"); out.print("
Code for this image:
"); out.print("
"); out.flush(); // start sending data to client while we wait out.print("Font: "); out.print("
Show Available Fonts

"); out.print("Font Size:
"); out.flush(); // start sending data to client while we wait out.print("Text:
"); out.print("Image Color: (Red|Green|Blue, 0-255)
"); out.print("Text Color: (Red|Green|Blue, 0-255)
"); out.flush(); // start sending data to client while we wait out.print("Text Shading: (Red|Green|Blue, 0-255)
"); out.print("Image Raised?: (true|false, case-sensitive)
"); out.flush(); // start sending data to client while we wait out.print("Image Height: (Optional - number of pixels)
"); out.print("Image Width: (Optional - number of pixels)
"); out.flush(); // start sending data to client while we wait // --creating a hiddne parameter to keep us in draw-mode out.print("
"); // --credits out.print("
"); out.flush(); // start sending data to client while we wait out.print("CJ's Servlet Graphics Package
Copyright 2000 by Erica Andrews (cylikon@hotmail.com). All rights reserved. This servlet may NOT be used for any sort of profit either directly or indirectly without the express, written consent of the author of this servlet. For more cool Java stuff, visit: ./ButchWhipAppeal/ and ./IcedPinky/."); out.print("
"); } // *********************** // Beginning of basic image-on-the-fly creation /* If the user did not ask to see a list of fonts and did not ask to go into draw-mode, then the servlet will simply return an image */ else { try { // ---- now, creating the actual image "realimage" realimage = frame.createImage(width,height); this.realimage = realimage; g2 = realimage.getGraphics(); this.g2 = g2; g2.setFont(f); g2.setColor(new Color(i1,i2,i3)); // base image color g2.fill3DRect(0,0,width,height,raised); g2.setColor(new Color(s1,s2,s3)); // text shading color int y1 = height-fm.getDescent()-2; int y2 = height-fm.getDescent()-1; // --- drawing TWO copies of the string at slightly // --- different positions to create a shading effect g2.drawString(text, 4, y2); // drawing text shading g2.setColor(new Color(t1,t2,t3)); // actual text colors g2.drawString(text, 2, y1); // drawing text // Encode the off screen image into a GIF and send it to the client res.setContentType("image/gif"); GifEncoder encoder = new GifEncoder(realimage, out); encoder.encode(); // Error Handling /* Handles instances where people choose an RGB color index outside of the * range of 0-255 */ } catch (IllegalArgumentException iae) { // showing a default error image res.setContentType("image/gif"); ErrorImage = frame.createImage(350,20); g3 = frame.getGraphics(); g3.setFont(new Font("Serif",Font.PLAIN,8)); g3.setColor(new Color(255,0,0)); // base image color g3.fill3DRect(0,0,350,20,false); g3.setColor(new Color(255,255,255)); g3.drawString("ERROR: The RGB color index you entered was not between 0-255", 18, 18); this.ErrorImage = ErrorImage; this.g3 = g3; GifEncoder errorim = new GifEncoder(ErrorImage,out); errorim.encode(); } finally { // Clean up resources if (g != null) g.dispose(); if (frame != null) frame.removeNotify(); if (g2 != null) g2.dispose(); if (image != null) image.flush(); if (realimage != null) realimage.flush(); // if there was an error image, let's get rid of that too if (g3 != null) g3.dispose(); if (ErrorImage != null) ErrorImage.flush(); } // end of try-finally } // end If-Else** // Error handling /* Handles instances where people insert non-numeric characters into the query * string where numbers are needed (i.e. "abc" instead of 255); */ }catch (NumberFormatException nfe) { out.print("

ERROR CREATING IMAGE!:


You entered non-numeric characters where numbers were required!

"); } // end BIG try-catch } // end of doGet method // --- Servlet Info method public String getServletInfo() { return "CJ's Servlet Graphics Package. This servlet was created February 20, 2000 by Erica Andrews. The servlet makes images on the fly using the Acme GifEncoder class library, lists all of the fonts available on the server, and even has a draw-mode/preview feature for creating images. CJ's Servlet Graphics Package Copyright 2000 by Erica Andrews (cylikon@hotmail.com). All rights reserved. This servlet may NOT be used for any sort of profit either directly or indirectly without the express, written consent of the author of this servlet."; } } // end Class