001: /**
002: * Chart2D, a java library for drawing two dimensional charts.
003: * Copyright (C) 2001 Jason J. Simas
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * The author of this library may be contacted at:
019: * E-mail: jjsimas@users.sourceforge.net
020: * Street Address: J J Simas, 887 Tico Road, Ojai, CA 93023-3555 USA
021: */package net.sourceforge.chart2d;
022:
023: import java.awt.*;
024: import java.awt.geom.*;
025:
026: /**
027: * A structure for recording the region of the graph to be painted in a special way.
028: * The background of the graph will be painted with a particular color.
029: * All graph components intering the region of the graph will be painted with a particular color.
030: */
031: final class WarningRegion {
032:
033: /**
034: * Indicates the top of the graph.
035: */
036: static final float TOP = Float.POSITIVE_INFINITY;
037:
038: /**
039: * Indicates the bottom of the graph.
040: */
041: static final float BOTTOM = Float.NEGATIVE_INFINITY;
042:
043: /**
044: * Indicates the region is for a labels bottom graph.
045: */
046: static final int LABELS_BOTTOM = 0;
047:
048: /**
049: * Indicates the region is for a labels left graph.
050: */
051: static final int LABELS_LEFT = 1;
052:
053: private int graphSpaceX, graphSpaceY, graphSpaceWidth,
054: graphSpaceHeight;
055: private float high;
056: private float highGraph;
057: private float low;
058: private float lowGraph;
059: private int graphType;
060: private Color componentColor;
061: private boolean backgroundExistence;
062: private Color backgroundColor;
063: private Rectangle2D.Float background;
064: private boolean needsUpdate;
065:
066: /**
067: * Creates a new WarningRegion object.
068: */
069: WarningRegion() {
070: needsUpdate = true;
071: }
072:
073: /**
074: * Sets the x location of the graph area.
075: * @param x The x location.
076: */
077: final void setGraphSpaceX(int x) {
078: graphSpaceX = x;
079: needsUpdate = true;
080: }
081:
082: /**
083: * Sets the y location of the graph area.
084: * @param y The y location.
085: */
086: final void setGraphSpaceY(int y) {
087: graphSpaceY = y;
088: needsUpdate = true;
089: }
090:
091: /**
092: * Sets the width of the graph area.
093: * @param width The width of the graph area.
094: */
095: final void setGraphSpaceWidth(int w) {
096: graphSpaceWidth = w;
097: needsUpdate = true;
098: }
099:
100: /**
101: * Sets the height of the graph area.
102: * @param width The height of the graph area.
103: */
104: final void setGraphSpaceHeight(int h) {
105: graphSpaceHeight = h;
106: needsUpdate = true;
107: }
108:
109: /**
110: * Sets the high value from the data for this warning region.
111: * @param h The data high value.
112: */
113: final void setHigh(float h) {
114: high = h;
115: needsUpdate = true;
116: }
117:
118: /**
119: * Sets the high value of the graph for this warning region.
120: * @param h The graph high value.
121: */
122: final void setHighGraph(float h) {
123:
124: highGraph = h;
125: needsUpdate = true;
126: }
127:
128: /**
129: * Sets the low value from the data for this warning region.
130: * @param h The data low value.
131: */
132: final void setLow(float l) {
133: low = l;
134: needsUpdate = true;
135: }
136:
137: /**
138: * Sets the low value of the graph for this warning region.
139: * @param h The graph low value.
140: */
141: final void setLowGraph(float l) {
142: lowGraph = l;
143: needsUpdate = true;
144: }
145:
146: /**
147: * Sets the graph type for this warning region.
148: * Either GraphArea.LABELSLEFT or GraphArea.LABELSBOTTOM.
149: * @param t The type of graph.
150: */
151: final void setGraphType(int t) {
152: graphType = t;
153: needsUpdate = true;
154: }
155:
156: /**
157: * Sets the color of the portions of components that enter this region.
158: * @param c The color of the components.
159: */
160: final void setComponentColor(Color c) {
161: componentColor = c;
162: needsUpdate = true;
163: }
164:
165: /**
166: * Sets whether the background warning region is painted or not.
167: * @param e If true, the it will be painted.
168: */
169: final void setBackgroundExistence(boolean e) {
170: backgroundExistence = e;
171: needsUpdate = true;
172: }
173:
174: /**
175: * Sets the color of the portions of graph background that enter this region.
176: * @param c The background color.
177: */
178: final void setBackgroundColor(Color c) {
179: backgroundColor = c;
180: needsUpdate = true;
181: }
182:
183: /**
184: * Gets the x location of the graph area.
185: * @return The x location.
186: */
187: final int getGraphSpaceX() {
188: return graphSpaceX;
189: }
190:
191: /**
192: * Gets the y location of the graph area.
193: * @return The y location.
194: */
195: final int getGraphSpaceY() {
196: return graphSpaceY;
197: }
198:
199: /**
200: * Gets the width of the graph area.
201: * @return The width of the graph area.
202: */
203:
204: final int getGraphSpaceWidth() {
205: return graphSpaceWidth;
206: }
207:
208: /**
209: * Gets the height of the graph area.
210: * @return The height of the graph area.
211: */
212: final int getGraphSpaceHeight() {
213: return graphSpaceHeight;
214: }
215:
216: /**
217: * Gets the high value from the data for this warning region.
218: * @return The data high value.
219: */
220: final float getHigh() {
221: return high;
222: }
223:
224: /**
225: * Gets the high value of the graph for this warning region.
226: * @return The graph high value.
227: */
228: final float getHighGraph() {
229: return highGraph;
230: }
231:
232: /**
233: * Gets the low value from the data for this warning region.
234: * @return The data low value.
235: */
236: final float getLow() {
237: return low;
238: }
239:
240: /**
241: * Gets the low value of the graph for this warning region.
242: * @return The graph low value.
243: */
244: final float getLowGraph() {
245: return lowGraph;
246: }
247:
248: /**
249: * Gets the graph type for this warning region.
250: * Either GraphArea.LABELSLEFT or GraphArea.LABELSBOTTOM.
251: * @return The type of graph.
252: */
253: final int getGraphType() {
254: return graphType;
255: }
256:
257: /**
258: * Gets the color of the portions of components that enter this region.
259: * @return The color of the components.
260: */
261: final Color getComponentColor() {
262: return componentColor;
263: }
264:
265: /**
266: * Gets whether the background warning region is painted or not.
267: * @return If true, the it will be painted.
268: */
269: final boolean getBackgroundExistence() {
270: return backgroundExistence;
271: }
272:
273: /**
274: * Gets the color of the portions of graph background that enter this region.
275: * @return The background color.
276: */
277: final Color getBackgroundColor() {
278: return backgroundColor;
279: }
280:
281: /**
282: * Gets the calculated background bounds of this warning region.
283: * @return The warning region's background bounds.
284: */
285: final Rectangle2D.Float getBackgroundBounds() {
286: updateWarningRegion();
287: return background;
288: }
289:
290: /**
291: * Indicates whether some property of this class has changed.
292: * @return True if some property has changed.
293: */
294: final boolean getWarningRegionNeedsUpdate() {
295: return needsUpdate;
296: }
297:
298: /**
299: * Paints this region.
300: * Updates everything before painting.
301: * @param g2D The graphics context for calculations and painting.
302: */
303: final void paintComponent(Graphics2D g2D) {
304:
305: updateWarningRegion();
306: if (backgroundExistence) {
307: g2D.setColor(backgroundColor);
308: g2D.fill(background);
309: }
310: }
311:
312: /**
313: * Updates this warning region.
314: * Calling this methods assures they are all updates with respect to eachother.
315: */
316: final void updateWarningRegion() {
317:
318: if (getWarningRegionNeedsUpdate()) {
319:
320: needsUpdate = false;
321:
322: float x, y, width, height;
323: if (graphType == LABELS_BOTTOM) {
324:
325: x = graphSpaceX;
326: width = graphSpaceWidth;
327:
328: y = graphSpaceY + graphSpaceHeight - highGraph;
329: height = highGraph - lowGraph;
330: } else {
331:
332: y = graphSpaceY;
333: height = graphSpaceHeight;
334:
335: x = graphSpaceX + lowGraph;
336: width = highGraph - lowGraph;
337: }
338:
339: background = new Rectangle2D.Float(x, y, width, height);
340: }
341: }
342: }
|