KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > GraphicsPlotCanvas2


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit;
20
21 import java.awt.Graphics JavaDoc;
22
23 import javax.swing.JPanel JavaDoc;
24
25 import jcckit.plot.PlotEvent;
26 import jcckit.util.ConfigParameters;
27
28 /**
29  * Java 2 aware extentions of {@link GraphicsPlotCanvas}.
30  *
31  * @author Franz-Josef Elmer
32  */

33 public class GraphicsPlotCanvas2 extends GraphicsPlotCanvas {
34   /** Wrapped Swing component. */
35   protected GraphicsJPanel _jPanel;
36
37   /**
38    * Swing component which actually shows the rendered plot.
39    *
40    * @author Franz-Josef Elmer
41    */

42   protected class GraphicsJPanel extends JPanel JavaDoc {
43     /** Painter which does the actual painting. */
44     protected GraphicsPainter _painter;
45     
46     /**
47      * Creates an instance with an appropriated
48      * {@link GraphicsPlotCanvas.GraphicsPainter}.
49      */

50     public GraphicsJPanel() {
51       super();
52       _painter = new GraphicsPainter(this);
53     }
54
55     /** Handles the event. Just invokes <tt>repaint()</tt>. */
56     public void handleEvent(PlotEvent event) {
57       repaint();
58     }
59     
60     public void paintComponent(Graphics JavaDoc g) {
61       super.paintComponent(g);
62       update(g);
63     }
64
65     public void update(Graphics JavaDoc g) {
66       _painter.paint(g);
67     }
68   }
69   
70   /**
71    * Creates an instance from the specified configuration parameters.
72    * The configuration parameters of the
73    * <a HREF=
74    * "GraphicsPlotCanvas.html#GraphicsPlotCanvas(jcckit.util.ConfigParameters)"
75    * >s constructor</a> of the superclass {@link GraphicsPlotCanvas} apply.
76    */

77   public GraphicsPlotCanvas2(ConfigParameters config) {
78     super(config);
79     createGraphicsJPanel();
80     _jPanel.setBackground(config.getColor(BACKGROUND_KEY,
81                                           _jPanel.getBackground()));
82     _jPanel.setForeground(config.getColor(FOREGROUND_KEY,
83                                           _jPanel.getForeground()));
84     _jPanel.setOpaque(true);
85   }
86
87   /**
88    * Creates an instance of {@link GraphicsJPanel}
89    * which wraps the rendered plot.
90    */

91   protected void createGraphicsJPanel() {
92     _jPanel = new GraphicsJPanel();
93   }
94
95   /** Repaints the wrapping GUI component. */
96   public void plotChanged(PlotEvent event) {
97     super.plotChanged(event);
98     _jPanel.handleEvent(event);
99   }
100
101   /**
102    * Returns a Swing component which wraps the rendered plot.
103    * Note that the returned <tt>JPanel</tt> object has preferred size zero.
104    * This is important to know when used with certain LayoutManagers like
105    * <tt>FlowLayout</tt>.
106    */

107   public JPanel JavaDoc getGraphicsJPanel() {
108     return _jPanel;
109   }
110 }
111
Popular Tags