KickJava   Java API By Example, From Geeks To Geeks.

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


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.geom.*;
29 import java.util.*;
30
31
32 /**
33  * An abstract class holding the properties and logic shared by all fancy shapes.
34  */

35 abstract class FancyShape {
36
37
38   /**
39    * Indicates left.
40    */

41   static final int LEFT = 0;
42
43   /**
44    * Indicates right.
45    */

46   static final int RIGHT = 1;
47
48   /**
49    * Indicates top.
50    */

51   static final int TOP = 2;
52
53   /**
54    * Indicates bottom.
55    */

56   static final int BOTTOM = 3;
57
58   /**
59    * Indicates none.
60    */

61   static final int NONE = 6;
62
63   /**
64    * Indicates all.
65    */

66   static final int ALL = 7;
67
68   /**
69    * Indicates labels bottom.
70    */

71   static final int LABELSBOTTOM = 0;
72
73   /**
74    * Indicates labels left.
75    */

76   static final int LABELSLEFT = 1;
77
78
79   private Rectangle2D.Float lightBounds;
80   private int lightSource;
81   private Color color, outlineColor;
82   private Paint paint, outlinePaint;
83   private boolean outlineExistence;
84   private Vector warningRegions;
85   private Vector warningPaints;
86   private int type;
87   private Rectangle2D.Float graphBounds;
88   private boolean needsUpdate;
89
90
91   /**
92    * Creates a default FancyShape object.
93    */

94   FancyShape() {
95     needsUpdate = true;
96     warningPaints = new Vector (24, 5);
97   }
98
99
100   /**
101    * Sets the bounds for the lighting effect.
102    * Allows for single lighting effect to be applied to multiple FancyShape objects.
103    * @param b The lighting effect bounds.
104    */

105   final void setLightBounds (Rectangle2D.Float b) {
106     lightBounds = b;
107     needsUpdate = true;
108   }
109
110
111   /**
112    * Sets the source of light for the lighting effect.
113    * On the side of the source, the color of the shape is brighter.
114    * @param s The light source.
115    */

116   final void setLightSource (int s) {
117     lightSource = s;
118     needsUpdate = true;
119   }
120
121
122   /**
123    * Sets the color of the shape.
124    * @param c The color.
125    */

126   final void setColor (Color c) {
127     color = c;
128     needsUpdate = true;
129   }
130
131
132   /**
133    * Sets whether the shape is outlined by a one pixel curve.
134    * @param e If true, then outlined.
135    */

136   final void setOutlineExistence (boolean e) {
137     outlineExistence = e;
138     needsUpdate = true;
139   }
140
141
142   /**
143    * Sets the color of the shape's outline if it exists.
144    * @param c The outline color.
145    */

146   final void setOutlineColor (Color c) {
147     outlineColor = c;
148     needsUpdate = true;
149   }
150
151
152   /**
153    * Sets the type of the chart the shape is being used in.
154    * Uses fields LABELSLEFT and LABELSBOTTOM.
155    * @param t The type.
156    */

157   final void setType (int t) {
158     type = t;
159     needsUpdate = true;
160   }
161
162
163   /**
164    * Sets the vector of WarningRegion objects that affect this object's painting.
165    * @param r The WarningRegion objects vector.
166    */

167   final void setWarningRegions (Vector r) {
168     warningRegions = r;
169     needsUpdate = true;
170   }
171
172
173   /**
174    * Sets the bounds for of the graph, for clipping edges.
175    * @param b The graph bounds.
176    */

177   final void setGraphBounds (Rectangle2D.Float b) {
178     graphBounds = b;
179     needsUpdate = true;
180   }
181
182
183   /**
184    * Gets the bounds for the lighting effect.
185    * Allows for single lighting effect to be applied to multiple FancyShape objects.
186    * @return The lighting effect bounds.
187    */

188   final Rectangle2D.Float getLightBounds() {
189     return lightBounds;
190   }
191
192
193   /**
194    * Gets the source of light for the lighting effect.
195    * On the side of the source, the color of the shape is brighter.
196    * @return The light source.
197    */

198   final int getLightSource() {
199     return lightSource;
200   }
201
202
203   /**
204    * Gets the color of the shape.
205    * @return The color.
206    */

207   final Color getColor() {
208     return color;
209   }
210
211
212   /**
213    * Gets the color of the shape's outline if it exists.
214    * @return The outline color.
215    */

216   final Color getOutlineColor() {
217     return outlineColor;
218   }
219
220
221   /**
222    * Gets whether the shape is outlined by a one pixel curve.
223    * @return If true, then outlined.
224    */

225   final boolean getOutlineExistence() {
226     return outlineExistence;
227   }
228
229
230   /**
231    * Gets the type of the chart the shape is being used in.
232    * Uses fields LABELSLEFT and LABELSBOTTOM.
233    * @return The type.
234    */

235   final int getType() {
236     return type;
237   }
238
239
240   /**
241    * Gets the bounds for of the graph, for clipping edges.
242    * @return The graph bounds.
243    */

244   final Rectangle2D.Float getGraphBounds() {
245     return graphBounds;
246   }
247
248   /**
249    * Gets the vector of WarningRegion objects that affect this object's painting.
250    * @return The WarningRegion objects vector.
251    */

252   final Vector getWarningRegions() {
253     return warningRegions;
254   }
255
256
257   /**
258    * Gets the gradient paint for this shape.
259    * @return The gradient paint.
260    */

261   final Paint getPaint() {
262     update();
263     return paint;
264   }
265
266
267   /**
268    * Gets the outline paint for this shape.
269    * The outline paint is just a solid color.
270    * @return The outline paint.
271    */

272   final Paint getOutlinePaint() {
273     update();
274     return outlinePaint;
275   }
276
277
278   /**
279    * Gets the paints for the warning regions of this shape.
280    * @return The warning region paints.
281    */

282   final Vector getWarningPaints() {
283     update();
284     return warningPaints;
285   }
286
287
288   /**
289    * Gets whether a property has changed such that an update is needed.
290    * @return If true, then needs update.
291    */

292   final boolean getNeedsUpdate() {
293     return needsUpdate;
294   }
295
296
297   /**
298    * Updates the FancyShape.
299    */

300   void update() { //don't update warning regions here, do it in GraphArea...
301

302     if (getNeedsUpdate()) {
303
304       if (lightSource == TOP) {
305         paint = new GradientPaint (lightBounds.x, lightBounds.y, color.brighter(),
306           lightBounds.x, lightBounds.y + lightBounds.height, color);
307         if (outlineExistence) {
308           outlinePaint = new GradientPaint (lightBounds.x, lightBounds.y, outlineColor.brighter(),
309             lightBounds.x, lightBounds.y + lightBounds.height, outlineColor);
310         }
311         warningPaints.removeAllElements();
312         if (warningRegions != null) {
313           for (int i = 0; i < warningRegions.size(); ++i) {
314             Color warningColor = ((WarningRegion)warningRegions.get(i)).getComponentColor();
315             warningPaints.add (
316               new GradientPaint (lightBounds.x, lightBounds.y, warningColor.brighter(),
317               lightBounds.x, lightBounds.y + lightBounds.height, warningColor));
318           }
319         }
320       }
321       else if (lightSource == BOTTOM) {
322         paint = new GradientPaint (lightBounds.x, lightBounds.y, color,
323           lightBounds.x, lightBounds.y + lightBounds.height, color.brighter());
324         if (outlineExistence) {
325           outlinePaint = new GradientPaint (lightBounds.x, lightBounds.y, outlineColor,
326             lightBounds.x, lightBounds.y + lightBounds.height, outlineColor.brighter());
327         }
328         warningPaints.removeAllElements();
329         for (int i = 0; i < warningRegions.size(); ++i) {
330           Color warningColor = ((WarningRegion)warningRegions.get(i)).getComponentColor();
331           warningPaints.add (
332             new GradientPaint (lightBounds.x, lightBounds.y, warningColor,
333             lightBounds.x, lightBounds.y + lightBounds.height, warningColor.brighter()));
334         }
335       }
336       else if (lightSource == LEFT) {
337
338         paint = new GradientPaint (lightBounds.x, lightBounds.y, color.brighter(),
339           lightBounds.x + lightBounds.width, lightBounds.y, color);
340         if (outlineExistence) {
341           outlinePaint = new GradientPaint (lightBounds.x, lightBounds.y, outlineColor.brighter(),
342             lightBounds.x + lightBounds.width, lightBounds.y, outlineColor);
343         }
344         warningPaints.removeAllElements();
345         if (warningRegions != null) {
346           for (int i = 0; i < warningRegions.size(); ++i) {
347             Color warningColor = ((WarningRegion)warningRegions.get(i)).getComponentColor();
348             warningPaints.add (
349               new GradientPaint (lightBounds.x, lightBounds.y, warningColor.brighter(),
350               lightBounds.x + lightBounds.width, lightBounds.y, warningColor));
351           }
352         }
353       }
354       else if (lightSource == RIGHT) {
355         paint = new GradientPaint (lightBounds.x, lightBounds.y, color,
356           lightBounds.x + lightBounds.width, lightBounds.y, color.brighter());
357         if (outlineExistence) {
358           outlinePaint = new GradientPaint (lightBounds.x, lightBounds.y, outlineColor,
359             lightBounds.x + lightBounds.width, lightBounds.y, outlineColor.brighter());
360         }
361         warningPaints.removeAllElements();
362         if (warningRegions != null) {
363           for (int i = 0; i < warningRegions.size(); ++i) {
364             Color warningColor = ((WarningRegion)warningRegions.get(i)).getComponentColor();
365             warningPaints.add (
366               new GradientPaint (lightBounds.x, lightBounds.y, warningColor,
367               lightBounds.x + lightBounds.width, lightBounds.y, warningColor.brighter()));
368           }
369         }
370       }
371       else {
372         paint = color;
373         outlinePaint = outlineColor;
374         warningPaints.removeAllElements();
375         if (warningRegions != null) {
376           for (int i = 0; i < warningRegions.size(); ++i) {
377             Color warningColor = ((WarningRegion)warningRegions.get(i)).getComponentColor();
378             warningPaints.add (warningColor);
379           }
380         }
381       }
382       needsUpdate = false;
383     }
384   }
385 }
Popular Tags