001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: ChartEncoder.java
021: Created on 11. November 2001, 18:23
022: */
023:
024: package de.progra.charting;
025:
026: import javax.imageio.ImageIO;
027: import java.awt.image.BufferedImage;
028: import java.io.OutputStream;
029: import java.io.IOException;
030: import java.awt.Rectangle;
031: import java.awt.Graphics2D;
032:
033: /**
034: * The ChartEncoder class provides several static methods to encode
035: * charts to an OutputStream. It uses the Java Advanced Imaging Library
036: * which is part of the JDK 1.4 release. The list of supported Image Formats
037: * may vary depending on the actual release. Quoting from the current
038: * webpage as of 28.1.2002: "As of JAI-1.1.1, the latest public version of JAI,
039: * the image formats supported by these ancillary codec classes are:
040: * BMP, GIF (decoder only), FlashPix (decoder only), JPEG, PNG, PNM, and TIFF."
041: * For the actual list of supported image formats call
042: * <code>{@link #getSupportedFormats}</code>.
043: * @author mueller
044: * @version 1.0
045: */
046: public class ChartEncoder {
047:
048: /** Prints the JPEG encoded image to an output stream.
049: * @param os the OutputStream where the image will be printed to.
050: * @param chart the Chart which will be printed to the output stream
051: * @throws EncodingException if an error occurred accessing the Stream
052: */
053: public static void createJPEG(OutputStream os, Chart chart)
054: throws EncodingException {
055: boolean success = true;
056: try {
057: Rectangle r = chart.getBounds();
058: BufferedImage img = new BufferedImage((int) r.getWidth(),
059: (int) r.getHeight(), BufferedImage.TYPE_INT_RGB);
060:
061: Graphics2D grafx = img.createGraphics();
062: chart.render(grafx);
063: success = ImageIO.write(img, "jpeg", os);
064: os.flush();
065: } catch (Throwable t) {
066: throw new EncodingException(t.getMessage(), t);
067: }
068:
069: if (!success)
070: throw new EncodingException(
071: "No ImageWriter for writing JPEGs found.");
072: }
073:
074: /** Prints the GIF encoded image to an output stream.
075: * @param os the OutputStream where the image will be printed to.
076: * @param chart the Chart which will be printed to the output stream
077: * @throws EncodingException if an error occurred accessing the Stream
078: * @deprecated GIF encoding is no longer supported, use PNG instead
079: */
080: public static void createGIF(OutputStream os, Chart chart)
081: throws EncodingException {
082: }
083:
084: /** Prints the PNG encoded image to an output stream.
085: * @param os the OutputStream where the image will be printed to.
086: * @param chart the Chart which will be printed to the output stream
087: * @throws EncodingException if an error occurred accessing the Stream
088: */
089: public static void createPNG(OutputStream os, Chart chart)
090: throws EncodingException {
091: boolean success = true;
092: try {
093: Rectangle r = chart.getBounds();
094: BufferedImage img = new BufferedImage((int) r.getWidth(),
095: (int) r.getHeight(), BufferedImage.TYPE_INT_RGB);
096:
097: Graphics2D grafx = img.createGraphics();
098: chart.render(grafx);
099: success = ImageIO.write(img, "png", os);
100: os.flush();
101: } catch (Throwable t) {
102: t.printStackTrace();
103: throw new EncodingException(t.getMessage(), t);
104: }
105:
106: if (!success)
107: throw new EncodingException(
108: "No ImageWriter for writing PNGs found.");
109: }
110:
111: /** Prints the encoded image to an output stream.
112: * @param os the OutputStream where the image will be printed to.
113: * @param chart the Chart which will be printed to the output stream
114: * @param format the informal format name
115: * @throws EncodingException if an error occurred accessing the Stream
116: */
117: public static void createEncodedImage(OutputStream os, Chart chart,
118: String format) throws EncodingException {
119: boolean success = true;
120: try {
121: Rectangle r = chart.getBounds();
122: BufferedImage img = new BufferedImage((int) r.getWidth(),
123: (int) r.getHeight(), BufferedImage.TYPE_INT_RGB);
124:
125: Graphics2D grafx = img.createGraphics();
126: chart.render(grafx);
127: success = ImageIO.write(img, format, os);
128: os.flush();
129: } catch (Throwable t) {
130: throw new EncodingException(t.getMessage(), t);
131: }
132:
133: if (!success)
134: throw new EncodingException("No ImageWriter for writing "
135: + format + " images found.");
136: }
137:
138: /** Returns a String array containing the informal format names for
139: * all supported image encodings.
140: */
141: public static String[] getSupportedFormats() {
142: return ImageIO.getWriterFormatNames();
143: }
144: }
|