KickJava   Java API By Example, From Geeks To Geeks.

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


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  * A rectangle capable of automatic gradient paint, rounded corners on any single side,
34  * and warning region coloring.
35  */

36 final class FancyBar extends FancyShape {
37
38
39   private Rectangle2D.Float bounds, clipBounds;
40   private float arcw, arch;
41   private RoundRectangle2D.Float bar;
42   private FancyBar[] warningBars;
43   private int dataSign, baseValue;
44   private boolean needsUpdate;
45
46
47   /**
48    * Values for x, y, width, height, and roundSide needs to be set before use.
49    * Default rounding is squuare.
50    */

51   FancyBar() {
52     needsUpdate = true;
53   }
54
55
56   /**
57    * Sets the value at which the bar should not begin for that end should not be rounded.
58    * @param value The base value.
59    */

60   final void setBaseValue (int value) {
61     baseValue = value;
62     needsUpdate = true;
63   }
64
65
66   /**
67    * Gets the value at which the bar should not begin for that end should not be rounded.
68    * @return The base value.
69    */

70   final int getBaseValue() {
71     return baseValue;
72   }
73
74
75   /**
76    * Sets which kinds of the data is graphed.
77    * Uses fields of GraphArea.
78    * Use POS for non-negative data.
79    * Use NEG for non-positive data.
80    * Use MIX for both positive and negative data.
81    * @param sign The sign of the data.
82    */

83   final void setDataSign (int sign) {
84
85     dataSign = sign;
86     needsUpdate = true;
87   }
88
89
90   /**
91    * Gets which kinds of the data is graphed.
92    * Uses fields of GraphArea.
93    * Use POS for non-negative data.
94    * Use NEG for non-positive data.
95    * Use MIX for both positive and negative data.
96    * @return The sign of the data.
97    */

98   final int getDataSign() {
99     return dataSign;
100   }
101
102
103   /**
104    * Sets the bounds for this bar (specifying the size and location of the bar).
105    * @param b The bounds.
106    */

107   final void setBounds (Rectangle2D.Float b) {
108     bounds = b;
109     needsUpdate = true;
110   }
111
112
113   /**
114    * Sets the clip bounds for this bar (specifying what will show).
115    * @param b The bounds.
116    */

117   final void setClipBounds (Rectangle2D.Float b) {
118     clipBounds = b;
119     needsUpdate = true;
120   }
121
122
123   /**
124    * Sets the radius of the (width) arc for the bar rounding.
125    * @param w The width arc radius.
126    */

127   final void setArcw (float w) {
128     arcw = w;
129     needsUpdate = true;
130   }
131
132
133   /*
134    * Sets the radius of the (height) arc for the bar rounding.
135    * @param h The height arc radius.
136    */

137   final void setArch (float h) {
138     arch = h;
139     needsUpdate = true;
140   }
141
142
143   /**
144    * Gets the bounds for this bar (specifying the size and location of the bar).
145    * @return The bounds.
146    */

147   final Rectangle2D.Float getBounds() {
148     return bounds;
149   }
150
151
152   /**
153    * Gets the clip bounds for this bar (specifying what will show).
154    * @return The bounds.
155    */

156   final Rectangle2D.Float getClipBounds() {
157     return clipBounds;
158   }
159
160
161   /**
162    * Gets the radius of the (width) arc for the bar rounding.
163    * @return The width arc radius.
164    */

165   final float getArcw() {
166     return arcw;
167   }
168
169
170   /*
171    * Gets the radius of the (height) arc for the bar rounding.
172    * @return The height arc radius.
173    */

174   final float getArch() {
175     return arch;
176   }
177
178
179   /**
180    * Paints the bar on the Graphics2D object after calling update.
181    * @param g2D The Graphics2D object.
182    */

183   final void paint (Graphics2D g2D) {
184
185     update();
186
187     Paint oldPaint = g2D.getPaint();
188     g2D.setPaint (getPaint());
189
190     Shape oldClip = g2D.getClip();
191     java.awt.geom.Area JavaDoc clipArea = new java.awt.geom.Area JavaDoc (clipBounds);
192     clipArea.intersect (new java.awt.geom.Area JavaDoc (oldClip));
193     g2D.setClip (clipArea);
194
195     g2D.fill (bar);
196
197     if (getOutlineExistence() && bounds.width > 2f && bounds.height > 2f) {
198
199       g2D.setPaint (getOutlinePaint());
200       g2D.draw (bar);
201     }
202
203     g2D.setPaint (oldPaint);
204     g2D.setClip (oldClip);
205
206     if (getWarningRegions() != null) {
207       for (int i = 0; i < warningBars.length; ++i) warningBars[i].paint (g2D);
208     }
209   }
210
211
212   /**
213    * Updates the FancyBar.
214    */

215   final void update() {
216
217     super.update();
218
219     if (needsUpdate) {
220
221       if (getType() == LABELSLEFT) {
222
223         if (dataSign == GraphArea.POS) {
224           bar = new RoundRectangle2D.Float (
225             bounds.x - (arcw / 2f), bounds.y,
226             bounds.width + (arcw / 2f), bounds.height, arcw, arch);
227         }
228         else if (dataSign == GraphArea.NEG) {
229           bar = new RoundRectangle2D.Float (
230             bounds.x, bounds.y,
231             bounds.width + (arcw / 2f), bounds.height, arcw, arch);
232         }
233         else {
234
235           if (bounds.x >= baseValue) {
236             bar = new RoundRectangle2D.Float (
237               bounds.x - (arcw / 2f), bounds.y,
238               bounds.width + (arcw / 2f), bounds.height, arcw, arch);
239           }
240           else {
241             bar = new RoundRectangle2D.Float (
242               bounds.x, bounds.y,
243               bounds.width + (arcw / 2f), bounds.height, arcw, arch);
244           }
245         }
246       }
247       else { //LABELSBOTTOM
248

249         if (dataSign == GraphArea.POS) {
250           bar = new RoundRectangle2D.Float (
251             bounds.x, bounds.y,
252             bounds.width, bounds.height + (arch / 2f), arcw, arch);
253         }
254         else if (dataSign == GraphArea.NEG) {
255           bar = new RoundRectangle2D.Float (
256             bounds.x, bounds.y - (arch / 2f),
257             bounds.width, bounds.height + (arch / 2f), arcw, arch);
258         }
259         else {
260
261           if (baseValue <= bounds.y) {
262             bar = new RoundRectangle2D.Float (
263               bounds.x, bounds.y - (arch / 2f),
264               bounds.width, bounds.height + (arch / 2f), arcw, arch);
265           }
266           else {
267             bar = new RoundRectangle2D.Float (
268               bounds.x, bounds.y, bounds.width, bounds.height + (arch / 2f), arcw, arch);
269           }
270         }
271       }
272
273       if (getWarningRegions() != null) {
274
275         warningBars = new FancyBar[getWarningRegions().size()];
276         for (int i = 0; i < warningBars.length; ++i) {
277
278           FancyBar warningBar = new FancyBar();
279           warningBar.setBounds (bounds);
280           warningBar.setArcw (arcw);
281           warningBar.setArch (arch);
282           warningBar.setDataSign (dataSign);
283           warningBar.setBaseValue (baseValue);
284           warningBar.setWarningRegions (null);
285           warningBar.setLightBounds (getLightBounds());
286           warningBar.setLightSource (getLightSource());
287           warningBar.setOutlineExistence (getOutlineExistence());
288           warningBar.setOutlineColor (getOutlineColor());
289           warningBar.setType (getType());
290           warningBar.setGraphBounds (getGraphBounds());
291           WarningRegion warningRegion = (WarningRegion)getWarningRegions().get (i);
292           Rectangle2D tempRect2D = bounds.createIntersection (warningRegion.getBackgroundBounds());
293           Rectangle2D.Float clipBoundsWR = new Rectangle2D.Float();
294           clipBoundsWR.setRect (tempRect2D);
295           warningBar.setClipBounds (clipBoundsWR);
296           warningBar.setColor (warningRegion.getComponentColor());
297           warningBars[i] = warningBar;
298         }
299       }
300       needsUpdate = false;
301     }
302   }
303 }
Popular Tags