001: /* ================================================================
002: * Cewolf : Chart enabling Web Objects Framework
003: * ================================================================
004: *
005: * Project Info: http://cewolf.sourceforge.net
006: * Project Lead: Guido Laures (guido@laures.de);
007: *
008: * (C) Copyright 2002, by Guido Laures
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package de.laures.cewolf.util;
024:
025: import java.awt.Color;
026: import java.awt.Font;
027: import java.awt.Graphics;
028: import java.awt.image.BufferedImage;
029: import java.io.IOException;
030: import java.io.OutputStream;
031: import java.io.PrintWriter;
032: import java.io.StringWriter;
033: import java.util.Enumeration;
034: import java.util.StringTokenizer;
035:
036: import com.sun.image.codec.jpeg.JPEGCodec;
037: import com.sun.image.codec.jpeg.JPEGEncodeParam;
038: import com.sun.image.codec.jpeg.JPEGImageEncoder;
039:
040: /**
041: * Some methods to render messages or exceptions into an image.
042: * @author glaures
043: */
044: public class RenderingHelper {
045:
046: private final static int PADDING_X = 5;
047:
048: public static String renderMessage(String msg, int width,
049: int height, OutputStream out) throws IOException {
050: BufferedImage image = ImageHelper.createImage(width, height);
051: Graphics gr = image.getGraphics();
052: gr.setColor(Color.white);
053: gr.fillRect(0, 0, width, height);
054: gr.setColor(Color.black);
055: gr.drawString(msg, PADDING_X, height / 2 - 7);
056: JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
057: JPEGEncodeParam param = encoder
058: .getDefaultJPEGEncodeParam(image);
059: param.setQuality(1.0f, true);
060: encoder.encode(image, param);
061: return "image/jpeg";
062: }
063:
064: public static String renderException(Throwable ex, int width,
065: int height, OutputStream out) throws IOException {
066: BufferedImage image = ImageHelper.createImage(width, height);
067: Graphics gr = image.getGraphics();
068: gr.setColor(Color.white);
069: gr.fillRect(0, 0, width, height);
070: gr.setColor(Color.red);
071: gr.drawString(ex.getClass().getName() + " raised:", PADDING_X,
072: 15);
073: gr.drawString(String.valueOf(ex.getMessage()), PADDING_X, 30);
074: gr.setColor(Color.black);
075: Font stFont = gr.getFont().deriveFont(9f);
076: gr.setFont(stFont);
077: drawStackTrace(gr, PADDING_X, 50, ex);
078: JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
079: JPEGEncodeParam param = encoder
080: .getDefaultJPEGEncodeParam(image);
081: param.setQuality(1.0f, true);
082: encoder.encode(image, param);
083: return "image/jpeg";
084: }
085:
086: private static void drawStackTrace(Graphics gr, int x, int y,
087: Throwable ex) {
088: final int linePadding = 4;
089: int lineHeight = gr.getFont().getSize() + linePadding;
090: int currY = y;
091: Enumeration lines = new StringTokenizer(getStackTrace(ex),
092: "\n", false);
093: while (lines.hasMoreElements()) {
094: gr.drawString(((String) lines.nextElement()).trim(), x,
095: currY);
096: currY += lineHeight;
097: }
098: }
099:
100: private static String getStackTrace(Throwable t) {
101: StringWriter sw = new StringWriter();
102: PrintWriter pw = new PrintWriter(sw);
103: t.printStackTrace(pw);
104: pw.close();
105: return sw.getBuffer().toString();
106: }
107:
108: }
|