KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
29
30
31 /**
32  * A data structure for holding the properties common to all sets of colors.
33  * Charting requires sets of colors (one color for each set for example).
34  * Color generation can be done manually or automatically.
35  * Even if manually, some automation adds convenience.
36  * A MultiColorsProperties takes care of the generation of color sets either way.
37  * Pass this to any number of color requiring objects.
38  */

39 public final class MultiColorsProperties {
40
41
42   /**
43    * NATURAL colors are more pure than PASTEL colors.
44    */

45   public static final int NATURAL = 0;
46
47   /**
48    * PASTEL colors have other colors besides their primary ones added to them.
49    */

50   public static final int PASTEL = 1;
51
52
53   /**
54    * CHART2D colors are a set of 12 colors selected by a graphic designer.
55    */

56   public static final int CHART2D = 2;
57
58   /**
59    * The default is false.
60    */

61   public static final boolean COLORS_CUSTOMIZE_DEFAULT = false;
62
63   /**
64    * The default is new Color[0].
65    */

66   public static final Color[] COLORS_CUSTOM_DEFAULT = new Color[0];
67
68   /**
69    * The default is CHART2D.
70    */

71   public static final int COLORS_TYPE_DEFAULT = CHART2D;
72
73
74   private static final int NUMCOLORS = 6;
75   private static final int NUMCOLORDEGREES = 2;
76   private static final int DEGREEVARIATION = 40;
77
78
79   private boolean customize;
80   private Color[] customArray;
81   private int colorsType;
82   private Color[] colorsArray;
83   private boolean needsUpdate;
84   private Vector object2DVector = new Vector (5, 5);
85   private Vector needsUpdateVector = new Vector (5, 5);
86
87
88   /**
89    * Creates a MultiColorsProperties object with the documented default values.
90    */

91   public MultiColorsProperties() {
92
93     needsUpdate = true;
94     setMultiColorsPropertiesToDefaults();
95   }
96
97
98   /**
99    * Creates a MultiColorsProperties object with property values copied from another object.
100    * The copying is a deep copy.
101    * @param multiColorsProps The properties to copy.
102    */

103   public MultiColorsProperties (MultiColorsProperties multiColorsProps) {
104
105     needsUpdate = true;
106     setMultiColorsProperties (multiColorsProps);
107   }
108
109
110   /**
111    * Sets all properties to their default values.
112    */

113   public final void setMultiColorsPropertiesToDefaults() {
114
115     needsUpdate = true;
116     setColorsCustomize (COLORS_CUSTOMIZE_DEFAULT);
117     setColorsCustom (COLORS_CUSTOM_DEFAULT);
118     setColorsType (COLORS_TYPE_DEFAULT);
119   }
120
121
122   /**
123    * Sets all properties to be the values of another MultiColorsProperties object.
124    * The copying is a deep copy.
125    * @param multiColorsProps The properties to copy.
126    */

127   public final void setMultiColorsProperties (MultiColorsProperties multiColorsProps) {
128
129     needsUpdate = true;
130     setColorsCustomize (multiColorsProps.getColorsCustomize());
131     setColorsCustom (multiColorsProps.getColorsCustom());
132     setColorsType (multiColorsProps.getColorsType());
133   }
134
135
136   /**
137    * Sets whether the colors are to be manual (custom) or to be automatically selected.
138    * If true, then use the setColorsCustom method to pass in an array of Color objects.
139    * @param cust If true, then manual colors are used.
140    */

141   public final void setColorsCustomize (boolean cust) {
142
143     needsUpdate = true;
144     customize = cust;
145   }
146
147
148   /**
149    * Sets the custom color array.
150    * To be used, the setColorsCustomize method must be set to true.
151    * @param colors The custom color array.
152    */

153   public final void setColorsCustom (Color[] colors) {
154
155     needsUpdate = true;
156     customArray = colors;
157   }
158
159
160   /**
161    * Sets the type of colors.
162    * Possible values are NATURAL and PASTEL.
163    * @param type The type of colors in the array.
164    */

165   public final void setColorsType (int type) {
166
167     needsUpdate = true;
168     colorsType = type;
169   }
170
171
172   /**
173    * Gets whether the colors are to be manual (custom) or to be automatically selected.
174    * If true, then use the setColorsCustom method to pass in an array of Color objects.
175    * @return If true, then manual colors are used.
176    */

177   public final boolean getColorsCustomize() {
178     return customize;
179   }
180
181
182   /**
183    * Gets the custom color array.
184    * To be used, the setColorsCustomize method must be set to true.
185    * @return The custom color array.
186    */

187   public final Color[] getColorsCustom() {
188     return customArray;
189   }
190
191
192   /**
193    * Gets the type of colors.
194    * Possible values are NATURAL and PASTEL.
195    * @return The type of colors in the array.
196    */

197   public final int getColorsType() {
198     return colorsType;
199   }
200
201
202   /**
203    * Gets an array of Colors of the length requested.
204    * @param length The number of colors requested.
205    * @return The color array.
206    */

207   public final Color[] getColorsArray (int length) {
208
209     updateObject2D (null);
210     Color[] colorsArrayOut = new Color[length];
211     for (int i = 0; i < length; ++i) colorsArrayOut[i] = colorsArray [i % colorsArray.length];
212     return colorsArrayOut;
213   }
214
215
216   /**
217    * Gets whether this object needs to be updated with new properties.
218    * @param object2D The object that may need to be updated.
219    * @return If true then needs update.
220    */

221   final boolean getObject2DNeedsUpdate (Object2D object2D) {
222
223     if (needsUpdate) return true;
224
225     int index = -1;
226     if ((index = object2DVector.indexOf (object2D)) != -1 &&
227       ((Boolean JavaDoc)needsUpdateVector.get (index)).booleanValue()) return true;
228
229     return false;
230   }
231
232
233   /**
234    * Adds an Object2D to the set of objects using these properties.
235    * @param object2D The Object2D to add.
236    */

237   final void addObject2D (Object JavaDoc object2D) {
238
239     if (!object2DVector.contains (object2D)) {
240       object2DVector.add (object2D);
241       needsUpdateVector.add (new Boolean JavaDoc (true));
242     }
243   }
244
245
246   /**
247    * Removes an Object2D object from the set of objects associated with this properties object.
248    * @param object2D The object to remove.
249    */

250   final void removeObject2D (Object2D object2D) {
251
252     int index = -1;
253     if ((index = object2DVector.indexOf (object2D)) != -1) {
254       object2DVector.remove (index);
255       needsUpdateVector.remove (index);
256     }
257   }
258
259
260   /**
261    * Validates the properties of this object.
262    * If debug is true then prints a messages indicating whether each property is valid.
263    * Returns true if all the properties were valid and false otherwise.
264    * @param debug If true then will print status messages.
265    * @return If true then valid.
266    */

267   final boolean validate (boolean debug) {
268
269     if (debug) System.out.println ("Validating MultiColorsProperties");
270
271     boolean valid = true;
272
273     if (customArray == null) {
274       valid = false;
275       if (debug) System.out.println ("CustomArray == null");
276     }
277     if (colorsType != NATURAL && colorsType != PASTEL && colorsType != CHART2D) {
278       valid = false;
279       if (debug) System.out.println ("Problem with ColorsType");
280     }
281
282     if (debug) {
283       if (valid) System.out.println ("MultiColorsProperties was valid");
284       else System.out.println ("MultiColorsProperties was invalid");
285     }
286
287     return (valid);
288   }
289
290
291   /**
292    * Updates the properties of this Object2D.
293    * @param object2D The object to update.
294    */

295   final void updateObject2D (Object2D object2D) {
296
297     if (object2D == null || getObject2DNeedsUpdate (object2D)) {
298
299       if (needsUpdate) {
300
301         for (int i = 0; i < needsUpdateVector.size(); ++i) {
302           needsUpdateVector.set (i, new Boolean JavaDoc (true));
303         }
304
305         if (customize) colorsArray = customArray;
306         else {
307
308           if (colorsType == CHART2D) {
309
310             colorsArray = new Color[12];
311             colorsArray[0] = new Color (000, 000, 102);
312             colorsArray[1] = new Color (051, 153, 102);
313             colorsArray[2] = new Color (204, 204, 102);
314             colorsArray[3] = new Color (153, 051, 102);
315             colorsArray[4] = new Color (051, 051, 153);
316             colorsArray[5] = new Color (000, 102, 051);
317             colorsArray[6] = new Color (153, 153, 051);
318             colorsArray[7] = new Color (204, 102, 153);
319             colorsArray[8] = new Color (102, 102, 204);
320             colorsArray[9] = new Color (102, 204, 153);
321             colorsArray[10] = new Color (102, 102, 000);
322             colorsArray[11] = new Color (102, 000, 051);
323           }
324           else {
325
326             int maxNum = NUMCOLORS * NUMCOLORDEGREES;
327             colorsArray = new Color[maxNum];
328             int i = 0;
329             while (i < maxNum) {
330
331               for (int j = 0; j < NUMCOLORDEGREES; j = (++j) % maxNum) {
332
333                 float varyDegree = 4.5f; //if j == 0
334
if (j == 1) {varyDegree = 3.5f;}
335                 else if (j == 2) {varyDegree = 2.5f;}
336
337                 float varyType = colorsType == NATURAL ? 0f : varyDegree - 1.5f;
338
339                 colorsArray[i] = new Color ( //blue
340
(int)(varyType * DEGREEVARIATION),
341                   (int)(varyType * DEGREEVARIATION),
342                   (int)(varyDegree * DEGREEVARIATION));
343                 if (++i == maxNum) break;
344
345                 colorsArray[i] = new Color ( //green
346
(int)(varyType * DEGREEVARIATION),
347                   (int)(varyDegree * DEGREEVARIATION),
348                   (int)(varyType * DEGREEVARIATION));
349                 if (++i == maxNum) break;
350
351                 colorsArray[i] = new Color ( //red
352
(int)(varyDegree * DEGREEVARIATION),
353                   (int)(varyType * DEGREEVARIATION),
354                   (int)(varyType * DEGREEVARIATION));
355                 if (++i == maxNum) break;
356
357                 colorsArray[i] = new Color ( //cyan
358
(int)(varyType * DEGREEVARIATION),
359                   (int)(varyDegree * DEGREEVARIATION),
360                   (int)(varyDegree * DEGREEVARIATION));
361                 if (++i == maxNum) break;
362
363                 colorsArray[i] = new Color ( //yellow
364
(int)(varyDegree * DEGREEVARIATION),
365                   (int)(varyDegree * DEGREEVARIATION),
366                   (int)(varyType * DEGREEVARIATION));
367                 if (++i == maxNum) break;
368
369                 colorsArray[i] = new Color ( //magenta
370
(int)(varyDegree * DEGREEVARIATION),
371                   (int)(varyType * DEGREEVARIATION),
372                   (int)(varyDegree * DEGREEVARIATION));
373                 if (++i == maxNum) break;
374               }
375             }
376           }
377         }
378         needsUpdate = false;
379       }
380
381       int index = -1;
382       if (object2D != null && (index = object2DVector.indexOf (object2D)) != -1) {
383         needsUpdateVector.set (index, new Boolean JavaDoc (false));
384       }
385     }
386   }
387 }
Popular Tags