KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > ChartEncoder


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     ChartEncoder.java
21     Created on 11. November 2001, 18:23
22  */

23
24 package de.progra.charting;
25
26 import javax.imageio.ImageIO JavaDoc;
27 import java.awt.image.BufferedImage JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.awt.Rectangle JavaDoc;
31 import java.awt.Graphics2D JavaDoc;
32
33 /**
34  * The ChartEncoder class provides several static methods to encode
35  * charts to an OutputStream. It uses the Java Advanced Imaging Library
36  * which is part of the JDK 1.4 release. The list of supported Image Formats
37  * may vary depending on the actual release. Quoting from the current
38  * webpage as of 28.1.2002: "As of JAI-1.1.1, the latest public version of JAI,
39  * the image formats supported by these ancillary codec classes are:
40  * BMP, GIF (decoder only), FlashPix (decoder only), JPEG, PNG, PNM, and TIFF."
41  * For the actual list of supported image formats call
42  * <code>{@link #getSupportedFormats}</code>.
43  * @author mueller
44  * @version 1.0
45  */

46 public class ChartEncoder {
47     
48     /** Prints the JPEG encoded image to an output stream.
49      * @param os the OutputStream where the image will be printed to.
50      * @param chart the Chart which will be printed to the output stream
51      * @throws EncodingException if an error occurred accessing the Stream
52      */

53     public static void createJPEG(OutputStream JavaDoc os, Chart chart) throws EncodingException {
54         boolean success = true;
55         try {
56             Rectangle JavaDoc r = chart.getBounds();
57             BufferedImage JavaDoc img = new BufferedImage JavaDoc((int)r.getWidth(),
58                                                   (int)r.getHeight(),
59                                                   BufferedImage.TYPE_INT_RGB);
60
61             Graphics2D JavaDoc grafx = img.createGraphics();
62             chart.render(grafx);
63             success = ImageIO.write(img, "jpeg", os);
64             os.flush();
65         } catch(Throwable JavaDoc t) {
66             throw new EncodingException(t.getMessage(), t);
67         }
68         
69         if(!success)
70             throw new EncodingException("No ImageWriter for writing JPEGs found.");
71     }
72     
73     /** Prints the GIF encoded image to an output stream.
74      * @param os the OutputStream where the image will be printed to.
75      * @param chart the Chart which will be printed to the output stream
76      * @throws EncodingException if an error occurred accessing the Stream
77      * @deprecated GIF encoding is no longer supported, use PNG instead
78      */

79     public static void createGIF(OutputStream JavaDoc os, Chart chart) throws EncodingException {
80     }
81     
82     /** Prints the PNG encoded image to an output stream.
83      * @param os the OutputStream where the image will be printed to.
84      * @param chart the Chart which will be printed to the output stream
85      * @throws EncodingException if an error occurred accessing the Stream
86      */

87     public static void createPNG(OutputStream JavaDoc os, Chart chart) throws EncodingException {
88         boolean success = true;
89         try {
90             Rectangle JavaDoc r = chart.getBounds();
91             BufferedImage JavaDoc img = new BufferedImage JavaDoc((int)r.getWidth(),
92                                                   (int)r.getHeight(),
93                                                   BufferedImage.TYPE_INT_RGB);
94
95             Graphics2D JavaDoc grafx = img.createGraphics();
96             chart.render(grafx);
97             success = ImageIO.write(img, "png", os);
98             os.flush();
99         } catch(Throwable JavaDoc t) {
100             t.printStackTrace();
101             throw new EncodingException(t.getMessage(), t);
102         }
103         
104         if(!success)
105             throw new EncodingException("No ImageWriter for writing PNGs found.");
106     }
107     
108     /** Prints the encoded image to an output stream.
109      * @param os the OutputStream where the image will be printed to.
110      * @param chart the Chart which will be printed to the output stream
111      * @param format the informal format name
112      * @throws EncodingException if an error occurred accessing the Stream
113      */

114     public static void createEncodedImage(OutputStream JavaDoc os, Chart chart, String JavaDoc format) throws EncodingException {
115         boolean success = true;
116         try {
117             Rectangle JavaDoc r = chart.getBounds();
118             BufferedImage JavaDoc img = new BufferedImage JavaDoc((int)r.getWidth(),
119                                                   (int)r.getHeight(),
120                                                   BufferedImage.TYPE_INT_RGB);
121
122             Graphics2D JavaDoc grafx = img.createGraphics();
123             chart.render(grafx);
124             success = ImageIO.write(img, format, os);
125             os.flush();
126         } catch(Throwable JavaDoc t) {
127             throw new EncodingException(t.getMessage(), t);
128         }
129         
130         if(!success)
131             throw new EncodingException("No ImageWriter for writing "+format+" images found.");
132     }
133     
134     /** Returns a String array containing the informal format names for
135      * all supported image encodings.
136      */

137     public static String JavaDoc[] getSupportedFormats() {
138        return ImageIO.getWriterFormatNames();
139     }
140 }
141
Popular Tags