Source Code Cross Referenced for ChartUtilities.java in  » Chart » jfreechart » org » jfree » chart » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Chart » jfreechart » org.jfree.chart 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ===========================================================
002:         * JFreeChart : a free chart library for the Java(tm) platform
003:         * ===========================================================
004:         *
005:         * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006:         *
007:         * Project Info:  http://www.jfree.org/jfreechart/index.html
008:         *
009:         * This library is free software; you can redistribute it and/or modify it 
010:         * under the terms of the GNU Lesser General Public License as published by 
011:         * the Free Software Foundation; either version 2.1 of the License, or 
012:         * (at your option) any later version.
013:         *
014:         * This library is distributed in the hope that it will be useful, but 
015:         * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016:         * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017:         * License for more details.
018:         *
019:         * You should have received a copy of the GNU Lesser General Public
020:         * License along with this library; if not, write to the Free Software
021:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022:         * USA.  
023:         *
024:         * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025:         * in the United States and other countries.]
026:         *
027:         * -------------------
028:         * ChartUtilities.java
029:         * -------------------
030:         * (C) Copyright 2001-2007, by Object Refinery Limited and Contributors.
031:         *
032:         * Original Author:  David Gilbert (for Object Refinery Limited);
033:         * Contributor(s):   Wolfgang Irler;
034:         *                   Richard Atkinson;
035:         *                   Xavier Poinsard;
036:         *
037:         * $Id: ChartUtilities.java,v 1.4.2.4 2007/03/19 14:07:53 mungady Exp $
038:         *
039:         * Changes
040:         * -------
041:         * 11-Dec-2001 : Version 1.  The JPEG method comes from Wolfgang Irler's 
042:         *               JFreeChartServletDemo class (DG);
043:         * 23-Jan-2002 : Changed saveChartAsXXX() methods to pass IOExceptions back to 
044:         *               caller (DG);
045:         * 26-Jun-2002 : Added image map methods (DG);
046:         * 05-Aug-2002 : Added writeBufferedImage methods
047:         *               Modified writeImageMap method to support flexible image 
048:         *               maps (RA);
049:         * 26-Aug-2002 : Added saveChartAsJPEG and writeChartAsJPEG methods with info 
050:         *               objects (RA);
051:         * 05-Sep-2002 : Added writeImageMap() method to support OverLIB
052:         *               - http://www.bosrup.com/web/overlib (RA);
053:         * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
054:         * 17-Oct-2002 : Exposed JPEG quality setting and PNG compression level as 
055:         *               parameters (DG);
056:         * 25-Oct-2002 : Fixed writeChartAsJPEG() empty method bug (DG);
057:         * 13-Mar-2003 : Updated writeImageMap method as suggested by Xavier Poinsard 
058:         *               (see Feature Request 688079) (DG);
059:         * 12-Aug-2003 : Added support for custom image maps using 
060:         *               ToolTipTagFragmentGenerator and URLTagFragmentGenerator (RA);
061:         * 02-Sep-2003 : Separated PNG encoding from writing chart to an 
062:         *               OutputStream (RA);
063:         * 04-Dec-2003 : Chart draw() method modified to include anchor point (DG);
064:         * 20-Feb-2004 : Edited Javadocs and added argument checking (DG);
065:         * 05-Apr-2004 : Fixed problem with buffered image type (DG);
066:         * 01-Aug-2004 : Modified to use EncoderUtil for all image encoding (RA);
067:         * 02-Aug-2004 : Delegated image map related functionality to ImageMapUtil (RA);
068:         * 13-Jan-2005 : Renamed ImageMapUtil --> ImageMapUtilities, removed method
069:         *               writeImageMap(PrintWriter, String, ChartRenderingInfo) which 
070:         *               exists in ImageMapUtilities (DG);
071:         * ------------- JFREECHART 1.0.x ---------------------------------------------
072:         * 06-Feb-2006 : API doc update (DG);
073:         * 19-Mar-2007 : Use try-finally to close output stream in saveChartAsXXX() 
074:         *               methods (DG);
075:         *
076:         */
077:
078:        package org.jfree.chart;
079:
080:        import java.awt.Graphics2D;
081:        import java.awt.geom.AffineTransform;
082:        import java.awt.geom.Rectangle2D;
083:        import java.awt.image.BufferedImage;
084:        import java.io.BufferedOutputStream;
085:        import java.io.File;
086:        import java.io.FileOutputStream;
087:        import java.io.IOException;
088:        import java.io.OutputStream;
089:        import java.io.PrintWriter;
090:
091:        import org.jfree.chart.imagemap.ImageMapUtilities;
092:        import org.jfree.chart.imagemap.OverLIBToolTipTagFragmentGenerator;
093:        import org.jfree.chart.imagemap.StandardToolTipTagFragmentGenerator;
094:        import org.jfree.chart.imagemap.StandardURLTagFragmentGenerator;
095:        import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator;
096:        import org.jfree.chart.imagemap.URLTagFragmentGenerator;
097:
098:        import org.jfree.chart.encoders.EncoderUtil;
099:        import org.jfree.chart.encoders.ImageFormat;
100:
101:        /**
102:         * A collection of utility methods for JFreeChart.  Includes methods for 
103:         * converting charts to image formats (PNG and JPEG) plus creating simple HTML 
104:         * image maps.
105:         * 
106:         * @see ImageMapUtilities
107:         */
108:        public abstract class ChartUtilities {
109:
110:            /**
111:             * Writes a chart to an output stream in PNG format.
112:             *
113:             * @param out  the output stream (<code>null</code> not permitted).
114:             * @param chart  the chart (<code>null</code> not permitted).
115:             * @param width  the image width.
116:             * @param height  the image height.
117:             *
118:             * @throws IOException if there are any I/O errors.
119:             */
120:            public static void writeChartAsPNG(OutputStream out,
121:                    JFreeChart chart, int width, int height) throws IOException {
122:
123:                // defer argument checking...
124:                writeChartAsPNG(out, chart, width, height, null);
125:
126:            }
127:
128:            /**
129:             * Writes a chart to an output stream in PNG format.
130:             *
131:             * @param out  the output stream (<code>null</code> not permitted).
132:             * @param chart  the chart (<code>null</code> not permitted).
133:             * @param width  the image width.
134:             * @param height  the image height.
135:             * @param encodeAlpha  encode alpha?
136:             * @param compression  the compression level (0-9).
137:             *
138:             * @throws IOException if there are any I/O errors.
139:             */
140:            public static void writeChartAsPNG(OutputStream out,
141:                    JFreeChart chart, int width, int height,
142:                    boolean encodeAlpha, int compression) throws IOException {
143:
144:                // defer argument checking...
145:                ChartUtilities.writeChartAsPNG(out, chart, width, height, null,
146:                        encodeAlpha, compression);
147:
148:            }
149:
150:            /**
151:             * Writes a chart to an output stream in PNG format.  This method allows 
152:             * you to pass in a {@link ChartRenderingInfo} object, to collect 
153:             * information about the chart dimensions/entities.  You will need this 
154:             * info if you want to create an HTML image map.
155:             *
156:             * @param out  the output stream (<code>null</code> not permitted).
157:             * @param chart  the chart (<code>null</code> not permitted).
158:             * @param width  the image width.
159:             * @param height  the image height.
160:             * @param info  the chart rendering info (<code>null</code> permitted).
161:             *
162:             * @throws IOException if there are any I/O errors.
163:             */
164:            public static void writeChartAsPNG(OutputStream out,
165:                    JFreeChart chart, int width, int height,
166:                    ChartRenderingInfo info) throws IOException {
167:
168:                if (chart == null) {
169:                    throw new IllegalArgumentException("Null 'chart' argument.");
170:                }
171:                BufferedImage bufferedImage = chart.createBufferedImage(width,
172:                        height, info);
173:                EncoderUtil.writeBufferedImage(bufferedImage, ImageFormat.PNG,
174:                        out);
175:            }
176:
177:            /**
178:             * Writes a chart to an output stream in PNG format.  This method allows 
179:             * you to pass in a {@link ChartRenderingInfo} object, to collect 
180:             * information about the chart dimensions/entities.  You will need this 
181:             * info if you want to create an HTML image map.
182:             *
183:             * @param out  the output stream (<code>null</code> not permitted).
184:             * @param chart  the chart (<code>null</code> not permitted).
185:             * @param width  the image width.
186:             * @param height  the image height.
187:             * @param info  carries back chart rendering info (<code>null</code> 
188:             *              permitted).
189:             * @param encodeAlpha  encode alpha?
190:             * @param compression  the PNG compression level (0-9).
191:             *
192:             * @throws IOException if there are any I/O errors.
193:             */
194:            public static void writeChartAsPNG(OutputStream out,
195:                    JFreeChart chart, int width, int height,
196:                    ChartRenderingInfo info, boolean encodeAlpha,
197:                    int compression) throws IOException {
198:
199:                if (out == null) {
200:                    throw new IllegalArgumentException("Null 'out' argument.");
201:                }
202:                if (chart == null) {
203:                    throw new IllegalArgumentException("Null 'chart' argument.");
204:                }
205:                BufferedImage chartImage = chart.createBufferedImage(width,
206:                        height, BufferedImage.TYPE_INT_ARGB, info);
207:                ChartUtilities.writeBufferedImageAsPNG(out, chartImage,
208:                        encodeAlpha, compression);
209:
210:            }
211:
212:            /**
213:             * Writes a scaled version of a chart to an output stream in PNG format.
214:             *
215:             * @param out  the output stream (<code>null</code> not permitted).
216:             * @param chart  the chart (<code>null</code> not permitted).
217:             * @param width  the unscaled chart width.
218:             * @param height  the unscaled chart height.
219:             * @param widthScaleFactor  the horizontal scale factor.
220:             * @param heightScaleFactor  the vertical scale factor.
221:             *
222:             * @throws IOException if there are any I/O problems.
223:             */
224:            public static void writeScaledChartAsPNG(OutputStream out,
225:                    JFreeChart chart, int width, int height,
226:                    int widthScaleFactor, int heightScaleFactor)
227:                    throws IOException {
228:
229:                if (out == null) {
230:                    throw new IllegalArgumentException("Null 'out' argument.");
231:                }
232:                if (chart == null) {
233:                    throw new IllegalArgumentException("Null 'chart' argument.");
234:                }
235:
236:                double desiredWidth = width * widthScaleFactor;
237:                double desiredHeight = height * heightScaleFactor;
238:                double defaultWidth = width;
239:                double defaultHeight = height;
240:                boolean scale = false;
241:
242:                // get desired width and height from somewhere then...
243:                if ((widthScaleFactor != 1) || (heightScaleFactor != 1)) {
244:                    scale = true;
245:                }
246:
247:                double scaleX = desiredWidth / defaultWidth;
248:                double scaleY = desiredHeight / defaultHeight;
249:
250:                BufferedImage image = new BufferedImage((int) desiredWidth,
251:                        (int) desiredHeight, BufferedImage.TYPE_INT_ARGB);
252:                Graphics2D g2 = image.createGraphics();
253:
254:                if (scale) {
255:                    AffineTransform saved = g2.getTransform();
256:                    g2.transform(AffineTransform.getScaleInstance(scaleX,
257:                            scaleY));
258:                    chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth,
259:                            defaultHeight), null, null);
260:                    g2.setTransform(saved);
261:                    g2.dispose();
262:                } else {
263:                    chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth,
264:                            defaultHeight), null, null);
265:                }
266:                out.write(encodeAsPNG(image));
267:
268:            }
269:
270:            /**
271:             * Saves a chart to the specified file in PNG format.
272:             *
273:             * @param file  the file name (<code>null</code> not permitted).
274:             * @param chart  the chart (<code>null</code> not permitted).
275:             * @param width  the image width.
276:             * @param height  the image height.
277:             *
278:             * @throws IOException if there are any I/O errors.
279:             */
280:            public static void saveChartAsPNG(File file, JFreeChart chart,
281:                    int width, int height) throws IOException {
282:
283:                // defer argument checking...
284:                saveChartAsPNG(file, chart, width, height, null);
285:
286:            }
287:
288:            /**
289:             * Saves a chart to a file in PNG format.  This method allows you to pass 
290:             * in a {@link ChartRenderingInfo} object, to collect information about the 
291:             * chart dimensions/entities.  You will need this info if you want to 
292:             * create an HTML image map.
293:             *
294:             * @param file  the file (<code>null</code> not permitted).
295:             * @param chart  the chart (<code>null</code> not permitted).
296:             * @param width  the image width.
297:             * @param height  the image height.
298:             * @param info  the chart rendering info (<code>null</code> permitted).
299:             *
300:             * @throws IOException if there are any I/O errors.
301:             */
302:            public static void saveChartAsPNG(File file, JFreeChart chart,
303:                    int width, int height, ChartRenderingInfo info)
304:                    throws IOException {
305:
306:                if (file == null) {
307:                    throw new IllegalArgumentException("Null 'file' argument.");
308:                }
309:                OutputStream out = new BufferedOutputStream(
310:                        new FileOutputStream(file));
311:                try {
312:                    ChartUtilities.writeChartAsPNG(out, chart, width, height,
313:                            info);
314:                } finally {
315:                    out.close();
316:                }
317:            }
318:
319:            /**
320:             * Saves a chart to a file in PNG format.  This method allows you to pass 
321:             * in a {@link ChartRenderingInfo} object, to collect information about the 
322:             * chart dimensions/entities.  You will need this info if you want to 
323:             * create an HTML image map.
324:             *
325:             * @param file  the file (<code>null</code> not permitted).
326:             * @param chart  the chart (<code>null</code> not permitted).
327:             * @param width  the image width.
328:             * @param height  the image height.
329:             * @param info  the chart rendering info (<code>null</code> permitted).
330:             * @param encodeAlpha  encode alpha?
331:             * @param compression  the PNG compression level (0-9).
332:             *
333:             * @throws IOException if there are any I/O errors.
334:             */
335:            public static void saveChartAsPNG(File file, JFreeChart chart,
336:                    int width, int height, ChartRenderingInfo info,
337:                    boolean encodeAlpha, int compression) throws IOException {
338:
339:                if (file == null) {
340:                    throw new IllegalArgumentException("Null 'file' argument.");
341:                }
342:                if (chart == null) {
343:                    throw new IllegalArgumentException("Null 'chart' argument.");
344:                }
345:
346:                OutputStream out = new BufferedOutputStream(
347:                        new FileOutputStream(file));
348:                try {
349:                    writeChartAsPNG(out, chart, width, height, info,
350:                            encodeAlpha, compression);
351:                } finally {
352:                    out.close();
353:                }
354:
355:            }
356:
357:            /**
358:             * Writes a chart to an output stream in JPEG format.  Please note that
359:             * JPEG is a poor format for chart images, use PNG if possible.
360:             * 
361:             * @param out  the output stream (<code>null</code> not permitted).
362:             * @param chart  the chart (<code>null</code> not permitted).
363:             * @param width  the image width.
364:             * @param height  the image height.
365:             *
366:             * @throws IOException if there are any I/O errors.
367:             */
368:            public static void writeChartAsJPEG(OutputStream out,
369:                    JFreeChart chart, int width, int height) throws IOException {
370:
371:                // defer argument checking...
372:                writeChartAsJPEG(out, chart, width, height, null);
373:
374:            }
375:
376:            /**
377:             * Writes a chart to an output stream in JPEG format.  Please note that
378:             * JPEG is a poor format for chart images, use PNG if possible.
379:             *
380:             * @param out  the output stream (<code>null</code> not permitted).
381:             * @param quality  the quality setting.
382:             * @param chart  the chart (<code>null</code> not permitted).
383:             * @param width  the image width.
384:             * @param height  the image height.
385:             *
386:             * @throws IOException if there are any I/O errors.
387:             */
388:            public static void writeChartAsJPEG(OutputStream out,
389:                    float quality, JFreeChart chart, int width, int height)
390:                    throws IOException {
391:
392:                // defer argument checking...
393:                ChartUtilities.writeChartAsJPEG(out, quality, chart, width,
394:                        height, null);
395:
396:            }
397:
398:            /**
399:             * Writes a chart to an output stream in JPEG format. This method allows 
400:             * you to pass in a {@link ChartRenderingInfo} object, to collect 
401:             * information about the chart dimensions/entities.  You will need this 
402:             * info if you want to create an HTML image map.
403:             *
404:             * @param out  the output stream (<code>null</code> not permitted).
405:             * @param chart  the chart (<code>null</code> not permitted).
406:             * @param width  the image width.
407:             * @param height  the image height.
408:             * @param info  the chart rendering info (<code>null</code> permitted).
409:             *
410:             * @throws IOException if there are any I/O errors.
411:             */
412:            public static void writeChartAsJPEG(OutputStream out,
413:                    JFreeChart chart, int width, int height,
414:                    ChartRenderingInfo info) throws IOException {
415:
416:                if (chart == null) {
417:                    throw new IllegalArgumentException("Null 'chart' argument.");
418:                }
419:                BufferedImage image = chart.createBufferedImage(width, height,
420:                        info);
421:                EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out);
422:
423:            }
424:
425:            /**
426:             * Writes a chart to an output stream in JPEG format.  This method allows 
427:             * you to pass in a {@link ChartRenderingInfo} object, to collect 
428:             * information about the chart dimensions/entities.  You will need this 
429:             * info if you want to create an HTML image map.
430:             *
431:             * @param out  the output stream (<code>null</code> not permitted).
432:             * @param quality  the output quality (0.0f to 1.0f).
433:             * @param chart  the chart (<code>null</code> not permitted).
434:             * @param width  the image width.
435:             * @param height  the image height.
436:             * @param info  the chart rendering info (<code>null</code> permitted).
437:             *
438:             * @throws IOException if there are any I/O errors.
439:             */
440:            public static void writeChartAsJPEG(OutputStream out,
441:                    float quality, JFreeChart chart, int width, int height,
442:                    ChartRenderingInfo info) throws IOException {
443:
444:                if (chart == null) {
445:                    throw new IllegalArgumentException("Null 'chart' argument.");
446:                }
447:                BufferedImage image = chart.createBufferedImage(width, height,
448:                        info);
449:                EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out,
450:                        quality);
451:
452:            }
453:
454:            /**
455:             * Saves a chart to a file in JPEG format.
456:             *
457:             * @param file  the file (<code>null</code> not permitted).
458:             * @param chart  the chart (<code>null</code> not permitted).
459:             * @param width  the image width.
460:             * @param height  the image height.
461:             *
462:             * @throws IOException if there are any I/O errors.
463:             */
464:            public static void saveChartAsJPEG(File file, JFreeChart chart,
465:                    int width, int height) throws IOException {
466:
467:                // defer argument checking...
468:                saveChartAsJPEG(file, chart, width, height, null);
469:
470:            }
471:
472:            /**
473:             * Saves a chart to a file in JPEG format.
474:             *
475:             * @param file  the file (<code>null</code> not permitted).
476:             * @param quality  the JPEG quality setting.
477:             * @param chart  the chart (<code>null</code> not permitted).
478:             * @param width  the image width.
479:             * @param height  the image height.
480:             *
481:             * @throws IOException if there are any I/O errors.
482:             */
483:            public static void saveChartAsJPEG(File file, float quality,
484:                    JFreeChart chart, int width, int height) throws IOException {
485:
486:                // defer argument checking...
487:                saveChartAsJPEG(file, quality, chart, width, height, null);
488:
489:            }
490:
491:            /**
492:             * Saves a chart to a file in JPEG format.  This method allows you to pass 
493:             * in a {@link ChartRenderingInfo} object, to collect information about the 
494:             * chart dimensions/entities.  You will need this info if you want to 
495:             * create an HTML image map.
496:             *
497:             * @param file  the file name (<code>null</code> not permitted).
498:             * @param chart  the chart (<code>null</code> not permitted).
499:             * @param width  the image width.
500:             * @param height  the image height.
501:             * @param info  the chart rendering info (<code>null</code> permitted).
502:             *
503:             * @throws IOException if there are any I/O errors.
504:             */
505:            public static void saveChartAsJPEG(File file, JFreeChart chart,
506:                    int width, int height, ChartRenderingInfo info)
507:                    throws IOException {
508:
509:                if (file == null) {
510:                    throw new IllegalArgumentException("Null 'file' argument.");
511:                }
512:                if (chart == null) {
513:                    throw new IllegalArgumentException("Null 'chart' argument.");
514:                }
515:                OutputStream out = new BufferedOutputStream(
516:                        new FileOutputStream(file));
517:                try {
518:                    writeChartAsJPEG(out, chart, width, height, info);
519:                } finally {
520:                    out.close();
521:                }
522:
523:            }
524:
525:            /**
526:             * Saves a chart to a file in JPEG format.  This method allows you to pass 
527:             * in a {@link ChartRenderingInfo} object, to collect information about the 
528:             * chart dimensions/entities.  You will need this info if you want to 
529:             * create an HTML image map.
530:             *
531:             * @param file  the file name (<code>null</code> not permitted).
532:             * @param quality  the quality setting.
533:             * @param chart  the chart (<code>null</code> not permitted).
534:             * @param width  the image width.
535:             * @param height  the image height.
536:             * @param info  the chart rendering info (<code>null</code> permitted).
537:             *
538:             * @throws IOException if there are any I/O errors.
539:             */
540:            public static void saveChartAsJPEG(File file, float quality,
541:                    JFreeChart chart, int width, int height,
542:                    ChartRenderingInfo info) throws IOException {
543:
544:                if (file == null) {
545:                    throw new IllegalArgumentException("Null 'file' argument.");
546:                }
547:                if (chart == null) {
548:                    throw new IllegalArgumentException("Null 'chart' argument.");
549:                }
550:
551:                OutputStream out = new BufferedOutputStream(
552:                        new FileOutputStream(file));
553:                try {
554:                    writeChartAsJPEG(out, quality, chart, width, height, info);
555:                } finally {
556:                    out.close();
557:                }
558:
559:            }
560:
561:            /**
562:             * Writes a {@link BufferedImage} to an output stream in JPEG format.
563:             *
564:             * @param out  the output stream (<code>null</code> not permitted).
565:             * @param image  the image (<code>null</code> not permitted).
566:             *
567:             * @throws IOException if there are any I/O errors.
568:             */
569:            public static void writeBufferedImageAsJPEG(OutputStream out,
570:                    BufferedImage image) throws IOException {
571:
572:                // defer argument checking...
573:                writeBufferedImageAsJPEG(out, 0.75f, image);
574:
575:            }
576:
577:            /**
578:             * Writes a {@link BufferedImage} to an output stream in JPEG format.
579:             *
580:             * @param out  the output stream (<code>null</code> not permitted).
581:             * @param quality  the image quality (0.0f to 1.0f).
582:             * @param image  the image (<code>null</code> not permitted).
583:             *
584:             * @throws IOException if there are any I/O errors.
585:             */
586:            public static void writeBufferedImageAsJPEG(OutputStream out,
587:                    float quality, BufferedImage image) throws IOException {
588:
589:                EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out,
590:                        quality);
591:
592:            }
593:
594:            /**
595:             * Writes a {@link BufferedImage} to an output stream in PNG format.
596:             *
597:             * @param out  the output stream (<code>null</code> not permitted).
598:             * @param image  the image (<code>null</code> not permitted).
599:             *
600:             * @throws IOException if there are any I/O errors.
601:             */
602:            public static void writeBufferedImageAsPNG(OutputStream out,
603:                    BufferedImage image) throws IOException {
604:
605:                EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out);
606:
607:            }
608:
609:            /**
610:             * Writes a {@link BufferedImage} to an output stream in PNG format.
611:             *
612:             * @param out  the output stream (<code>null</code> not permitted).
613:             * @param image  the image (<code>null</code> not permitted).
614:             * @param encodeAlpha  encode alpha?
615:             * @param compression  the compression level (0-9).
616:             *
617:             * @throws IOException if there are any I/O errors.
618:             */
619:            public static void writeBufferedImageAsPNG(OutputStream out,
620:                    BufferedImage image, boolean encodeAlpha, int compression)
621:                    throws IOException {
622:
623:                EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out,
624:                        compression, encodeAlpha);
625:            }
626:
627:            /**
628:             * Encodes a {@link BufferedImage} to PNG format.
629:             *
630:             * @param image  the image (<code>null</code> not permitted).
631:             *
632:             * @return A byte array in PNG format.
633:             * 
634:             * @throws IOException if there is an I/O problem.
635:             */
636:            public static byte[] encodeAsPNG(BufferedImage image)
637:                    throws IOException {
638:                return EncoderUtil.encode(image, ImageFormat.PNG);
639:            }
640:
641:            /**
642:             * Encodes a {@link BufferedImage} to PNG format.
643:             *
644:             * @param image  the image (<code>null</code> not permitted).
645:             * @param encodeAlpha  encode alpha?
646:             * @param compression  the PNG compression level (0-9).
647:             *
648:             * @return The byte array in PNG format.
649:             * 
650:             * @throws IOException if there is an I/O problem.
651:             */
652:            public static byte[] encodeAsPNG(BufferedImage image,
653:                    boolean encodeAlpha, int compression) throws IOException {
654:                return EncoderUtil.encode(image, ImageFormat.PNG, compression,
655:                        encodeAlpha);
656:            }
657:
658:            /**
659:             * Writes an image map to an output stream.
660:             *
661:             * @param writer  the writer (<code>null</code> not permitted).
662:             * @param name  the map name (<code>null</code> not permitted).
663:             * @param info  the chart rendering info (<code>null</code> not permitted).
664:             * @param useOverLibForToolTips  whether to use OverLIB for tooltips
665:             *                               (http://www.bosrup.com/web/overlib/).
666:             *
667:             * @throws IOException if there are any I/O errors.
668:             */
669:            public static void writeImageMap(PrintWriter writer, String name,
670:                    ChartRenderingInfo info, boolean useOverLibForToolTips)
671:                    throws IOException {
672:
673:                ToolTipTagFragmentGenerator toolTipTagFragmentGenerator = null;
674:                if (useOverLibForToolTips) {
675:                    toolTipTagFragmentGenerator = new OverLIBToolTipTagFragmentGenerator();
676:                } else {
677:                    toolTipTagFragmentGenerator = new StandardToolTipTagFragmentGenerator();
678:                }
679:                ImageMapUtilities.writeImageMap(writer, name, info,
680:                        toolTipTagFragmentGenerator,
681:                        new StandardURLTagFragmentGenerator());
682:
683:            }
684:
685:            /**
686:             * Writes an image map to the specified writer.
687:             *
688:             * @param writer  the writer (<code>null</code> not permitted).
689:             * @param name  the map name (<code>null</code> not permitted).
690:             * @param info  the chart rendering info (<code>null</code> not permitted).
691:             * @param toolTipTagFragmentGenerator  a generator for the HTML fragment
692:             *     that will contain the tooltip text (<code>null</code> not permitted 
693:             *     if <code>info</code> contains tooltip information).
694:             * @param urlTagFragmentGenerator  a generator for the HTML fragment that
695:             *     will contain the URL reference (<code>null</code> not permitted if 
696:             *     <code>info</code> contains URLs).
697:             *
698:             * @throws IOException if there are any I/O errors.
699:             */
700:            public static void writeImageMap(PrintWriter writer, String name,
701:                    ChartRenderingInfo info,
702:                    ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
703:                    URLTagFragmentGenerator urlTagFragmentGenerator)
704:                    throws IOException {
705:
706:                writer.println(ImageMapUtilities.getImageMap(name, info,
707:                        toolTipTagFragmentGenerator, urlTagFragmentGenerator));
708:            }
709:
710:            /**
711:             * Creates an HTML image map.  This method maps to 
712:             * {@link ImageMapUtilities#getImageMap(String, ChartRenderingInfo, 
713:             * ToolTipTagFragmentGenerator, URLTagFragmentGenerator)}, using default 
714:             * generators.
715:             *
716:             * @param name  the map name (<code>null</code> not permitted).
717:             * @param info  the chart rendering info (<code>null</code> not permitted).
718:             *
719:             * @return The map tag.
720:             */
721:            public static String getImageMap(String name,
722:                    ChartRenderingInfo info) {
723:                return ImageMapUtilities.getImageMap(name, info,
724:                        new StandardToolTipTagFragmentGenerator(),
725:                        new StandardURLTagFragmentGenerator());
726:            }
727:
728:            /**
729:             * Creates an HTML image map.  This method maps directly to
730:             * {@link ImageMapUtilities#getImageMap(String, ChartRenderingInfo, 
731:             * ToolTipTagFragmentGenerator, URLTagFragmentGenerator)}.
732:             *
733:             * @param name  the map name (<code>null</code> not permitted).
734:             * @param info  the chart rendering info (<code>null</code> not permitted).
735:             * @param toolTipTagFragmentGenerator  a generator for the HTML fragment
736:             *     that will contain the tooltip text (<code>null</code> not permitted 
737:             *     if <code>info</code> contains tooltip information).
738:             * @param urlTagFragmentGenerator  a generator for the HTML fragment that
739:             *     will contain the URL reference (<code>null</code> not permitted if 
740:             *     <code>info</code> contains URLs).
741:             *
742:             * @return The map tag.
743:             */
744:            public static String getImageMap(String name,
745:                    ChartRenderingInfo info,
746:                    ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
747:                    URLTagFragmentGenerator urlTagFragmentGenerator) {
748:
749:                return ImageMapUtilities.getImageMap(name, info,
750:                        toolTipTagFragmentGenerator, urlTagFragmentGenerator);
751:
752:            }
753:
754:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.