KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > Graphics2DPlotCanvas


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.Component JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.awt.Graphics2D JavaDoc;
25 import java.awt.Image JavaDoc;
26 import java.awt.RenderingHints JavaDoc;
27 import java.awt.geom.AffineTransform JavaDoc;
28 import java.awt.geom.NoninvertibleTransformException JavaDoc;
29
30 import jcckit.graphic.GraphPoint;
31 import jcckit.graphic.Renderer;
32 import jcckit.renderer.Graphics2DRenderer;
33 import jcckit.util.ConfigParameters;
34 import jcckit.util.Factory;
35
36 /**
37  * A subclass of {@link GraphicsPlotCanvas} for
38  * the {@link jcckit.renderer.Graphics2DRenderer}.
39  *
40  * @author Franz-Josef Elmer
41  */

42 public class Graphics2DPlotCanvas extends GraphicsPlotCanvas2 {
43   /** Key of a configuration parameter. */
44   public static final String JavaDoc ANTI_ALIASINGD_KEY = "antiAliasing";
45     
46   private static final AffineTransform JavaDoc IDENTITY = new AffineTransform JavaDoc();
47   
48   /**
49    * Painter which draw the plot into a <tt>Graphics2D</tt> instance.
50    *
51    * @author Franz-Josef Elmer
52    */

53   protected class Graphics2DPainter extends GraphicsPainter {
54     public Graphics2DPainter(Component JavaDoc component) {
55       super(component);
56     }
57
58     /**
59      * Sets identity transformation for the specified <tt>Graphics2D</tt>
60      * context.
61      */

62     protected void prepare(Graphics JavaDoc g) {
63       ((Graphics2D JavaDoc) g).setTransform(IDENTITY);
64     }
65
66     /**
67      * Creates an instance of {@link Graphics2DRenderer} for the
68      * specified <tt>Graphics2D</tt> context.
69      */

70     protected Renderer createRenderer(Graphics JavaDoc g) {
71       ((Graphics2D JavaDoc) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
72              _antiAliasing ? RenderingHints.VALUE_ANTIALIAS_ON
73                            : RenderingHints.VALUE_ANTIALIAS_OFF);
74       ((Graphics2D JavaDoc) g).setTransform(_transformation);
75       return ((Graphics2DRenderer) Factory.create(_renderer))
76                                                     .init((Graphics2D JavaDoc) g);
77     }
78     
79     protected void calculateTransformation(Dimension JavaDoc size) {
80       double pWidth = getPaper().getMaxX() - getPaper().getMinX();
81       double pHeight = getPaper().getMaxY() - getPaper().getMinY();
82       double scale = Math.min(size.width / pWidth, size.height / pHeight);
83       double x0 = 0.5 * getHorizontalAnchor().getFactor()
84           * (size.width - scale * pWidth) + scale * getPaper().getMinX();
85       double y0 = 0.5 * getVerticalAnchor().getFactor() * (scale * pHeight
86                                                            - size.height)
87                   + size.height + scale * getPaper().getMinY();
88       _transformation = new AffineTransform JavaDoc(scale, 0, 0, -scale, x0, y0);
89     }
90   }
91
92   protected class Graphics2DCanvas extends GraphicsCanvas {
93     public Graphics2DCanvas() {
94       super();
95       _painter = new Graphics2DPainter(this);
96     }
97   }
98
99   protected class Graphics2DJPanel extends GraphicsJPanel {
100     public Graphics2DJPanel() {
101       super();
102       _painter = new Graphics2DPainter(this);
103     }
104   }
105
106   protected String JavaDoc _renderer = "jcckit.renderer.Graphics2DRenderer";
107   private AffineTransform JavaDoc _transformation;
108   private final boolean _antiAliasing;
109   
110   /**
111    * Creates an instance from the specified configuration parameters.
112    * <table border=1 cellpadding=5>
113    * <tr><th>Key &amp; Default Value</th><th>Type</th><th>Mandatory</th>
114    * <th>Description</th></tr>
115    * <tr><td><tt>antiAliasing = true</tt></td>
116    * <td><tt>boolean</tt></td><td>no</td>
117    * <td>If <tt>true</tt> everything will be rendererd
118    * anti-aliasing.</td></tr>
119    * </table>
120    * In addition the configuration parameters of the
121    * <a HREF=
122    * "GraphicsPlotCanvas.html#GraphicsPlotCanvas(jcckit.util.ConfigParameters)"
123    * >s constructor</a> of the superclass {@link GraphicsPlotCanvas} apply.
124    */

125   public Graphics2DPlotCanvas(ConfigParameters config) {
126     super(config);
127     _antiAliasing = config.getBoolean(ANTI_ALIASINGD_KEY, true);
128     setRenderer("jcckit.renderer.Graphics2DRenderer");
129   }
130
131   /**
132    * Creates an instance of {@link Graphics2DCanvas}
133    * which wraps the rendered plot.
134    */

135   protected void createGraphicsCanvas() {
136     _canvas = new Graphics2DCanvas();
137   }
138   
139   /**
140    * Creates an instance of {@link Graphics2DJPanel}
141    * which wraps the rendered plot.
142    */

143   protected void createGraphicsJPanel() {
144     _jPanel = new Graphics2DJPanel();
145   }
146
147   /**
148    * Draws the plot into the specified image by using Java2D API.
149    * Can be used for off-screen renderering if double-buffering is switched
150    * off. Otherwise <tt>NullPointerException</tt> will be thrown.
151    */

152   public void draw2DInto(Image JavaDoc image)
153   {
154     _jPanel.setSize(image.getWidth(null), image.getHeight(null));
155     _jPanel.update(image.getGraphics());
156   }
157
158
159   /**
160    * Maps the cursor position onto a point in device-independent coordinates.
161    * @param x X-coordinate of the cursor.
162    * @param y Y-coordinate of the cursor.
163    */

164   public GraphPoint mapCursorPosition(int x, int y) {
165     double[] coordinates = new double[] {(double) x, (double) y, 0, 0};
166     try {
167       _transformation.inverseTransform(coordinates, 0, coordinates, 2, 1);
168     } catch (NoninvertibleTransformException JavaDoc e) {}
169     return new GraphPoint(coordinates[2], coordinates[3]);
170   }
171   
172   
173
174   /**
175    * Shows a plot in a <tt>Frame</tt>. The plot (data and layout) is
176    * defined in the <tt>.properties</tt> file specified by the first
177    * command line argument.
178    * <p>
179    * Usage: <tt>java jcckit.Graphics2DPlotCanvas [-r &lt;renderer class&gt;]
180    * &lt;<i>properties file</i>&gt;</tt>
181    */

182   public static void main(String JavaDoc[] args) throws Exception JavaDoc {
183     run(args, "jcckit.Graphics2DPlotCanvas");
184   }
185 }
186
Popular Tags