001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2014, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ---------------
028 * JFreeChart.java
029 * ---------------
030 * (C) Copyright 2000-2014, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   Andrzej Porebski;
034 *                   David Li;
035 *                   Wolfgang Irler;
036 *                   Christian W. Zuckschwerdt;
037 *                   Klaus Rheinwald;
038 *                   Nicolas Brodu;
039 *                   Peter Kolb (patch 2603321);
040 *
041 * NOTE: The above list of contributors lists only the people that have
042 * contributed to this source file (JFreeChart.java) - for a list of ALL
043 * contributors to the project, please see the README.txt file.
044 *
045 * Changes (from 20-Jun-2001)
046 * --------------------------
047 * 20-Jun-2001 : Modifications submitted by Andrzej Porebski for legend
048 *               placement;
049 * 21-Jun-2001 : Removed JFreeChart parameter from Plot constructors (DG);
050 * 22-Jun-2001 : Multiple titles added (original code by David Berry, with
051 *               reworkings by DG);
052 * 18-Sep-2001 : Updated header (DG);
053 * 15-Oct-2001 : Moved data source classes into new package
054 *               com.jrefinery.data.* (DG);
055 * 18-Oct-2001 : New factory method for creating VerticalXYBarChart (DG);
056 * 19-Oct-2001 : Moved series paint and stroke methods to the Plot class (DG);
057 *               Moved static chart creation methods to new ChartFactory
058 *               class (DG);
059 * 22-Oct-2001 : Renamed DataSource.java --> Dataset.java etc. (DG);
060 *               Fixed bug where chart isn't registered with the dataset (DG);
061 * 07-Nov-2001 : Fixed bug where null title in constructor causes
062 *               exception (DG);
063 *               Tidied up event notification code (DG);
064 * 17-Nov-2001 : Added getLegendItemCount() method (DG);
065 * 21-Nov-2001 : Set clipping in draw method to ensure that nothing gets drawn
066 *               outside the chart area (DG);
067 * 11-Dec-2001 : Added the createBufferedImage() method, taken from the
068 *               JFreeChartServletDemo class (DG);
069 * 13-Dec-2001 : Added tooltips (DG);
070 * 16-Jan-2002 : Added handleClick() method (DG);
071 * 22-Jan-2002 : Fixed bug correlating legend labels with pie data (DG);
072 * 05-Feb-2002 : Removed redundant tooltips code (DG);
073 * 19-Feb-2002 : Added accessor methods for the backgroundImage and
074 *               backgroundImageAlpha attributes (DG);
075 * 21-Feb-2002 : Added static fields for INFO, COPYRIGHT, LICENCE, CONTRIBUTORS
076 *               and LIBRARIES.  These can be used to display information about
077 *               JFreeChart (DG);
078 * 06-Mar-2002 : Moved constants to JFreeChartConstants interface (DG);
079 * 18-Apr-2002 : PieDataset is no longer sorted (oldman);
080 * 23-Apr-2002 : Moved dataset to the Plot class (DG);
081 * 13-Jun-2002 : Added an extra draw() method (DG);
082 * 25-Jun-2002 : Implemented the Drawable interface and removed redundant
083 *               imports (DG);
084 * 26-Jun-2002 : Added another createBufferedImage() method (DG);
085 * 18-Sep-2002 : Fixed issues reported by Checkstyle (DG);
086 * 23-Sep-2002 : Added new contributor (DG);
087 * 28-Oct-2002 : Created main title and subtitle list to replace existing title
088 *               list (DG);
089 * 08-Jan-2003 : Added contributor (DG);
090 * 17-Jan-2003 : Added new constructor (DG);
091 * 22-Jan-2003 : Added ChartColor class by Cameron Riley, and background image
092 *               alignment code by Christian W. Zuckschwerdt (DG);
093 * 11-Feb-2003 : Added flag to allow suppression of chart change events, based
094 *               on a suggestion by Klaus Rheinwald (DG);
095 * 04-Mar-2003 : Added small fix for suppressed chart change events (see bug id
096 *               690865) (DG);
097 * 10-Mar-2003 : Added Benoit Xhenseval to contributors (DG);
098 * 26-Mar-2003 : Implemented Serializable (DG);
099 * 15-Jul-2003 : Added an optional border for the chart (DG);
100 * 11-Sep-2003 : Took care of listeners while cloning (NB);
101 * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
102 * 22-Sep-2003 : Added nullpointer checks.
103 * 25-Sep-2003 : Added nullpointer checks too (NB).
104 * 03-Dec-2003 : Legends are now registered by this class instead of using the
105 *               old constructor way (TM);
106 * 03-Dec-2003 : Added anchorPoint to draw() method (DG);
107 * 08-Jan-2004 : Reworked title code, introducing line wrapping (DG);
108 * 09-Feb-2004 : Created additional createBufferedImage() method (DG);
109 * 05-Apr-2004 : Added new createBufferedImage() method (DG);
110 * 27-May-2004 : Moved constants from JFreeChartConstants.java back to this
111 *               class (DG);
112 * 25-Nov-2004 : Updates for changes to Title class (DG);
113 * 06-Jan-2005 : Change lookup for default background color (DG);
114 * 31-Jan-2005 : Added Don Elliott to contributors (DG);
115 * 02-Feb-2005 : Added clearSubtitles() method (DG);
116 * 03-Feb-2005 : Added Mofeed Shahin to contributors (DG);
117 * 08-Feb-2005 : Updated for RectangleConstraint changes (DG);
118 * 28-Mar-2005 : Renamed Legend --> OldLegend (DG);
119 * 12-Apr-2005 : Added methods to access legend(s) in subtitle list (DG);
120 * 13-Apr-2005 : Added removeLegend() and removeSubtitle() methods (DG);
121 * 20-Apr-2005 : Modified to collect chart entities from titles and
122 *               subtitles (DG);
123 * 26-Apr-2005 : Removed LOGGER (DG);
124 * 06-Jun-2005 : Added addLegend() method and padding attribute, fixed equals()
125 *               method (DG);
126 * 24-Nov-2005 : Removed OldLegend and related code - don't want to support
127 *               this in 1.0.0 final (DG);
128 * ------------- JFREECHART 1.0.x ---------------------------------------------
129 * 27-Jan-2006 : Updated version number (DG);
130 * 07-Dec-2006 : Added some missing credits (DG);
131 * 17-Jan-2007 : Added Darren Jung to contributor list (DG);
132 * 05-Mar-2007 : Added Sergei Ivanov to the contributor list (DG);
133 * 16-Mar-2007 : Modified initial legend border (DG);
134 * 22-Mar-2007 : New methods for text anti-aliasing (DG);
135 * 16-May-2007 : Fixed argument check in getSubtitle(), copy list in
136 *               get/setSubtitles(), and added new addSubtitle(int, Title)
137 *               method (DG);
138 * 05-Jun-2007 : Add change listener to default legend (DG);
139 * 04-Dec-2007 : In createBufferedImage() methods, make the default image type
140 *               BufferedImage.TYPE_INT_ARGB (thanks to Klaus Rheinwald) (DG);
141 * 05-Dec-2007 : Fixed bug 1749124 (not registering as listener with
142 *               TextTitle) (DG);
143 * 23-Apr-2008 : Added new contributor (Diego Pierangeli) (DG);
144 * 16-May-2008 : Added new contributor (Michael Siemer) (DG);
145 * 19-Sep-2008 : Check for title visibility (DG);
146 * 18-Dec-2008 : Use ResourceBundleWrapper - see patch 1607918 by
147 *               Jess Thrysoee (DG);
148 * 19-Mar-2009 : Added entity support - see patch 2603321 by Peter Kolb (DG);
149 * 19-May-2009 : Fixed FindBugs warnings, patch by Michal Wozniak (DG);
150 * 29-Jun-2009 : Check visibility flag in main title (DG);
151 * 02-Jul-2013 : Use ParamChecks class (DG);
152 * 21-Jun-2014 : Set default hint value for stroke control (DG);
153 *
154 */
155
156package org.jfree.chart;
157
158import java.awt.AlphaComposite;
159import java.awt.BasicStroke;
160import java.awt.Color;
161import java.awt.Composite;
162import java.awt.Font;
163import java.awt.Graphics2D;
164import java.awt.Image;
165import java.awt.Paint;
166import java.awt.RenderingHints;
167import java.awt.Shape;
168import java.awt.Stroke;
169import java.awt.geom.AffineTransform;
170import java.awt.geom.Point2D;
171import java.awt.geom.Rectangle2D;
172import java.awt.image.BufferedImage;
173import java.io.IOException;
174import java.io.ObjectInputStream;
175import java.io.ObjectOutputStream;
176import java.io.Serializable;
177import java.net.URL;
178import java.util.ArrayList;
179import java.util.Arrays;
180import java.util.Iterator;
181import java.util.List;
182import java.util.ResourceBundle;
183
184import javax.swing.ImageIcon;
185import javax.swing.UIManager;
186import javax.swing.event.EventListenerList;
187
188import org.jfree.JCommon;
189import org.jfree.chart.block.BlockParams;
190import org.jfree.chart.block.EntityBlockResult;
191import org.jfree.chart.block.LengthConstraintType;
192import org.jfree.chart.block.LineBorder;
193import org.jfree.chart.block.RectangleConstraint;
194import org.jfree.chart.entity.EntityCollection;
195import org.jfree.chart.entity.JFreeChartEntity;
196import org.jfree.chart.event.ChartChangeEvent;
197import org.jfree.chart.event.ChartChangeListener;
198import org.jfree.chart.event.ChartProgressEvent;
199import org.jfree.chart.event.ChartProgressListener;
200import org.jfree.chart.event.PlotChangeEvent;
201import org.jfree.chart.event.PlotChangeListener;
202import org.jfree.chart.event.TitleChangeEvent;
203import org.jfree.chart.event.TitleChangeListener;
204import org.jfree.chart.plot.CategoryPlot;
205import org.jfree.chart.plot.Plot;
206import org.jfree.chart.plot.PlotRenderingInfo;
207import org.jfree.chart.plot.XYPlot;
208import org.jfree.chart.title.LegendTitle;
209import org.jfree.chart.title.TextTitle;
210import org.jfree.chart.title.Title;
211import org.jfree.chart.util.ParamChecks;
212import org.jfree.chart.util.ResourceBundleWrapper;
213import org.jfree.data.Range;
214import org.jfree.io.SerialUtilities;
215import org.jfree.ui.Align;
216import org.jfree.ui.Drawable;
217import org.jfree.ui.HorizontalAlignment;
218import org.jfree.ui.RectangleEdge;
219import org.jfree.ui.RectangleInsets;
220import org.jfree.ui.Size2D;
221import org.jfree.ui.VerticalAlignment;
222import org.jfree.ui.about.Contributor;
223import org.jfree.ui.about.Licences;
224import org.jfree.ui.about.ProjectInfo;
225import org.jfree.util.ObjectUtilities;
226import org.jfree.util.PaintUtilities;
227
228/**
229 * A chart class implemented using the Java 2D APIs.  The current version
230 * supports bar charts, line charts, pie charts and xy plots (including time
231 * series data).
232 * <P>
233 * JFreeChart coordinates several objects to achieve its aim of being able to
234 * draw a chart on a Java 2D graphics device: a list of {@link Title} objects
235 * (which often includes the chart's legend), a {@link Plot} and a
236 * {@link org.jfree.data.general.Dataset} (the plot in turn manages a
237 * domain axis and a range axis).
238 * <P>
239 * You should use a {@link ChartPanel} to display a chart in a GUI.
240 * <P>
241 * The {@link ChartFactory} class contains static methods for creating
242 * 'ready-made' charts.
243 *
244 * @see ChartPanel
245 * @see ChartFactory
246 * @see Title
247 * @see Plot
248 */
249public class JFreeChart implements Drawable, TitleChangeListener,
250        PlotChangeListener, Serializable, Cloneable {
251
252    /** For serialization. */
253    private static final long serialVersionUID = -3470703747817429120L;
254
255    /** Information about the project. */
256    public static final ProjectInfo INFO = new JFreeChartInfo();
257
258    /** The default font for titles. */
259    public static final Font DEFAULT_TITLE_FONT
260            = new Font("SansSerif", Font.BOLD, 18);
261
262    /** The default background color. */
263    public static final Paint DEFAULT_BACKGROUND_PAINT
264            = UIManager.getColor("Panel.background");
265
266    /** The default background image. */
267    public static final Image DEFAULT_BACKGROUND_IMAGE = null;
268
269    /** The default background image alignment. */
270    public static final int DEFAULT_BACKGROUND_IMAGE_ALIGNMENT = Align.FIT;
271
272    /** The default background image alpha. */
273    public static final float DEFAULT_BACKGROUND_IMAGE_ALPHA = 0.5f;
274
275    /**
276     * The key for a rendering hint that can suppress the generation of a 
277     * shadow effect when drawing the chart.  The hint value must be a 
278     * Boolean.
279     * 
280     * @since 1.0.16
281     */
282    public static final RenderingHints.Key KEY_SUPPRESS_SHADOW_GENERATION
283            = new RenderingHints.Key(0) {
284        @Override
285        public boolean isCompatibleValue(Object val) {
286            return val instanceof Boolean;
287        }
288    };
289    
290    /**
291     * Rendering hints that will be used for chart drawing.  This should never
292     * be <code>null</code>.
293     */
294    private transient RenderingHints renderingHints;
295
296    /** A flag that controls whether or not the chart border is drawn. */
297    private boolean borderVisible;
298
299    /** The stroke used to draw the chart border (if visible). */
300    private transient Stroke borderStroke;
301
302    /** The paint used to draw the chart border (if visible). */
303    private transient Paint borderPaint;
304
305    /** The padding between the chart border and the chart drawing area. */
306    private RectangleInsets padding;
307
308    /** The chart title (optional). */
309    private TextTitle title;
310
311    /**
312     * The chart subtitles (zero, one or many).  This field should never be
313     * <code>null</code>.
314     */
315    private List subtitles;
316
317    /** Draws the visual representation of the data. */
318    private Plot plot;
319
320    /** Paint used to draw the background of the chart. */
321    private transient Paint backgroundPaint;
322
323    /** An optional background image for the chart. */
324    private transient Image backgroundImage;  // todo: not serialized yet
325
326    /** The alignment for the background image. */
327    private int backgroundImageAlignment = Align.FIT;
328
329    /** The alpha transparency for the background image. */
330    private float backgroundImageAlpha = 0.5f;
331
332    /** Storage for registered change listeners. */
333    private transient EventListenerList changeListeners;
334
335    /** Storage for registered progress listeners. */
336    private transient EventListenerList progressListeners;
337
338    /**
339     * A flag that can be used to enable/disable notification of chart change
340     * events.
341     */
342    private boolean notify;
343
344    /**
345     * Creates a new chart based on the supplied plot.  The chart will have
346     * a legend added automatically, but no title (although you can easily add
347     * one later).
348     * <br><br>
349     * Note that the  {@link ChartFactory} class contains a range
350     * of static methods that will return ready-made charts, and often this
351     * is a more convenient way to create charts than using this constructor.
352     *
353     * @param plot  the plot (<code>null</code> not permitted).
354     */
355    public JFreeChart(Plot plot) {
356        this(null, null, plot, true);
357    }
358
359    /**
360     * Creates a new chart with the given title and plot.  A default font
361     * ({@link #DEFAULT_TITLE_FONT}) is used for the title, and the chart will
362     * have a legend added automatically.
363     * <br><br>
364     * Note that the {@link ChartFactory} class contains a range
365     * of static methods that will return ready-made charts, and often this
366     * is a more convenient way to create charts than using this constructor.
367     *
368     * @param title  the chart title (<code>null</code> permitted).
369     * @param plot  the plot (<code>null</code> not permitted).
370     */
371    public JFreeChart(String title, Plot plot) {
372        this(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
373    }
374
375    /**
376     * Creates a new chart with the given title and plot.  The
377     * <code>createLegend</code> argument specifies whether or not a legend
378     * should be added to the chart.
379     * <br><br>
380     * Note that the  {@link ChartFactory} class contains a range
381     * of static methods that will return ready-made charts, and often this
382     * is a more convenient way to create charts than using this constructor.
383     *
384     * @param title  the chart title (<code>null</code> permitted).
385     * @param titleFont  the font for displaying the chart title
386     *                   (<code>null</code> permitted).
387     * @param plot  controller of the visual representation of the data
388     *              (<code>null</code> not permitted).
389     * @param createLegend  a flag indicating whether or not a legend should
390     *                      be created for the chart.
391     */
392    public JFreeChart(String title, Font titleFont, Plot plot,
393                      boolean createLegend) {
394
395        ParamChecks.nullNotPermitted(plot, "plot");
396
397        // create storage for listeners...
398        this.progressListeners = new EventListenerList();
399        this.changeListeners = new EventListenerList();
400        this.notify = true;  // default is to notify listeners when the
401                             // chart changes
402
403        this.renderingHints = new RenderingHints(
404                RenderingHints.KEY_ANTIALIASING,
405                RenderingHints.VALUE_ANTIALIAS_ON);
406        // added the following hint because of 
407        // http://stackoverflow.com/questions/7785082/
408        this.renderingHints.put(RenderingHints.KEY_STROKE_CONTROL,
409                RenderingHints.VALUE_STROKE_PURE);
410        
411        this.borderVisible = false;
412        this.borderStroke = new BasicStroke(1.0f);
413        this.borderPaint = Color.black;
414
415        this.padding = RectangleInsets.ZERO_INSETS;
416
417        this.plot = plot;
418        plot.addChangeListener(this);
419
420        this.subtitles = new ArrayList();
421
422        // create a legend, if requested...
423        if (createLegend) {
424            LegendTitle legend = new LegendTitle(this.plot);
425            legend.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
426            legend.setFrame(new LineBorder());
427            legend.setBackgroundPaint(Color.white);
428            legend.setPosition(RectangleEdge.BOTTOM);
429            this.subtitles.add(legend);
430            legend.addChangeListener(this);
431        }
432
433        // add the chart title, if one has been specified...
434        if (title != null) {
435            if (titleFont == null) {
436                titleFont = DEFAULT_TITLE_FONT;
437            }
438            this.title = new TextTitle(title, titleFont);
439            this.title.addChangeListener(this);
440        }
441
442        this.backgroundPaint = DEFAULT_BACKGROUND_PAINT;
443
444        this.backgroundImage = DEFAULT_BACKGROUND_IMAGE;
445        this.backgroundImageAlignment = DEFAULT_BACKGROUND_IMAGE_ALIGNMENT;
446        this.backgroundImageAlpha = DEFAULT_BACKGROUND_IMAGE_ALPHA;
447
448    }
449
450    /**
451     * Returns the collection of rendering hints for the chart.
452     *
453     * @return The rendering hints for the chart (never <code>null</code>).
454     *
455     * @see #setRenderingHints(RenderingHints)
456     */
457    public RenderingHints getRenderingHints() {
458        return this.renderingHints;
459    }
460
461    /**
462     * Sets the rendering hints for the chart.  These will be added (using the
463     * {@code Graphics2D.addRenderingHints()} method) near the start of the
464     * {@code JFreeChart.draw()} method.
465     *
466     * @param renderingHints  the rendering hints ({@code null} not permitted).
467     *
468     * @see #getRenderingHints()
469     */
470    public void setRenderingHints(RenderingHints renderingHints) {
471        ParamChecks.nullNotPermitted(renderingHints, "renderingHints");
472        this.renderingHints = renderingHints;
473        fireChartChanged();
474    }
475
476    /**
477     * Returns a flag that controls whether or not a border is drawn around the
478     * outside of the chart.
479     *
480     * @return A boolean.
481     *
482     * @see #setBorderVisible(boolean)
483     */
484    public boolean isBorderVisible() {
485        return this.borderVisible;
486    }
487
488    /**
489     * Sets a flag that controls whether or not a border is drawn around the
490     * outside of the chart.
491     *
492     * @param visible  the flag.
493     *
494     * @see #isBorderVisible()
495     */
496    public void setBorderVisible(boolean visible) {
497        this.borderVisible = visible;
498        fireChartChanged();
499    }
500
501    /**
502     * Returns the stroke used to draw the chart border (if visible).
503     *
504     * @return The border stroke.
505     *
506     * @see #setBorderStroke(Stroke)
507     */
508    public Stroke getBorderStroke() {
509        return this.borderStroke;
510    }
511
512    /**
513     * Sets the stroke used to draw the chart border (if visible).
514     *
515     * @param stroke  the stroke.
516     *
517     * @see #getBorderStroke()
518     */
519    public void setBorderStroke(Stroke stroke) {
520        this.borderStroke = stroke;
521        fireChartChanged();
522    }
523
524    /**
525     * Returns the paint used to draw the chart border (if visible).
526     *
527     * @return The border paint.
528     *
529     * @see #setBorderPaint(Paint)
530     */
531    public Paint getBorderPaint() {
532        return this.borderPaint;
533    }
534
535    /**
536     * Sets the paint used to draw the chart border (if visible).
537     *
538     * @param paint  the paint.
539     *
540     * @see #getBorderPaint()
541     */
542    public void setBorderPaint(Paint paint) {
543        this.borderPaint = paint;
544        fireChartChanged();
545    }
546
547    /**
548     * Returns the padding between the chart border and the chart drawing area.
549     *
550     * @return The padding (never <code>null</code>).
551     *
552     * @see #setPadding(RectangleInsets)
553     */
554    public RectangleInsets getPadding() {
555        return this.padding;
556    }
557
558    /**
559     * Sets the padding between the chart border and the chart drawing area,
560     * and sends a {@link ChartChangeEvent} to all registered listeners.
561     *
562     * @param padding  the padding (<code>null</code> not permitted).
563     *
564     * @see #getPadding()
565     */
566    public void setPadding(RectangleInsets padding) {
567        ParamChecks.nullNotPermitted(padding, "padding");
568        this.padding = padding;
569        notifyListeners(new ChartChangeEvent(this));
570    }
571
572    /**
573     * Returns the main chart title.  Very often a chart will have just one
574     * title, so we make this case simple by providing accessor methods for
575     * the main title.  However, multiple titles are supported - see the
576     * {@link #addSubtitle(Title)} method.
577     *
578     * @return The chart title (possibly <code>null</code>).
579     *
580     * @see #setTitle(TextTitle)
581     */
582    public TextTitle getTitle() {
583        return this.title;
584    }
585
586    /**
587     * Sets the main title for the chart and sends a {@link ChartChangeEvent}
588     * to all registered listeners.  If you do not want a title for the
589     * chart, set it to <code>null</code>.  If you want more than one title on
590     * a chart, use the {@link #addSubtitle(Title)} method.
591     *
592     * @param title  the title (<code>null</code> permitted).
593     *
594     * @see #getTitle()
595     */
596    public void setTitle(TextTitle title) {
597        if (this.title != null) {
598            this.title.removeChangeListener(this);
599        }
600        this.title = title;
601        if (title != null) {
602            title.addChangeListener(this);
603        }
604        fireChartChanged();
605    }
606
607    /**
608     * Sets the chart title and sends a {@link ChartChangeEvent} to all
609     * registered listeners.  This is a convenience method that ends up calling
610     * the {@link #setTitle(TextTitle)} method.  If there is an existing title,
611     * its text is updated, otherwise a new title using the default font is
612     * added to the chart.  If <code>text</code> is <code>null</code> the chart
613     * title is set to <code>null</code>.
614     *
615     * @param text  the title text (<code>null</code> permitted).
616     *
617     * @see #getTitle()
618     */
619    public void setTitle(String text) {
620        if (text != null) {
621            if (this.title == null) {
622                setTitle(new TextTitle(text, JFreeChart.DEFAULT_TITLE_FONT));
623            }
624            else {
625                this.title.setText(text);
626            }
627        }
628        else {
629            setTitle((TextTitle) null);
630        }
631    }
632
633    /**
634     * Adds a legend to the plot and sends a {@link ChartChangeEvent} to all
635     * registered listeners.
636     *
637     * @param legend  the legend (<code>null</code> not permitted).
638     *
639     * @see #removeLegend()
640     */
641    public void addLegend(LegendTitle legend) {
642        addSubtitle(legend);
643    }
644
645    /**
646     * Returns the legend for the chart, if there is one.  Note that a chart
647     * can have more than one legend - this method returns the first.
648     *
649     * @return The legend (possibly <code>null</code>).
650     *
651     * @see #getLegend(int)
652     */
653    public LegendTitle getLegend() {
654        return getLegend(0);
655    }
656
657    /**
658     * Returns the nth legend for a chart, or <code>null</code>.
659     *
660     * @param index  the legend index (zero-based).
661     *
662     * @return The legend (possibly <code>null</code>).
663     *
664     * @see #addLegend(LegendTitle)
665     */
666    public LegendTitle getLegend(int index) {
667        int seen = 0;
668        Iterator iterator = this.subtitles.iterator();
669        while (iterator.hasNext()) {
670            Title subtitle = (Title) iterator.next();
671            if (subtitle instanceof LegendTitle) {
672                if (seen == index) {
673                    return (LegendTitle) subtitle;
674                }
675                else {
676                    seen++;
677                }
678            }
679        }
680        return null;
681    }
682
683    /**
684     * Removes the first legend in the chart and sends a
685     * {@link ChartChangeEvent} to all registered listeners.
686     *
687     * @see #getLegend()
688     */
689    public void removeLegend() {
690        removeSubtitle(getLegend());
691    }
692
693    /**
694     * Returns the list of subtitles for the chart.
695     *
696     * @return The subtitle list (possibly empty, but never <code>null</code>).
697     *
698     * @see #setSubtitles(List)
699     */
700    public List getSubtitles() {
701        return new ArrayList(this.subtitles);
702    }
703
704    /**
705     * Sets the title list for the chart (completely replaces any existing
706     * titles) and sends a {@link ChartChangeEvent} to all registered
707     * listeners.
708     *
709     * @param subtitles  the new list of subtitles (<code>null</code> not
710     *                   permitted).
711     *
712     * @see #getSubtitles()
713     */
714    public void setSubtitles(List subtitles) {
715        if (subtitles == null) {
716            throw new NullPointerException("Null 'subtitles' argument.");
717        }
718        setNotify(false);
719        clearSubtitles();
720        Iterator iterator = subtitles.iterator();
721        while (iterator.hasNext()) {
722            Title t = (Title) iterator.next();
723            if (t != null) {
724                addSubtitle(t);
725            }
726        }
727        setNotify(true);  // this fires a ChartChangeEvent
728    }
729
730    /**
731     * Returns the number of titles for the chart.
732     *
733     * @return The number of titles for the chart.
734     *
735     * @see #getSubtitles()
736     */
737    public int getSubtitleCount() {
738        return this.subtitles.size();
739    }
740
741    /**
742     * Returns a chart subtitle.
743     *
744     * @param index  the index of the chart subtitle (zero based).
745     *
746     * @return A chart subtitle.
747     *
748     * @see #addSubtitle(Title)
749     */
750    public Title getSubtitle(int index) {
751        if ((index < 0) || (index >= getSubtitleCount())) {
752            throw new IllegalArgumentException("Index out of range.");
753        }
754        return (Title) this.subtitles.get(index);
755    }
756
757    /**
758     * Adds a chart subtitle, and notifies registered listeners that the chart
759     * has been modified.
760     *
761     * @param subtitle  the subtitle (<code>null</code> not permitted).
762     *
763     * @see #getSubtitle(int)
764     */
765    public void addSubtitle(Title subtitle) {
766        ParamChecks.nullNotPermitted(subtitle, "subtitle");
767        this.subtitles.add(subtitle);
768        subtitle.addChangeListener(this);
769        fireChartChanged();
770    }
771
772    /**
773     * Adds a subtitle at a particular position in the subtitle list, and sends
774     * a {@link ChartChangeEvent} to all registered listeners.
775     *
776     * @param index  the index (in the range 0 to {@link #getSubtitleCount()}).
777     * @param subtitle  the subtitle to add (<code>null</code> not permitted).
778     *
779     * @since 1.0.6
780     */
781    public void addSubtitle(int index, Title subtitle) {
782        if (index < 0 || index > getSubtitleCount()) {
783            throw new IllegalArgumentException(
784                    "The 'index' argument is out of range.");
785        }
786        ParamChecks.nullNotPermitted(subtitle, "subtitle");
787        this.subtitles.add(index, subtitle);
788        subtitle.addChangeListener(this);
789        fireChartChanged();
790    }
791
792    /**
793     * Clears all subtitles from the chart and sends a {@link ChartChangeEvent}
794     * to all registered listeners.
795     *
796     * @see #addSubtitle(Title)
797     */
798    public void clearSubtitles() {
799        Iterator iterator = this.subtitles.iterator();
800        while (iterator.hasNext()) {
801            Title t = (Title) iterator.next();
802            t.removeChangeListener(this);
803        }
804        this.subtitles.clear();
805        fireChartChanged();
806    }
807
808    /**
809     * Removes the specified subtitle and sends a {@link ChartChangeEvent} to
810     * all registered listeners.
811     *
812     * @param title  the title.
813     *
814     * @see #addSubtitle(Title)
815     */
816    public void removeSubtitle(Title title) {
817        this.subtitles.remove(title);
818        fireChartChanged();
819    }
820
821    /**
822     * Returns the plot for the chart.  The plot is a class responsible for
823     * coordinating the visual representation of the data, including the axes
824     * (if any).
825     *
826     * @return The plot.
827     */
828    public Plot getPlot() {
829        return this.plot;
830    }
831
832    /**
833     * Returns the plot cast as a {@link CategoryPlot}.
834     * <p>
835     * NOTE: if the plot is not an instance of {@link CategoryPlot}, then a
836     * <code>ClassCastException</code> is thrown.
837     *
838     * @return The plot.
839     *
840     * @see #getPlot()
841     */
842    public CategoryPlot getCategoryPlot() {
843        return (CategoryPlot) this.plot;
844    }
845
846    /**
847     * Returns the plot cast as an {@link XYPlot}.
848     * <p>
849     * NOTE: if the plot is not an instance of {@link XYPlot}, then a
850     * <code>ClassCastException</code> is thrown.
851     *
852     * @return The plot.
853     *
854     * @see #getPlot()
855     */
856    public XYPlot getXYPlot() {
857        return (XYPlot) this.plot;
858    }
859
860    /**
861     * Returns a flag that indicates whether or not anti-aliasing is used when
862     * the chart is drawn.
863     *
864     * @return The flag.
865     *
866     * @see #setAntiAlias(boolean)
867     */
868    public boolean getAntiAlias() {
869        Object val = this.renderingHints.get(RenderingHints.KEY_ANTIALIASING);
870        return RenderingHints.VALUE_ANTIALIAS_ON.equals(val);
871    }
872
873    /**
874     * Sets a flag that indicates whether or not anti-aliasing is used when the
875     * chart is drawn.
876     * <P>
877     * Anti-aliasing usually improves the appearance of charts, but is slower.
878     *
879     * @param flag  the new value of the flag.
880     *
881     * @see #getAntiAlias()
882     */
883    public void setAntiAlias(boolean flag) {
884        Object hint = flag ? RenderingHints.VALUE_ANTIALIAS_ON 
885                : RenderingHints.VALUE_ANTIALIAS_OFF;
886        this.renderingHints.put(RenderingHints.KEY_ANTIALIASING, hint);
887        fireChartChanged();
888    }
889
890    /**
891     * Returns the current value stored in the rendering hints table for
892     * {@link RenderingHints#KEY_TEXT_ANTIALIASING}.
893     *
894     * @return The hint value (possibly <code>null</code>).
895     *
896     * @since 1.0.5
897     *
898     * @see #setTextAntiAlias(Object)
899     */
900    public Object getTextAntiAlias() {
901        return this.renderingHints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
902    }
903
904    /**
905     * Sets the value in the rendering hints table for
906     * {@link RenderingHints#KEY_TEXT_ANTIALIASING} to either
907     * {@link RenderingHints#VALUE_TEXT_ANTIALIAS_ON} or
908     * {@link RenderingHints#VALUE_TEXT_ANTIALIAS_OFF}, then sends a
909     * {@link ChartChangeEvent} to all registered listeners.
910     *
911     * @param flag  the new value of the flag.
912     *
913     * @since 1.0.5
914     *
915     * @see #getTextAntiAlias()
916     * @see #setTextAntiAlias(Object)
917     */
918    public void setTextAntiAlias(boolean flag) {
919        if (flag) {
920            setTextAntiAlias(RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
921        }
922        else {
923            setTextAntiAlias(RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
924        }
925    }
926
927    /**
928     * Sets the value in the rendering hints table for
929     * {@link RenderingHints#KEY_TEXT_ANTIALIASING} and sends a
930     * {@link ChartChangeEvent} to all registered listeners.
931     *
932     * @param val  the new value (<code>null</code> permitted).
933     *
934     * @since 1.0.5
935     *
936     * @see #getTextAntiAlias()
937     * @see #setTextAntiAlias(boolean)
938     */
939    public void setTextAntiAlias(Object val) {
940        this.renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, val);
941        notifyListeners(new ChartChangeEvent(this));
942    }
943
944    /**
945     * Returns the paint used for the chart background.
946     *
947     * @return The paint (possibly <code>null</code>).
948     *
949     * @see #setBackgroundPaint(Paint)
950     */
951    public Paint getBackgroundPaint() {
952        return this.backgroundPaint;
953    }
954
955    /**
956     * Sets the paint used to fill the chart background and sends a
957     * {@link ChartChangeEvent} to all registered listeners.
958     *
959     * @param paint  the paint (<code>null</code> permitted).
960     *
961     * @see #getBackgroundPaint()
962     */
963    public void setBackgroundPaint(Paint paint) {
964
965        if (this.backgroundPaint != null) {
966            if (!this.backgroundPaint.equals(paint)) {
967                this.backgroundPaint = paint;
968                fireChartChanged();
969            }
970        }
971        else {
972            if (paint != null) {
973                this.backgroundPaint = paint;
974                fireChartChanged();
975            }
976        }
977
978    }
979
980    /**
981     * Returns the background image for the chart, or <code>null</code> if
982     * there is no image.
983     *
984     * @return The image (possibly <code>null</code>).
985     *
986     * @see #setBackgroundImage(Image)
987     */
988    public Image getBackgroundImage() {
989        return this.backgroundImage;
990    }
991
992    /**
993     * Sets the background image for the chart and sends a
994     * {@link ChartChangeEvent} to all registered listeners.
995     *
996     * @param image  the image (<code>null</code> permitted).
997     *
998     * @see #getBackgroundImage()
999     */
1000    public void setBackgroundImage(Image image) {
1001
1002        if (this.backgroundImage != null) {
1003            if (!this.backgroundImage.equals(image)) {
1004                this.backgroundImage = image;
1005                fireChartChanged();
1006            }
1007        }
1008        else {
1009            if (image != null) {
1010                this.backgroundImage = image;
1011                fireChartChanged();
1012            }
1013        }
1014
1015    }
1016
1017    /**
1018     * Returns the background image alignment. Alignment constants are defined
1019     * in the <code>org.jfree.ui.Align</code> class in the JCommon class
1020     * library.
1021     *
1022     * @return The alignment.
1023     *
1024     * @see #setBackgroundImageAlignment(int)
1025     */
1026    public int getBackgroundImageAlignment() {
1027        return this.backgroundImageAlignment;
1028    }
1029
1030    /**
1031     * Sets the background alignment.  Alignment options are defined by the
1032     * {@link org.jfree.ui.Align} class.
1033     *
1034     * @param alignment  the alignment.
1035     *
1036     * @see #getBackgroundImageAlignment()
1037     */
1038    public void setBackgroundImageAlignment(int alignment) {
1039        if (this.backgroundImageAlignment != alignment) {
1040            this.backgroundImageAlignment = alignment;
1041            fireChartChanged();
1042        }
1043    }
1044
1045    /**
1046     * Returns the alpha-transparency for the chart's background image.
1047     *
1048     * @return The alpha-transparency.
1049     *
1050     * @see #setBackgroundImageAlpha(float)
1051     */
1052    public float getBackgroundImageAlpha() {
1053        return this.backgroundImageAlpha;
1054    }
1055
1056    /**
1057     * Sets the alpha-transparency for the chart's background image.
1058     * Registered listeners are notified that the chart has been changed.
1059     *
1060     * @param alpha  the alpha value.
1061     *
1062     * @see #getBackgroundImageAlpha()
1063     */
1064    public void setBackgroundImageAlpha(float alpha) {
1065
1066        if (this.backgroundImageAlpha != alpha) {
1067            this.backgroundImageAlpha = alpha;
1068            fireChartChanged();
1069        }
1070
1071    }
1072
1073    /**
1074     * Returns a flag that controls whether or not change events are sent to
1075     * registered listeners.
1076     *
1077     * @return A boolean.
1078     *
1079     * @see #setNotify(boolean)
1080     */
1081    public boolean isNotify() {
1082        return this.notify;
1083    }
1084
1085    /**
1086     * Sets a flag that controls whether or not listeners receive
1087     * {@link ChartChangeEvent} notifications.
1088     *
1089     * @param notify  a boolean.
1090     *
1091     * @see #isNotify()
1092     */
1093    public void setNotify(boolean notify) {
1094        this.notify = notify;
1095        // if the flag is being set to true, there may be queued up changes...
1096        if (notify) {
1097            notifyListeners(new ChartChangeEvent(this));
1098        }
1099    }
1100
1101    /**
1102     * Draws the chart on a Java 2D graphics device (such as the screen or a
1103     * printer).
1104     * <P>
1105     * This method is the focus of the entire JFreeChart library.
1106     *
1107     * @param g2  the graphics device.
1108     * @param area  the area within which the chart should be drawn.
1109     */
1110    @Override
1111    public void draw(Graphics2D g2, Rectangle2D area) {
1112        draw(g2, area, null, null);
1113    }
1114
1115    /**
1116     * Draws the chart on a Java 2D graphics device (such as the screen or a
1117     * printer).  This method is the focus of the entire JFreeChart library.
1118     *
1119     * @param g2  the graphics device.
1120     * @param area  the area within which the chart should be drawn.
1121     * @param info  records info about the drawing (null means collect no info).
1122     */
1123    public void draw(Graphics2D g2, Rectangle2D area, ChartRenderingInfo info) {
1124        draw(g2, area, null, info);
1125    }
1126
1127    /**
1128     * Draws the chart on a Java 2D graphics device (such as the screen or a
1129     * printer).
1130     * <P>
1131     * This method is the focus of the entire JFreeChart library.
1132     *
1133     * @param g2  the graphics device.
1134     * @param chartArea  the area within which the chart should be drawn.
1135     * @param anchor  the anchor point (in Java2D space) for the chart
1136     *                (<code>null</code> permitted).
1137     * @param info  records info about the drawing (null means collect no info).
1138     */
1139    public void draw(Graphics2D g2, Rectangle2D chartArea, Point2D anchor,
1140             ChartRenderingInfo info) {
1141
1142        notifyListeners(new ChartProgressEvent(this, this,
1143                ChartProgressEvent.DRAWING_STARTED, 0));
1144        
1145        EntityCollection entities = null;
1146        // record the chart area, if info is requested...
1147        if (info != null) {
1148            info.clear();
1149            info.setChartArea(chartArea);
1150            entities = info.getEntityCollection();
1151        }
1152        if (entities != null) {
1153            entities.add(new JFreeChartEntity((Rectangle2D) chartArea.clone(),
1154                    this));
1155        }
1156
1157        // ensure no drawing occurs outside chart area...
1158        Shape savedClip = g2.getClip();
1159        g2.clip(chartArea);
1160
1161        g2.addRenderingHints(this.renderingHints);
1162
1163        // draw the chart background...
1164        if (this.backgroundPaint != null) {
1165            g2.setPaint(this.backgroundPaint);
1166            g2.fill(chartArea);
1167        }
1168
1169        if (this.backgroundImage != null) {
1170            Composite originalComposite = g2.getComposite();
1171            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
1172                    this.backgroundImageAlpha));
1173            Rectangle2D dest = new Rectangle2D.Double(0.0, 0.0,
1174                    this.backgroundImage.getWidth(null),
1175                    this.backgroundImage.getHeight(null));
1176            Align.align(dest, chartArea, this.backgroundImageAlignment);
1177            g2.drawImage(this.backgroundImage, (int) dest.getX(),
1178                    (int) dest.getY(), (int) dest.getWidth(),
1179                    (int) dest.getHeight(), null);
1180            g2.setComposite(originalComposite);
1181        }
1182
1183        if (isBorderVisible()) {
1184            Paint paint = getBorderPaint();
1185            Stroke stroke = getBorderStroke();
1186            if (paint != null && stroke != null) {
1187                Rectangle2D borderArea = new Rectangle2D.Double(
1188                        chartArea.getX(), chartArea.getY(),
1189                        chartArea.getWidth() - 1.0, chartArea.getHeight()
1190                        - 1.0);
1191                g2.setPaint(paint);
1192                g2.setStroke(stroke);
1193                g2.draw(borderArea);
1194            }
1195        }
1196
1197        // draw the title and subtitles...
1198        Rectangle2D nonTitleArea = new Rectangle2D.Double();
1199        nonTitleArea.setRect(chartArea);
1200        this.padding.trim(nonTitleArea);
1201
1202        if (this.title != null && this.title.isVisible()) {
1203            EntityCollection e = drawTitle(this.title, g2, nonTitleArea,
1204                    (entities != null));
1205            if (e != null && entities != null) {
1206                entities.addAll(e);
1207            }
1208        }
1209
1210        Iterator iterator = this.subtitles.iterator();
1211        while (iterator.hasNext()) {
1212            Title currentTitle = (Title) iterator.next();
1213            if (currentTitle.isVisible()) {
1214                EntityCollection e = drawTitle(currentTitle, g2, nonTitleArea,
1215                        (entities != null));
1216                if (e != null && entities != null) {
1217                    entities.addAll(e);
1218                }
1219            }
1220        }
1221
1222        Rectangle2D plotArea = nonTitleArea;
1223
1224        // draw the plot (axes and data visualisation)
1225        PlotRenderingInfo plotInfo = null;
1226        if (info != null) {
1227            plotInfo = info.getPlotInfo();
1228        }
1229        this.plot.draw(g2, plotArea, anchor, null, plotInfo);
1230
1231        g2.setClip(savedClip);
1232
1233        notifyListeners(new ChartProgressEvent(this, this,
1234                ChartProgressEvent.DRAWING_FINISHED, 100));
1235    }
1236
1237    /**
1238     * Creates a rectangle that is aligned to the frame.
1239     *
1240     * @param dimensions  the dimensions for the rectangle.
1241     * @param frame  the frame to align to.
1242     * @param hAlign  the horizontal alignment.
1243     * @param vAlign  the vertical alignment.
1244     *
1245     * @return A rectangle.
1246     */
1247    private Rectangle2D createAlignedRectangle2D(Size2D dimensions,
1248            Rectangle2D frame, HorizontalAlignment hAlign,
1249            VerticalAlignment vAlign) {
1250        double x = Double.NaN;
1251        double y = Double.NaN;
1252        if (hAlign == HorizontalAlignment.LEFT) {
1253            x = frame.getX();
1254        }
1255        else if (hAlign == HorizontalAlignment.CENTER) {
1256            x = frame.getCenterX() - (dimensions.width / 2.0);
1257        }
1258        else if (hAlign == HorizontalAlignment.RIGHT) {
1259            x = frame.getMaxX() - dimensions.width;
1260        }
1261        if (vAlign == VerticalAlignment.TOP) {
1262            y = frame.getY();
1263        }
1264        else if (vAlign == VerticalAlignment.CENTER) {
1265            y = frame.getCenterY() - (dimensions.height / 2.0);
1266        }
1267        else if (vAlign == VerticalAlignment.BOTTOM) {
1268            y = frame.getMaxY() - dimensions.height;
1269        }
1270
1271        return new Rectangle2D.Double(x, y, dimensions.width,
1272                dimensions.height);
1273    }
1274
1275    /**
1276     * Draws a title.  The title should be drawn at the top, bottom, left or
1277     * right of the specified area, and the area should be updated to reflect
1278     * the amount of space used by the title.
1279     *
1280     * @param t  the title (<code>null</code> not permitted).
1281     * @param g2  the graphics device (<code>null</code> not permitted).
1282     * @param area  the chart area, excluding any existing titles
1283     *              (<code>null</code> not permitted).
1284     * @param entities  a flag that controls whether or not an entity
1285     *                  collection is returned for the title.
1286     *
1287     * @return An entity collection for the title (possibly <code>null</code>).
1288     */
1289    protected EntityCollection drawTitle(Title t, Graphics2D g2,
1290                                         Rectangle2D area, boolean entities) {
1291
1292        ParamChecks.nullNotPermitted(t, "t");
1293        ParamChecks.nullNotPermitted(area, "area");
1294        Rectangle2D titleArea;
1295        RectangleEdge position = t.getPosition();
1296        double ww = area.getWidth();
1297        if (ww <= 0.0) {
1298            return null;
1299        }
1300        double hh = area.getHeight();
1301        if (hh <= 0.0) {
1302            return null;
1303        }
1304        RectangleConstraint constraint = new RectangleConstraint(ww,
1305                new Range(0.0, ww), LengthConstraintType.RANGE, hh,
1306                new Range(0.0, hh), LengthConstraintType.RANGE);
1307        Object retValue = null;
1308        BlockParams p = new BlockParams();
1309        p.setGenerateEntities(entities);
1310        if (position == RectangleEdge.TOP) {
1311            Size2D size = t.arrange(g2, constraint);
1312            titleArea = createAlignedRectangle2D(size, area,
1313                    t.getHorizontalAlignment(), VerticalAlignment.TOP);
1314            retValue = t.draw(g2, titleArea, p);
1315            area.setRect(area.getX(), Math.min(area.getY() + size.height,
1316                    area.getMaxY()), area.getWidth(), Math.max(area.getHeight()
1317                    - size.height, 0));
1318        }
1319        else if (position == RectangleEdge.BOTTOM) {
1320            Size2D size = t.arrange(g2, constraint);
1321            titleArea = createAlignedRectangle2D(size, area,
1322                    t.getHorizontalAlignment(), VerticalAlignment.BOTTOM);
1323            retValue = t.draw(g2, titleArea, p);
1324            area.setRect(area.getX(), area.getY(), area.getWidth(),
1325                    area.getHeight() - size.height);
1326        }
1327        else if (position == RectangleEdge.RIGHT) {
1328            Size2D size = t.arrange(g2, constraint);
1329            titleArea = createAlignedRectangle2D(size, area,
1330                    HorizontalAlignment.RIGHT, t.getVerticalAlignment());
1331            retValue = t.draw(g2, titleArea, p);
1332            area.setRect(area.getX(), area.getY(), area.getWidth()
1333                    - size.width, area.getHeight());
1334        }
1335
1336        else if (position == RectangleEdge.LEFT) {
1337            Size2D size = t.arrange(g2, constraint);
1338            titleArea = createAlignedRectangle2D(size, area,
1339                    HorizontalAlignment.LEFT, t.getVerticalAlignment());
1340            retValue = t.draw(g2, titleArea, p);
1341            area.setRect(area.getX() + size.width, area.getY(), area.getWidth()
1342                    - size.width, area.getHeight());
1343        }
1344        else {
1345            throw new RuntimeException("Unrecognised title position.");
1346        }
1347        EntityCollection result = null;
1348        if (retValue instanceof EntityBlockResult) {
1349            EntityBlockResult ebr = (EntityBlockResult) retValue;
1350            result = ebr.getEntityCollection();
1351        }
1352        return result;
1353    }
1354
1355    /**
1356     * Creates and returns a buffered image into which the chart has been drawn.
1357     *
1358     * @param width  the width.
1359     * @param height  the height.
1360     *
1361     * @return A buffered image.
1362     */
1363    public BufferedImage createBufferedImage(int width, int height) {
1364        return createBufferedImage(width, height, null);
1365    }
1366
1367    /**
1368     * Creates and returns a buffered image into which the chart has been drawn.
1369     *
1370     * @param width  the width.
1371     * @param height  the height.
1372     * @param info  carries back chart state information (<code>null</code>
1373     *              permitted).
1374     *
1375     * @return A buffered image.
1376     */
1377    public BufferedImage createBufferedImage(int width, int height,
1378                                             ChartRenderingInfo info) {
1379        return createBufferedImage(width, height, BufferedImage.TYPE_INT_ARGB,
1380                info);
1381    }
1382
1383    /**
1384     * Creates and returns a buffered image into which the chart has been drawn.
1385     *
1386     * @param width  the width.
1387     * @param height  the height.
1388     * @param imageType  the image type.
1389     * @param info  carries back chart state information (<code>null</code>
1390     *              permitted).
1391     *
1392     * @return A buffered image.
1393     */
1394    public BufferedImage createBufferedImage(int width, int height,
1395                                             int imageType,
1396                                             ChartRenderingInfo info) {
1397        BufferedImage image = new BufferedImage(width, height, imageType);
1398        Graphics2D g2 = image.createGraphics();
1399        draw(g2, new Rectangle2D.Double(0, 0, width, height), null, info);
1400        g2.dispose();
1401        return image;
1402    }
1403
1404    /**
1405     * Creates and returns a buffered image into which the chart has been drawn.
1406     *
1407     * @param imageWidth  the image width.
1408     * @param imageHeight  the image height.
1409     * @param drawWidth  the width for drawing the chart (will be scaled to
1410     *                   fit image).
1411     * @param drawHeight  the height for drawing the chart (will be scaled to
1412     *                    fit image).
1413     * @param info  optional object for collection chart dimension and entity
1414     *              information.
1415     *
1416     * @return A buffered image.
1417     */
1418    public BufferedImage createBufferedImage(int imageWidth,
1419                                             int imageHeight,
1420                                             double drawWidth,
1421                                             double drawHeight,
1422                                             ChartRenderingInfo info) {
1423
1424        BufferedImage image = new BufferedImage(imageWidth, imageHeight,
1425                BufferedImage.TYPE_INT_ARGB);
1426        Graphics2D g2 = image.createGraphics();
1427        double scaleX = imageWidth / drawWidth;
1428        double scaleY = imageHeight / drawHeight;
1429        AffineTransform st = AffineTransform.getScaleInstance(scaleX, scaleY);
1430        g2.transform(st);
1431        draw(g2, new Rectangle2D.Double(0, 0, drawWidth, drawHeight), null,
1432                info);
1433        g2.dispose();
1434        return image;
1435
1436    }
1437
1438    /**
1439     * Handles a 'click' on the chart.  JFreeChart is not a UI component, so
1440     * some other object (for example, {@link ChartPanel}) needs to capture
1441     * the click event and pass it onto the JFreeChart object.
1442     * If you are not using JFreeChart in a client application, then this
1443     * method is not required.
1444     *
1445     * @param x  x-coordinate of the click (in Java2D space).
1446     * @param y  y-coordinate of the click (in Java2D space).
1447     * @param info  contains chart dimension and entity information
1448     *              (<code>null</code> not permitted).
1449     */
1450    public void handleClick(int x, int y, ChartRenderingInfo info) {
1451
1452        // pass the click on to the plot...
1453        // rely on the plot to post a plot change event and redraw the chart...
1454        this.plot.handleClick(x, y, info.getPlotInfo());
1455
1456    }
1457
1458    /**
1459     * Registers an object for notification of changes to the chart.
1460     *
1461     * @param listener  the listener (<code>null</code> not permitted).
1462     *
1463     * @see #removeChangeListener(ChartChangeListener)
1464     */
1465    public void addChangeListener(ChartChangeListener listener) {
1466        ParamChecks.nullNotPermitted(listener, "listener");
1467        this.changeListeners.add(ChartChangeListener.class, listener);
1468    }
1469
1470    /**
1471     * Deregisters an object for notification of changes to the chart.
1472     *
1473     * @param listener  the listener (<code>null</code> not permitted)
1474     *
1475     * @see #addChangeListener(ChartChangeListener)
1476     */
1477    public void removeChangeListener(ChartChangeListener listener) {
1478        ParamChecks.nullNotPermitted(listener, "listener");
1479        this.changeListeners.remove(ChartChangeListener.class, listener);
1480    }
1481
1482    /**
1483     * Sends a default {@link ChartChangeEvent} to all registered listeners.
1484     * <P>
1485     * This method is for convenience only.
1486     */
1487    public void fireChartChanged() {
1488        ChartChangeEvent event = new ChartChangeEvent(this);
1489        notifyListeners(event);
1490    }
1491
1492    /**
1493     * Sends a {@link ChartChangeEvent} to all registered listeners.
1494     *
1495     * @param event  information about the event that triggered the
1496     *               notification.
1497     */
1498    protected void notifyListeners(ChartChangeEvent event) {
1499        if (this.notify) {
1500            Object[] listeners = this.changeListeners.getListenerList();
1501            for (int i = listeners.length - 2; i >= 0; i -= 2) {
1502                if (listeners[i] == ChartChangeListener.class) {
1503                    ((ChartChangeListener) listeners[i + 1]).chartChanged(
1504                            event);
1505                }
1506            }
1507        }
1508    }
1509
1510    /**
1511     * Registers an object for notification of progress events relating to the
1512     * chart.
1513     *
1514     * @param listener  the object being registered.
1515     *
1516     * @see #removeProgressListener(ChartProgressListener)
1517     */
1518    public void addProgressListener(ChartProgressListener listener) {
1519        this.progressListeners.add(ChartProgressListener.class, listener);
1520    }
1521
1522    /**
1523     * Deregisters an object for notification of changes to the chart.
1524     *
1525     * @param listener  the object being deregistered.
1526     *
1527     * @see #addProgressListener(ChartProgressListener)
1528     */
1529    public void removeProgressListener(ChartProgressListener listener) {
1530        this.progressListeners.remove(ChartProgressListener.class, listener);
1531    }
1532
1533    /**
1534     * Sends a {@link ChartProgressEvent} to all registered listeners.
1535     *
1536     * @param event  information about the event that triggered the
1537     *               notification.
1538     */
1539    protected void notifyListeners(ChartProgressEvent event) {
1540
1541        Object[] listeners = this.progressListeners.getListenerList();
1542        for (int i = listeners.length - 2; i >= 0; i -= 2) {
1543            if (listeners[i] == ChartProgressListener.class) {
1544                ((ChartProgressListener) listeners[i + 1]).chartProgress(event);
1545            }
1546        }
1547
1548    }
1549
1550    /**
1551     * Receives notification that a chart title has changed, and passes this
1552     * on to registered listeners.
1553     *
1554     * @param event  information about the chart title change.
1555     */
1556    @Override
1557    public void titleChanged(TitleChangeEvent event) {
1558        event.setChart(this);
1559        notifyListeners(event);
1560    }
1561
1562    /**
1563     * Receives notification that the plot has changed, and passes this on to
1564     * registered listeners.
1565     *
1566     * @param event  information about the plot change.
1567     */
1568    @Override
1569    public void plotChanged(PlotChangeEvent event) {
1570        event.setChart(this);
1571        notifyListeners(event);
1572    }
1573
1574    /**
1575     * Tests this chart for equality with another object.
1576     *
1577     * @param obj  the object (<code>null</code> permitted).
1578     *
1579     * @return A boolean.
1580     */
1581    @Override
1582    public boolean equals(Object obj) {
1583        if (obj == this) {
1584            return true;
1585        }
1586        if (!(obj instanceof JFreeChart)) {
1587            return false;
1588        }
1589        JFreeChart that = (JFreeChart) obj;
1590        if (!this.renderingHints.equals(that.renderingHints)) {
1591            return false;
1592        }
1593        if (this.borderVisible != that.borderVisible) {
1594            return false;
1595        }
1596        if (!ObjectUtilities.equal(this.borderStroke, that.borderStroke)) {
1597            return false;
1598        }
1599        if (!PaintUtilities.equal(this.borderPaint, that.borderPaint)) {
1600            return false;
1601        }
1602        if (!this.padding.equals(that.padding)) {
1603            return false;
1604        }
1605        if (!ObjectUtilities.equal(this.title, that.title)) {
1606            return false;
1607        }
1608        if (!ObjectUtilities.equal(this.subtitles, that.subtitles)) {
1609            return false;
1610        }
1611        if (!ObjectUtilities.equal(this.plot, that.plot)) {
1612            return false;
1613        }
1614        if (!PaintUtilities.equal(
1615            this.backgroundPaint, that.backgroundPaint
1616        )) {
1617            return false;
1618        }
1619        if (!ObjectUtilities.equal(this.backgroundImage,
1620                that.backgroundImage)) {
1621            return false;
1622        }
1623        if (this.backgroundImageAlignment != that.backgroundImageAlignment) {
1624            return false;
1625        }
1626        if (this.backgroundImageAlpha != that.backgroundImageAlpha) {
1627            return false;
1628        }
1629        if (this.notify != that.notify) {
1630            return false;
1631        }
1632        return true;
1633    }
1634
1635    /**
1636     * Provides serialization support.
1637     *
1638     * @param stream  the output stream.
1639     *
1640     * @throws IOException  if there is an I/O error.
1641     */
1642    private void writeObject(ObjectOutputStream stream) throws IOException {
1643        stream.defaultWriteObject();
1644        SerialUtilities.writeStroke(this.borderStroke, stream);
1645        SerialUtilities.writePaint(this.borderPaint, stream);
1646        SerialUtilities.writePaint(this.backgroundPaint, stream);
1647    }
1648
1649    /**
1650     * Provides serialization support.
1651     *
1652     * @param stream  the input stream.
1653     *
1654     * @throws IOException  if there is an I/O error.
1655     * @throws ClassNotFoundException  if there is a classpath problem.
1656     */
1657    private void readObject(ObjectInputStream stream)
1658        throws IOException, ClassNotFoundException {
1659        stream.defaultReadObject();
1660        this.borderStroke = SerialUtilities.readStroke(stream);
1661        this.borderPaint = SerialUtilities.readPaint(stream);
1662        this.backgroundPaint = SerialUtilities.readPaint(stream);
1663        this.progressListeners = new EventListenerList();
1664        this.changeListeners = new EventListenerList();
1665        this.renderingHints = new RenderingHints(
1666                RenderingHints.KEY_ANTIALIASING,
1667                RenderingHints.VALUE_ANTIALIAS_ON);
1668        this.renderingHints.put(RenderingHints.KEY_STROKE_CONTROL,
1669                RenderingHints.VALUE_STROKE_PURE);
1670        
1671        // register as a listener with sub-components...
1672        if (this.title != null) {
1673            this.title.addChangeListener(this);
1674        }
1675
1676        for (int i = 0; i < getSubtitleCount(); i++) {
1677            getSubtitle(i).addChangeListener(this);
1678        }
1679        this.plot.addChangeListener(this);
1680    }
1681
1682    /**
1683     * Prints information about JFreeChart to standard output.
1684     *
1685     * @param args  no arguments are honored.
1686     */
1687    public static void main(String[] args) {
1688        System.out.println(JFreeChart.INFO.toString());
1689    }
1690
1691    /**
1692     * Clones the object, and takes care of listeners.
1693     * Note: caller shall register its own listeners on cloned graph.
1694     *
1695     * @return A clone.
1696     *
1697     * @throws CloneNotSupportedException if the chart is not cloneable.
1698     */
1699    @Override
1700    public Object clone() throws CloneNotSupportedException {
1701        JFreeChart chart = (JFreeChart) super.clone();
1702
1703        chart.renderingHints = (RenderingHints) this.renderingHints.clone();
1704        // private boolean borderVisible;
1705        // private transient Stroke borderStroke;
1706        // private transient Paint borderPaint;
1707
1708        if (this.title != null) {
1709            chart.title = (TextTitle) this.title.clone();
1710            chart.title.addChangeListener(chart);
1711        }
1712
1713        chart.subtitles = new ArrayList();
1714        for (int i = 0; i < getSubtitleCount(); i++) {
1715            Title subtitle = (Title) getSubtitle(i).clone();
1716            chart.subtitles.add(subtitle);
1717            subtitle.addChangeListener(chart);
1718        }
1719
1720        if (this.plot != null) {
1721            chart.plot = (Plot) this.plot.clone();
1722            chart.plot.addChangeListener(chart);
1723        }
1724
1725        chart.progressListeners = new EventListenerList();
1726        chart.changeListeners = new EventListenerList();
1727        return chart;
1728    }
1729
1730}
1731
1732/**
1733 * Information about the JFreeChart project.  One instance of this class is
1734 * assigned to <code>JFreeChart.INFO</code>.
1735 */
1736class JFreeChartInfo extends ProjectInfo {
1737
1738    /**
1739     * Default constructor.
1740     */
1741    public JFreeChartInfo() {
1742
1743        // get a locale-specific resource bundle...
1744        String baseResourceClass
1745                = "org.jfree.chart.resources.JFreeChartResources";
1746        ResourceBundle resources = ResourceBundleWrapper.getBundle(
1747                baseResourceClass);
1748
1749        setName(resources.getString("project.name"));
1750        setVersion(resources.getString("project.version"));
1751        setInfo(resources.getString("project.info"));
1752        setCopyright(resources.getString("project.copyright"));
1753        setLogo(null);  // load only when required
1754        setLicenceName("LGPL");
1755        setLicenceText(Licences.getInstance().getLGPL());
1756
1757        setContributors(Arrays.asList(
1758            new Contributor[]{
1759                new Contributor("Eric Alexander", "-"),
1760                new Contributor("Richard Atkinson",
1761                        "richard_c_atkinson@ntlworld.com"),
1762                new Contributor("David Basten", "-"),
1763                new Contributor("David Berry", "-"),
1764                new Contributor("Chris Boek", "-"),
1765                new Contributor("Zoheb Borbora", "-"),
1766                new Contributor("Anthony Boulestreau", "-"),
1767                new Contributor("Jeremy Bowman", "-"),
1768                new Contributor("Nicolas Brodu", "-"),
1769                new Contributor("Jody Brownell", "-"),
1770                new Contributor("David Browning", "-"),
1771                new Contributor("Soren Caspersen", "-"),
1772                new Contributor("Chuanhao Chiu", "-"),
1773                new Contributor("Brian Cole", "-"),
1774                new Contributor("Pascal Collet", "-"),
1775                new Contributor("Martin Cordova", "-"),
1776                new Contributor("Paolo Cova", "-"),
1777                new Contributor("Greg Darke", "-"),
1778                new Contributor("Mike Duffy", "-"),
1779                new Contributor("Don Elliott", "-"),
1780                new Contributor("David Forslund", "-"),
1781                new Contributor("Jonathan Gabbai", "-"),
1782                new Contributor("David Gilbert",
1783                        "david.gilbert@object-refinery.com"),
1784                new Contributor("Serge V. Grachov", "-"),
1785                new Contributor("Daniel Gredler", "-"),
1786                new Contributor("Hans-Jurgen Greiner", "-"),
1787                new Contributor("Joao Guilherme Del Valle", "-"),
1788                new Contributor("Aiman Han", "-"),
1789                new Contributor("Cameron Hayne", "-"),
1790                new Contributor("Martin Hoeller", "-"),
1791                new Contributor("Jon Iles", "-"),
1792                new Contributor("Wolfgang Irler", "-"),
1793                new Contributor("Sergei Ivanov", "-"),
1794                new Contributor("Adriaan Joubert", "-"),
1795                new Contributor("Darren Jung", "-"),
1796                new Contributor("Xun Kang", "-"),
1797                new Contributor("Bill Kelemen", "-"),
1798                new Contributor("Norbert Kiesel", "-"),
1799                new Contributor("Peter Kolb", "-"),
1800                new Contributor("Gideon Krause", "-"),
1801                new Contributor("Pierre-Marie Le Biot", "-"),
1802                new Contributor("Arnaud Lelievre", "-"),
1803                new Contributor("Wolfgang Lenhard", "-"),
1804                new Contributor("David Li", "-"),
1805                new Contributor("Yan Liu", "-"),
1806                new Contributor("Tin Luu", "-"),
1807                new Contributor("Craig MacFarlane", "-"),
1808                new Contributor("Achilleus Mantzios", "-"),
1809                new Contributor("Thomas Meier", "-"),
1810                new Contributor("Jim Moore", "-"),
1811                new Contributor("Jonathan Nash", "-"),
1812                new Contributor("Barak Naveh", "-"),
1813                new Contributor("David M. O'Donnell", "-"),
1814                new Contributor("Krzysztof Paz", "-"),
1815                new Contributor("Eric Penfold", "-"),
1816                new Contributor("Tomer Peretz", "-"),
1817                new Contributor("Diego Pierangeli", "-"),
1818                new Contributor("Xavier Poinsard", "-"),
1819                new Contributor("Andrzej Porebski", "-"),
1820                new Contributor("Viktor Rajewski", "-"),
1821                new Contributor("Eduardo Ramalho", "-"),
1822                new Contributor("Michael Rauch", "-"),
1823                new Contributor("Cameron Riley", "-"),
1824                new Contributor("Klaus Rheinwald", "-"),
1825                new Contributor("Dan Rivett", "d.rivett@ukonline.co.uk"),
1826                new Contributor("Scott Sams", "-"),
1827                new Contributor("Michel Santos", "-"),
1828                new Contributor("Thierry Saura", "-"),
1829                new Contributor("Andreas Schneider", "-"),
1830                new Contributor("Jean-Luc SCHWAB", "-"),
1831                new Contributor("Bryan Scott", "-"),
1832                new Contributor("Tobias Selb", "-"),
1833                new Contributor("Darshan Shah", "-"),
1834                new Contributor("Mofeed Shahin", "-"),
1835                new Contributor("Michael Siemer", "-"),
1836                new Contributor("Pady Srinivasan", "-"),
1837                new Contributor("Greg Steckman", "-"),
1838                new Contributor("Gerald Struck", "-"),
1839                new Contributor("Roger Studner", "-"),
1840                new Contributor("Irv Thomae", "-"),
1841                new Contributor("Eric Thomas", "-"),
1842                new Contributor("Rich Unger", "-"),
1843                new Contributor("Daniel van Enckevort", "-"),
1844                new Contributor("Laurence Vanhelsuwe", "-"),
1845                new Contributor("Sylvain Vieujot", "-"),
1846                new Contributor("Ulrich Voigt", "-"),
1847                new Contributor("Jelai Wang", "-"),
1848                new Contributor("Mark Watson", "www.markwatson.com"),
1849                new Contributor("Alex Weber", "-"),
1850                new Contributor("Matthew Wright", "-"),
1851                new Contributor("Benoit Xhenseval", "-"),
1852                new Contributor("Christian W. Zuckschwerdt",
1853                        "Christian.Zuckschwerdt@Informatik.Uni-Oldenburg.de"),
1854                new Contributor("Hari", "-"),
1855                new Contributor("Sam (oldman)", "-"),
1856                new Contributor("Patrick Schlott", "-"),
1857                new Contributor("Christoph Schroeder", "-")
1858            }
1859        ));
1860
1861        addLibrary(JCommon.INFO);
1862
1863    }
1864
1865    /**
1866     * Returns the JFreeChart logo (a picture of a gorilla).
1867     *
1868     * @return The JFreeChart logo.
1869     */
1870    @Override
1871    public Image getLogo() {
1872        Image logo = super.getLogo();
1873        if (logo == null) {
1874            URL imageURL = this.getClass().getClassLoader().getResource(
1875                    "org/jfree/chart/gorilla.jpg");
1876            if (imageURL != null) {
1877                ImageIcon temp = new ImageIcon(imageURL);
1878                    // use ImageIcon because it waits for the image to load...
1879                logo = temp.getImage();
1880                setLogo(logo);
1881            }
1882        }
1883        return logo;
1884    }
1885
1886}