001    /* PAVLOV -- Multiple Choice Study System
002     * Copyright (C) 2000 - 2004 T.J. Willis
003     *
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *
018     * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/pluglets/feedback/charts/ChartPanel.java,v 1.3 2004/05/10 15:22:10 tj_willis Exp $
019     *
020     *
021     * NOTE: This class is an interface between Pavlov and jCharts.
022     * jCharts is a registered trademark of Nathaniel G. Auvil
023     * see: http://jcharts.sourceforge.net
024     *      http://sourceforge.net/projects/jcharts
025     *
026     * jCharts includes software developed by the Apache Software Foundation 
027     * (http://www.apache.org/), namely the Apache XML Batik Project
028     */
029    package net.sourceforge.pavlov.pluglets.feedback.charts;
030    import org.jCharts.*;
031    
032    import javax.swing.*;
033    import java.awt.*;
034    
035    /**
036     * A Swing component to display JCharts that is resizable and repaints
037     * itself as necessary.
038     *
039     * @author <a href="mailto:"></a>
040     * @version 1.0
041     */
042    public class ChartPanel extends JLabel
043    {
044        private Chart chart;
045        private Graphics2D graf = null;
046        /**
047         * True when the chart's data has changed.
048         *
049         */
050        protected boolean chartChanged = true;
051    
052        /**
053         * Creates a new <code>ChartPanel</code> instance.
054         *
055         */
056        public ChartPanel( )
057        {
058            super();
059        }
060        
061        /**
062         * Describe <code>setChart</code> method here.
063         *
064         * @param ac a <code>Chart</code> value
065         */
066        public void setChart(Chart ac)
067        {
068            chart = ac;
069            chart.setGraphics2D( (Graphics2D) getGraphics() );
070            chartChanged = true;
071        }
072    
073        /**
074         * Delegates to this.paint(Graphics g).
075         *
076         */
077        public void repaint() {paint(this.getGraphics());}
078        /**
079         * Delegates to this.paint(Graphics2D g).
080         *
081         * @param g a <code>Graphics</code> value
082         */
083        public void paint(Graphics g){ paint( (Graphics2D)g); }
084    
085        /**
086         * Paints this component and rerenders the chart as necessary.
087         *
088         * @param g a <code>Graphics2D</code> value
089         */
090        public void paint(Graphics2D g)
091        {
092            //super.paintComponents(g);
093            //super.paint(g);
094            if(chart==null)
095                {
096                    super.paint(g);
097                    return;
098                }
099            try {
100                if(graf==null || graf!=g)
101                    {
102                        graf = g;
103                        chart.setGraphics2D( g );
104                    }
105                chart.render();
106                chartChanged = false;
107            }  catch (Exception e) {
108                e.printStackTrace(); 
109                graf=null;
110                chartChanged = true;
111            }
112        }
113     
114    }