KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > laures > cewolf > util > RenderingHelper


1 /* ================================================================
2  * Cewolf : Chart enabling Web Objects Framework
3  * ================================================================
4  *
5  * Project Info: http://cewolf.sourceforge.net
6  * Project Lead: Guido Laures (guido@laures.de);
7  *
8  * (C) Copyright 2002, by Guido Laures
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23 package de.laures.cewolf.util;
24
25 import java.awt.Color JavaDoc;
26 import java.awt.Font JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import java.awt.image.BufferedImage JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.io.StringWriter JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35
36 import com.sun.image.codec.jpeg.JPEGCodec;
37 import com.sun.image.codec.jpeg.JPEGEncodeParam;
38 import com.sun.image.codec.jpeg.JPEGImageEncoder;
39
40 /**
41  * Some methods to render messages or exceptions into an image.
42  * @author glaures
43  */

44 public class RenderingHelper {
45     
46     private final static int PADDING_X = 5;
47     
48     public static String JavaDoc renderMessage(String JavaDoc msg, int width, int height, OutputStream JavaDoc out) throws IOException JavaDoc {
49         BufferedImage JavaDoc image = ImageHelper.createImage(width, height);
50         Graphics JavaDoc gr = image.getGraphics();
51         gr.setColor(Color.white);
52         gr.fillRect(0, 0, width, height);
53         gr.setColor(Color.black);
54         gr.drawString(msg, PADDING_X, height/2 - 7);
55         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
56         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
57         param.setQuality(1.0f, true);
58         encoder.encode(image, param);
59         return "image/jpeg";
60     }
61     
62     public static String JavaDoc renderException(Throwable JavaDoc ex, int width, int height, OutputStream JavaDoc out) throws IOException JavaDoc {
63         BufferedImage JavaDoc image = ImageHelper.createImage(width, height);
64         Graphics JavaDoc gr = image.getGraphics();
65         gr.setColor(Color.white);
66         gr.fillRect(0, 0, width, height);
67         gr.setColor(Color.red);
68         gr.drawString(ex.getClass().getName() + " raised:", PADDING_X, 15);
69         gr.drawString(String.valueOf(ex.getMessage()), PADDING_X, 30);
70         gr.setColor(Color.black);
71         Font JavaDoc stFont = gr.getFont().deriveFont(9f);
72         gr.setFont(stFont);
73         drawStackTrace(gr, PADDING_X, 50, ex);
74         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
75         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
76         param.setQuality(1.0f, true);
77         encoder.encode(image, param);
78         return "image/jpeg";
79     }
80     
81     private static void drawStackTrace(Graphics JavaDoc gr, int x, int y, Throwable JavaDoc ex) {
82         final int linePadding = 4;
83         int lineHeight = gr.getFont().getSize() + linePadding;
84         int currY = y;
85         Enumeration JavaDoc lines = new StringTokenizer JavaDoc(getStackTrace(ex), "\n", false);
86         while (lines.hasMoreElements()) {
87             gr.drawString(((String JavaDoc)lines.nextElement()).trim(), x, currY);
88             currY += lineHeight;
89         }
90     }
91     
92     private static String JavaDoc getStackTrace(Throwable JavaDoc t) {
93         StringWriter JavaDoc sw = new StringWriter JavaDoc();
94         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
95         t.printStackTrace(pw);
96         pw.close();
97         return sw.getBuffer().toString();
98     }
99     
100 }
101
Popular Tags