KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chart2d > LLChart2D


1 /**
2  * Chart2D, a java library for drawing two dimensional charts.
3  * Copyright (C) 2001 Jason J. Simas
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * The author of this library may be contacted at:
19  * E-mail: jjsimas@users.sourceforge.net
20  * Street Address: J J Simas, 887 Tico Road, Ojai, CA 93023-3555 USA
21  */

22
23
24 package net.sourceforge.chart2d;
25
26
27 import java.awt.*;
28 import java.awt.image.*;
29
30
31 /**
32  * A class for the methods of GraphChart2D charts of the "Labels Left" type.
33  * A LLChart2D object is one with it's category descriptor labels along the left (or y) axis.
34  * For example, a horizontal bar chart is a LLChart2D type chart.
35  * Changes through its set methods are updated upon next repaint() or getImage() calls.
36  */

37 public final class LLChart2D extends GraphChart2D {
38
39
40   private boolean needsUpdate;
41   private LLChartArea chart;
42   private BufferedImage image;
43   private Dimension size;
44   private Dimension imageSize;
45   private Dimension prefSize;
46   private boolean customizePrefSize;
47   private Dimension customPrefSize;
48
49
50   /**
51    * Creates a LLChart2D object with its defaults.
52    */

53   public LLChart2D() {
54
55     needsUpdate = true;
56     chart = new LLChartArea();
57     size = new Dimension();
58     imageSize = new Dimension();
59     prefSize = null;
60     customizePrefSize = false;
61     customPrefSize = null;
62   }
63
64
65   /**
66    * Sets a custom preferred size for the chart.
67    * This custom size will override the preferred size calculations that normally occurr.
68    * If null is passed, the preferred size calculations will be reinstated.
69    * @param size The custom preferred size for this chart.
70    */

71   public final void setPreferredSize (Dimension size) {
72
73     needsUpdate = true;
74     customizePrefSize = size != null;
75     customPrefSize = size;
76     prefSize = null;
77   }
78
79
80   /**
81    * Gets a buffered image of the chart.
82    * @return An image of this chart
83    */

84   public final BufferedImage getImage() {
85
86     if (getSize().width <= 0 || getSize().height <= 0) pack();
87     else updateImage (getSize());
88
89     if (!chart.getBackgroundExistence()) {
90
91       Graphics2D imageG2D = image.createGraphics();
92       imageG2D.setRenderingHint (
93         RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
94       chart.paintComponent (imageG2D);
95       Point location = chart.getSizeLocation (chart.MIN);
96       imageSize = chart.getSize (chart.MIN);
97       image = image.getSubimage (location.x, location.y, imageSize.width, imageSize.height);
98     }
99
100     return image;
101   }
102
103
104   /**
105    * Gets the preferred size of the chart.
106    * The preferred size is within the maximum and minimum sizes of the chart.
107    * Much calculation is performed when calling this method.
108    * @return The preferred minimum size of the chart.
109    */

110   public final Dimension getPreferredSize() {
111
112     updateGraphChart2D();
113
114     if (!customizePrefSize) {
115
116       boolean autoModel = chart.getAutoSize (chart.MAXMODEL);
117       boolean autoMin = chart.getAutoSize (chart.MIN);
118       chart.setAutoSizes (true, false);
119       chart.resetLLChartAreaModel (true);
120       chart.setAutoSetLayoutRatios (true);
121       chart.setSize (chart.MAX, getMaximumSize());
122       BufferedImage image = new BufferedImage (
123         getMaximumSize().width, getMaximumSize().height, BufferedImage.TYPE_INT_BGR);
124       Graphics2D imageG2D = image.createGraphics();
125       prefSize = chart.getPrefSize (imageG2D);
126       chart.setAutoSizes (autoModel, autoMin);
127     }
128     else prefSize = customPrefSize;
129
130     int prefWidth =
131       prefSize.width < getMinimumSize().width ? getMinimumSize().width : prefSize.width;
132     int prefHeight =
133       prefSize.height < getMinimumSize().height ? getMinimumSize().height : prefSize.height;
134     prefSize.setSize (prefWidth, prefHeight);
135
136     this.size = prefSize;
137     chart.resetLLChartAreaModel (true);
138     chart.setAutoSetLayoutRatios (true);
139     chart.setSize (chart.MAX, size);
140     if (!chart.getBackgroundExistence()) {
141
142       image = new BufferedImage (
143           getMaximumSize().width, getMaximumSize().height, BufferedImage.TYPE_INT_BGR);
144       Graphics2D imageG2D = image.createGraphics();
145       imageG2D.setRenderingHint (
146         RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
147       chart.updateLLChartArea (imageG2D);
148     }
149     else {
150
151       image = new BufferedImage (size.width, size.height, BufferedImage.TYPE_INT_BGR);
152       Graphics2D imageG2D = image.createGraphics();
153       imageG2D.setRenderingHint (
154         RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
155       chart.paintComponent (imageG2D);
156       Point location = chart.getSizeLocation (chart.MIN);
157       imageSize = chart.getSize (chart.MIN);
158       image = image.getSubimage (location.x, location.y, imageSize.width, imageSize.height);
159     }
160
161     needsUpdate = false;
162
163     return prefSize;
164   }
165
166
167   /**
168    * Causes the object to reinintialize to it's preferred size.
169    */

170   public final void pack() {
171
172     needsUpdate = true;
173     setSize (getPreferredSize());
174   }
175
176
177   /**
178    * Validates the properties of this object.
179    * If debug is true then prints a messages indicating whether each property is valid.
180    * Returns true if all the properties were valid and false otherwise.
181    * @param debug If true then will print status messages.
182    * @return If true then valid.
183    */

184   public final boolean validate (boolean debug) {
185
186     if (debug) System.out.println ("Validating LLChart2D");
187
188     boolean valid = true;
189
190     if (!validateGraphChart2D (debug)) valid = false;
191
192     if (debug) {
193
194       if (valid) System.out.println ("LLChart2D was valid");
195       else System.out.println ("LLChart2D was invalid");
196     }
197
198     return valid;
199   }
200
201
202   /**
203    * Paints the chart.
204    * This is provided for the layout manager to call.
205    * @param g The graphics context for calculations and painting.
206    */

207   public final void paintComponent (Graphics g) {
208
209     super.paintComponent (g);
210     Graphics2D g2D = (Graphics2D)g;
211
212     updateImage (getSize());
213
214     if (!chart.getBackgroundExistence()) {
215
216       g2D.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
217       chart.paintComponent (g2D);
218     }
219     else g2D.drawImage (image, 0, 0, imageSize.width, imageSize.height, this);
220   }
221
222
223   /**
224    * Gets the LLChartArea of this LLChart2D.
225    * @return The LLChartArea of this LLChart2D.
226    */

227   final TitledArea getObjectArea() {
228     return chart;
229   }
230
231
232   /**
233    * Gets the chart type, LABELS_LEFT.
234    * @return The type of the chart, LABELS_LEFT.
235    */

236   final int getGraphChartType() {
237     return LABELS_LEFT;
238   }
239
240
241   private boolean getNeedsUpdate() {
242
243     return (needsUpdate || size.width != getSize().width || size.height != getSize().height ||
244       getNeedsUpdateGraphChart2D());
245   }
246
247
248   private void updateImage (Dimension size) {
249
250     if (prefSize == null) getPreferredSize();
251
252     if (getNeedsUpdate()) {
253
254       updateGraphChart2D();
255
256       this.size = size;
257       chart.setSize (chart.MAX, size);
258
259       if (!chart.getBackgroundExistence()) {
260
261         image = new BufferedImage (
262           getMaximumSize().width, getMaximumSize().height, BufferedImage.TYPE_INT_BGR);
263         Graphics2D imageG2D = image.createGraphics();
264         imageG2D.setRenderingHint (
265           RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
266         chart.updateLLChartArea (imageG2D);
267       }
268       else {
269
270         image = new BufferedImage (
271           size.width, size.height, BufferedImage.TYPE_INT_BGR);
272         Graphics2D imageG2D = image.createGraphics();
273         imageG2D.setRenderingHint (
274           RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
275         chart.paintComponent (imageG2D);
276         Point location = chart.getSizeLocation (chart.MIN);
277         imageSize = chart.getSize (chart.MIN);
278         image = image.getSubimage (location.x, location.y, imageSize.width, imageSize.height);
279       }
280
281       needsUpdate = false;
282     }
283   }
284 }
Popular Tags