KickJava   Java API By Example, From Geeks To Geeks.

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


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
30
31 /**
32  * A structure for recording the region of the graph to be painted in a special way.
33  * The background of the graph will be painted with a particular color.
34  * All graph components intering the region of the graph will be painted with a particular color.
35  */

36 final class WarningRegion {
37
38   /**
39    * Indicates the top of the graph.
40    */

41   static final float TOP = Float.POSITIVE_INFINITY;
42
43   /**
44    * Indicates the bottom of the graph.
45    */

46   static final float BOTTOM = Float.NEGATIVE_INFINITY;
47
48   /**
49    * Indicates the region is for a labels bottom graph.
50    */

51   static final int LABELS_BOTTOM = 0;
52
53   /**
54    * Indicates the region is for a labels left graph.
55    */

56   static final int LABELS_LEFT = 1;
57
58
59   private int graphSpaceX, graphSpaceY, graphSpaceWidth, graphSpaceHeight;
60   private float high;
61   private float highGraph;
62   private float low;
63   private float lowGraph;
64   private int graphType;
65   private Color componentColor;
66   private boolean backgroundExistence;
67   private Color backgroundColor;
68   private Rectangle2D.Float background;
69   private boolean needsUpdate;
70
71
72   /**
73    * Creates a new WarningRegion object.
74    */

75   WarningRegion() {
76     needsUpdate = true;
77   }
78
79
80   /**
81    * Sets the x location of the graph area.
82    * @param x The x location.
83    */

84   final void setGraphSpaceX (int x) {
85     graphSpaceX = x;
86     needsUpdate = true;
87   }
88
89
90   /**
91    * Sets the y location of the graph area.
92    * @param y The y location.
93    */

94   final void setGraphSpaceY (int y) {
95     graphSpaceY = y;
96     needsUpdate = true;
97   }
98
99
100   /**
101    * Sets the width of the graph area.
102    * @param width The width of the graph area.
103    */

104   final void setGraphSpaceWidth (int w) {
105     graphSpaceWidth = w;
106     needsUpdate = true;
107   }
108
109
110   /**
111    * Sets the height of the graph area.
112    * @param width The height of the graph area.
113    */

114   final void setGraphSpaceHeight (int h) {
115     graphSpaceHeight = h;
116     needsUpdate = true;
117   }
118
119
120   /**
121    * Sets the high value from the data for this warning region.
122    * @param h The data high value.
123    */

124   final void setHigh (float h) {
125     high = h;
126     needsUpdate = true;
127   }
128
129
130   /**
131    * Sets the high value of the graph for this warning region.
132    * @param h The graph high value.
133    */

134   final void setHighGraph (float h) {
135
136     highGraph = h;
137     needsUpdate = true;
138   }
139
140
141   /**
142    * Sets the low value from the data for this warning region.
143    * @param h The data low value.
144    */

145   final void setLow (float l) {
146     low = l;
147     needsUpdate = true;
148   }
149
150
151   /**
152    * Sets the low value of the graph for this warning region.
153    * @param h The graph low value.
154    */

155   final void setLowGraph (float l) {
156     lowGraph = l;
157     needsUpdate = true;
158   }
159
160
161   /**
162    * Sets the graph type for this warning region.
163    * Either GraphArea.LABELSLEFT or GraphArea.LABELSBOTTOM.
164    * @param t The type of graph.
165    */

166   final void setGraphType (int t) {
167     graphType = t;
168     needsUpdate = true;
169   }
170
171
172   /**
173    * Sets the color of the portions of components that enter this region.
174    * @param c The color of the components.
175    */

176   final void setComponentColor (Color c) {
177     componentColor = c;
178     needsUpdate = true;
179   }
180
181
182   /**
183    * Sets whether the background warning region is painted or not.
184    * @param e If true, the it will be painted.
185    */

186   final void setBackgroundExistence (boolean e) {
187     backgroundExistence = e;
188     needsUpdate = true;
189   }
190
191
192   /**
193    * Sets the color of the portions of graph background that enter this region.
194    * @param c The background color.
195    */

196   final void setBackgroundColor (Color c) {
197     backgroundColor = c;
198     needsUpdate = true;
199   }
200
201
202   /**
203    * Gets the x location of the graph area.
204    * @return The x location.
205    */

206   final int getGraphSpaceX() {
207     return graphSpaceX;
208   }
209
210
211   /**
212    * Gets the y location of the graph area.
213    * @return The y location.
214    */

215   final int getGraphSpaceY() {
216     return graphSpaceY;
217   }
218
219
220   /**
221    * Gets the width of the graph area.
222    * @return The width of the graph area.
223    */

224
225   final int getGraphSpaceWidth() {
226     return graphSpaceWidth;
227   }
228
229
230   /**
231    * Gets the height of the graph area.
232    * @return The height of the graph area.
233    */

234   final int getGraphSpaceHeight() {
235     return graphSpaceHeight;
236   }
237
238
239   /**
240    * Gets the high value from the data for this warning region.
241    * @return The data high value.
242    */

243   final float getHigh() {
244     return high;
245   }
246
247
248   /**
249    * Gets the high value of the graph for this warning region.
250    * @return The graph high value.
251    */

252   final float getHighGraph() {
253     return highGraph;
254   }
255
256
257   /**
258    * Gets the low value from the data for this warning region.
259    * @return The data low value.
260    */

261   final float getLow() {
262     return low;
263   }
264
265
266   /**
267    * Gets the low value of the graph for this warning region.
268    * @return The graph low value.
269    */

270   final float getLowGraph() {
271     return lowGraph;
272   }
273
274
275   /**
276    * Gets the graph type for this warning region.
277    * Either GraphArea.LABELSLEFT or GraphArea.LABELSBOTTOM.
278    * @return The type of graph.
279    */

280   final int getGraphType() {
281     return graphType;
282   }
283
284
285   /**
286    * Gets the color of the portions of components that enter this region.
287    * @return The color of the components.
288    */

289   final Color getComponentColor() {
290     return componentColor;
291   }
292
293
294   /**
295    * Gets whether the background warning region is painted or not.
296    * @return If true, the it will be painted.
297    */

298   final boolean getBackgroundExistence() {
299     return backgroundExistence;
300   }
301
302
303   /**
304    * Gets the color of the portions of graph background that enter this region.
305    * @return The background color.
306    */

307   final Color getBackgroundColor() {
308     return backgroundColor;
309   }
310
311
312   /**
313    * Gets the calculated background bounds of this warning region.
314    * @return The warning region's background bounds.
315    */

316   final Rectangle2D.Float getBackgroundBounds() {
317     updateWarningRegion();
318     return background;
319   }
320
321
322
323   /**
324    * Indicates whether some property of this class has changed.
325    * @return True if some property has changed.
326    */

327   final boolean getWarningRegionNeedsUpdate() {
328     return needsUpdate;
329   }
330
331
332   /**
333    * Paints this region.
334    * Updates everything before painting.
335    * @param g2D The graphics context for calculations and painting.
336    */

337   final void paintComponent (Graphics2D g2D) {
338
339     updateWarningRegion();
340     if (backgroundExistence) {
341       g2D.setColor (backgroundColor);
342       g2D.fill (background);
343     }
344   }
345
346
347   /**
348    * Updates this warning region.
349    * Calling this methods assures they are all updates with respect to eachother.
350    */

351   final void updateWarningRegion() {
352
353     if (getWarningRegionNeedsUpdate()) {
354
355       needsUpdate = false;
356
357       float x, y, width, height;
358       if (graphType == LABELS_BOTTOM) {
359
360         x = graphSpaceX;
361         width = graphSpaceWidth;
362
363         y = graphSpaceY + graphSpaceHeight - highGraph;
364         height = highGraph - lowGraph;
365       }
366       else {
367
368         y = graphSpaceY;
369         height = graphSpaceHeight;
370
371         x = graphSpaceX + lowGraph;
372         width = highGraph - lowGraph;
373       }
374
375       background = new Rectangle2D.Float (x, y, width, height);
376     }
377   }
378 }
Popular Tags