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: import java.util.*;
026:
027: /**
028: * An abstract class holding the properties and logic shared by all fancy shapes.
029: */
030: abstract class FancyShape {
031:
032: /**
033: * Indicates left.
034: */
035: static final int LEFT = 0;
036:
037: /**
038: * Indicates right.
039: */
040: static final int RIGHT = 1;
041:
042: /**
043: * Indicates top.
044: */
045: static final int TOP = 2;
046:
047: /**
048: * Indicates bottom.
049: */
050: static final int BOTTOM = 3;
051:
052: /**
053: * Indicates none.
054: */
055: static final int NONE = 6;
056:
057: /**
058: * Indicates all.
059: */
060: static final int ALL = 7;
061:
062: /**
063: * Indicates labels bottom.
064: */
065: static final int LABELSBOTTOM = 0;
066:
067: /**
068: * Indicates labels left.
069: */
070: static final int LABELSLEFT = 1;
071:
072: private Rectangle2D.Float lightBounds;
073: private int lightSource;
074: private Color color, outlineColor;
075: private Paint paint, outlinePaint;
076: private boolean outlineExistence;
077: private Vector warningRegions;
078: private Vector warningPaints;
079: private int type;
080: private Rectangle2D.Float graphBounds;
081: private boolean needsUpdate;
082:
083: /**
084: * Creates a default FancyShape object.
085: */
086: FancyShape() {
087: needsUpdate = true;
088: warningPaints = new Vector(24, 5);
089: }
090:
091: /**
092: * Sets the bounds for the lighting effect.
093: * Allows for single lighting effect to be applied to multiple FancyShape objects.
094: * @param b The lighting effect bounds.
095: */
096: final void setLightBounds(Rectangle2D.Float b) {
097: lightBounds = b;
098: needsUpdate = true;
099: }
100:
101: /**
102: * Sets the source of light for the lighting effect.
103: * On the side of the source, the color of the shape is brighter.
104: * @param s The light source.
105: */
106: final void setLightSource(int s) {
107: lightSource = s;
108: needsUpdate = true;
109: }
110:
111: /**
112: * Sets the color of the shape.
113: * @param c The color.
114: */
115: final void setColor(Color c) {
116: color = c;
117: needsUpdate = true;
118: }
119:
120: /**
121: * Sets whether the shape is outlined by a one pixel curve.
122: * @param e If true, then outlined.
123: */
124: final void setOutlineExistence(boolean e) {
125: outlineExistence = e;
126: needsUpdate = true;
127: }
128:
129: /**
130: * Sets the color of the shape's outline if it exists.
131: * @param c The outline color.
132: */
133: final void setOutlineColor(Color c) {
134: outlineColor = c;
135: needsUpdate = true;
136: }
137:
138: /**
139: * Sets the type of the chart the shape is being used in.
140: * Uses fields LABELSLEFT and LABELSBOTTOM.
141: * @param t The type.
142: */
143: final void setType(int t) {
144: type = t;
145: needsUpdate = true;
146: }
147:
148: /**
149: * Sets the vector of WarningRegion objects that affect this object's painting.
150: * @param r The WarningRegion objects vector.
151: */
152: final void setWarningRegions(Vector r) {
153: warningRegions = r;
154: needsUpdate = true;
155: }
156:
157: /**
158: * Sets the bounds for of the graph, for clipping edges.
159: * @param b The graph bounds.
160: */
161: final void setGraphBounds(Rectangle2D.Float b) {
162: graphBounds = b;
163: needsUpdate = true;
164: }
165:
166: /**
167: * Gets the bounds for the lighting effect.
168: * Allows for single lighting effect to be applied to multiple FancyShape objects.
169: * @return The lighting effect bounds.
170: */
171: final Rectangle2D.Float getLightBounds() {
172: return lightBounds;
173: }
174:
175: /**
176: * Gets the source of light for the lighting effect.
177: * On the side of the source, the color of the shape is brighter.
178: * @return The light source.
179: */
180: final int getLightSource() {
181: return lightSource;
182: }
183:
184: /**
185: * Gets the color of the shape.
186: * @return The color.
187: */
188: final Color getColor() {
189: return color;
190: }
191:
192: /**
193: * Gets the color of the shape's outline if it exists.
194: * @return The outline color.
195: */
196: final Color getOutlineColor() {
197: return outlineColor;
198: }
199:
200: /**
201: * Gets whether the shape is outlined by a one pixel curve.
202: * @return If true, then outlined.
203: */
204: final boolean getOutlineExistence() {
205: return outlineExistence;
206: }
207:
208: /**
209: * Gets the type of the chart the shape is being used in.
210: * Uses fields LABELSLEFT and LABELSBOTTOM.
211: * @return The type.
212: */
213: final int getType() {
214: return type;
215: }
216:
217: /**
218: * Gets the bounds for of the graph, for clipping edges.
219: * @return The graph bounds.
220: */
221: final Rectangle2D.Float getGraphBounds() {
222: return graphBounds;
223: }
224:
225: /**
226: * Gets the vector of WarningRegion objects that affect this object's painting.
227: * @return The WarningRegion objects vector.
228: */
229: final Vector getWarningRegions() {
230: return warningRegions;
231: }
232:
233: /**
234: * Gets the gradient paint for this shape.
235: * @return The gradient paint.
236: */
237: final Paint getPaint() {
238: update();
239: return paint;
240: }
241:
242: /**
243: * Gets the outline paint for this shape.
244: * The outline paint is just a solid color.
245: * @return The outline paint.
246: */
247: final Paint getOutlinePaint() {
248: update();
249: return outlinePaint;
250: }
251:
252: /**
253: * Gets the paints for the warning regions of this shape.
254: * @return The warning region paints.
255: */
256: final Vector getWarningPaints() {
257: update();
258: return warningPaints;
259: }
260:
261: /**
262: * Gets whether a property has changed such that an update is needed.
263: * @return If true, then needs update.
264: */
265: final boolean getNeedsUpdate() {
266: return needsUpdate;
267: }
268:
269: /**
270: * Updates the FancyShape.
271: */
272: void update() { //don't update warning regions here, do it in GraphArea...
273:
274: if (getNeedsUpdate()) {
275:
276: if (lightSource == TOP) {
277: paint = new GradientPaint(lightBounds.x, lightBounds.y,
278: color.brighter(), lightBounds.x, lightBounds.y
279: + lightBounds.height, color);
280: if (outlineExistence) {
281: outlinePaint = new GradientPaint(lightBounds.x,
282: lightBounds.y, outlineColor.brighter(),
283: lightBounds.x, lightBounds.y
284: + lightBounds.height, outlineColor);
285: }
286: warningPaints.removeAllElements();
287: if (warningRegions != null) {
288: for (int i = 0; i < warningRegions.size(); ++i) {
289: Color warningColor = ((WarningRegion) warningRegions
290: .get(i)).getComponentColor();
291: warningPaints.add(new GradientPaint(
292: lightBounds.x, lightBounds.y,
293: warningColor.brighter(), lightBounds.x,
294: lightBounds.y + lightBounds.height,
295: warningColor));
296: }
297: }
298: } else if (lightSource == BOTTOM) {
299: paint = new GradientPaint(lightBounds.x, lightBounds.y,
300: color, lightBounds.x, lightBounds.y
301: + lightBounds.height, color.brighter());
302: if (outlineExistence) {
303: outlinePaint = new GradientPaint(lightBounds.x,
304: lightBounds.y, outlineColor, lightBounds.x,
305: lightBounds.y + lightBounds.height,
306: outlineColor.brighter());
307: }
308: warningPaints.removeAllElements();
309: for (int i = 0; i < warningRegions.size(); ++i) {
310: Color warningColor = ((WarningRegion) warningRegions
311: .get(i)).getComponentColor();
312: warningPaints.add(new GradientPaint(lightBounds.x,
313: lightBounds.y, warningColor, lightBounds.x,
314: lightBounds.y + lightBounds.height,
315: warningColor.brighter()));
316: }
317: } else if (lightSource == LEFT) {
318:
319: paint = new GradientPaint(lightBounds.x, lightBounds.y,
320: color.brighter(), lightBounds.x
321: + lightBounds.width, lightBounds.y,
322: color);
323: if (outlineExistence) {
324: outlinePaint = new GradientPaint(lightBounds.x,
325: lightBounds.y, outlineColor.brighter(),
326: lightBounds.x + lightBounds.width,
327: lightBounds.y, outlineColor);
328: }
329: warningPaints.removeAllElements();
330: if (warningRegions != null) {
331: for (int i = 0; i < warningRegions.size(); ++i) {
332: Color warningColor = ((WarningRegion) warningRegions
333: .get(i)).getComponentColor();
334: warningPaints.add(new GradientPaint(
335: lightBounds.x, lightBounds.y,
336: warningColor.brighter(), lightBounds.x
337: + lightBounds.width,
338: lightBounds.y, warningColor));
339: }
340: }
341: } else if (lightSource == RIGHT) {
342: paint = new GradientPaint(lightBounds.x, lightBounds.y,
343: color, lightBounds.x + lightBounds.width,
344: lightBounds.y, color.brighter());
345: if (outlineExistence) {
346: outlinePaint = new GradientPaint(lightBounds.x,
347: lightBounds.y, outlineColor, lightBounds.x
348: + lightBounds.width, lightBounds.y,
349: outlineColor.brighter());
350: }
351: warningPaints.removeAllElements();
352: if (warningRegions != null) {
353: for (int i = 0; i < warningRegions.size(); ++i) {
354: Color warningColor = ((WarningRegion) warningRegions
355: .get(i)).getComponentColor();
356: warningPaints
357: .add(new GradientPaint(lightBounds.x,
358: lightBounds.y, warningColor,
359: lightBounds.x
360: + lightBounds.width,
361: lightBounds.y, warningColor
362: .brighter()));
363: }
364: }
365: } else {
366: paint = color;
367: outlinePaint = outlineColor;
368: warningPaints.removeAllElements();
369: if (warningRegions != null) {
370: for (int i = 0; i < warningRegions.size(); ++i) {
371: Color warningColor = ((WarningRegion) warningRegions
372: .get(i)).getComponentColor();
373: warningPaints.add(warningColor);
374: }
375: }
376: }
377: needsUpdate = false;
378: }
379: }
380: }
|