KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > encoders > ImageEncoderFactory


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ------------------------
28  * ImageEncoderFactory.java
29  * ------------------------
30  * (C) Copyright 2004, 2005, by Richard Atkinson and Contributors.
31  *
32  * Original Author: Richard Atkinson;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: ImageEncoderFactory.java,v 1.3.2.2 2005/11/01 16:37:01 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 01-Aug-2004 : Initial version (RA);
40  * 01-Nov-2005 : Now using ImageIO for JPEG encoding, so we no longer have a
41  * dependency on com.sun.* which isn't available on all
42  * implementations (DG);
43  *
44  */

45
46 package org.jfree.chart.encoders;
47
48 import java.util.Hashtable JavaDoc;
49
50 /**
51  * Factory class for returning {@link ImageEncoder}s for different
52  * {@link ImageFormat}s.
53  *
54  * @author Richard Atkinson
55  */

56 public class ImageEncoderFactory {
57     private static Hashtable JavaDoc encoders = null;
58
59     static {
60         init();
61     }
62
63     /**
64      * Sets up default encoders (uses Sun PNG Encoder if JDK 1.4+ and the
65      * SunPNGEncoderAdapter class is available).
66      */

67     private static void init() {
68         encoders = new Hashtable JavaDoc();
69         encoders.put("jpeg", "org.jfree.chart.encoders.SunJPEGEncoderAdapter");
70         try {
71             // Test for being run under JDK 1.4+
72
Class.forName("javax.imageio.ImageIO");
73             // Test for JFreeChart being compiled under JDK 1.4+
74
Class.forName("org.jfree.chart.encoders.SunPNGEncoderAdapter");
75             encoders.put("png",
76                     "org.jfree.chart.encoders.SunPNGEncoderAdapter");
77             encoders.put("jpeg",
78                     "org.jfree.chart.encoders.SunJPEGEncoderAdapter");
79         }
80         catch (ClassNotFoundException JavaDoc e) {
81             encoders.put("png",
82                     "org.jfree.chart.encoders.KeypointPNGEncoderAdapter");
83         }
84     }
85
86     /**
87      * Used to set additional encoders or replace default ones.
88      *
89      * @param format The image format name.
90      * @param imageEncoderClassName The name of the ImageEncoder class.
91      */

92     public static void setImageEncoder(String JavaDoc format,
93                                        String JavaDoc imageEncoderClassName) {
94         encoders.put(format, imageEncoderClassName);
95     }
96
97     /**
98      * Used to retrieve an ImageEncoder for a specific image format.
99      *
100      * @param format The image format required.
101      *
102      * @return The ImageEncoder or <code>null</code> if none available.
103      */

104     public static ImageEncoder newInstance(String JavaDoc format) {
105         ImageEncoder imageEncoder = null;
106         String JavaDoc className = (String JavaDoc) encoders.get(format);
107         if (className == null) {
108             throw new IllegalArgumentException JavaDoc("Unsupported image format - "
109                     + format);
110         }
111         try {
112             Class JavaDoc imageEncoderClass = Class.forName(className);
113             imageEncoder = (ImageEncoder) imageEncoderClass.newInstance();
114         }
115         catch (Exception JavaDoc e) {
116             throw new IllegalArgumentException JavaDoc(e.toString());
117         }
118         return imageEncoder;
119     }
120
121     /**
122      * Used to retrieve an ImageEncoder for a specific image format.
123      *
124      * @param format The image format required.
125      * @param quality The quality to be set before returning.
126      *
127      * @return The ImageEncoder or <code>null</code> if none available.
128      */

129     public static ImageEncoder newInstance(String JavaDoc format, float quality) {
130         ImageEncoder imageEncoder = newInstance(format);
131         imageEncoder.setQuality(quality);
132         return imageEncoder;
133     }
134
135     /**
136      * Used to retrieve an ImageEncoder for a specific image format.
137      *
138      * @param format The image format required.
139      * @param encodingAlpha Sets whether alpha transparency should be encoded.
140      *
141      * @return The ImageEncoder or <code>null</code> if none available.
142      */

143     public static ImageEncoder newInstance(String JavaDoc format,
144                                            boolean encodingAlpha) {
145         ImageEncoder imageEncoder = newInstance(format);
146         imageEncoder.setEncodingAlpha(encodingAlpha);
147         return imageEncoder;
148     }
149
150     /**
151      * Used to retrieve an ImageEncoder for a specific image format.
152      *
153      * @param format The image format required.
154      * @param quality The quality to be set before returning.
155      * @param encodingAlpha Sets whether alpha transparency should be encoded.
156      *
157      * @return The ImageEncoder or <code>null</code> if none available.
158      */

159     public static ImageEncoder newInstance(String JavaDoc format, float quality,
160                                            boolean encodingAlpha) {
161         ImageEncoder imageEncoder = newInstance(format);
162         imageEncoder.setQuality(quality);
163         imageEncoder.setEncodingAlpha(encodingAlpha);
164         return imageEncoder;
165     }
166
167 }
168
Popular Tags