1 22 23 24 package net.sourceforge.chart2d; 25 26 27 import java.awt.*; 28 import java.util.*; 29 30 31 39 public final class MultiColorsProperties { 40 41 42 45 public static final int NATURAL = 0; 46 47 50 public static final int PASTEL = 1; 51 52 53 56 public static final int CHART2D = 2; 57 58 61 public static final boolean COLORS_CUSTOMIZE_DEFAULT = false; 62 63 66 public static final Color[] COLORS_CUSTOM_DEFAULT = new Color[0]; 67 68 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 91 public MultiColorsProperties() { 92 93 needsUpdate = true; 94 setMultiColorsPropertiesToDefaults(); 95 } 96 97 98 103 public MultiColorsProperties (MultiColorsProperties multiColorsProps) { 104 105 needsUpdate = true; 106 setMultiColorsProperties (multiColorsProps); 107 } 108 109 110 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 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 141 public final void setColorsCustomize (boolean cust) { 142 143 needsUpdate = true; 144 customize = cust; 145 } 146 147 148 153 public final void setColorsCustom (Color[] colors) { 154 155 needsUpdate = true; 156 customArray = colors; 157 } 158 159 160 165 public final void setColorsType (int type) { 166 167 needsUpdate = true; 168 colorsType = type; 169 } 170 171 172 177 public final boolean getColorsCustomize() { 178 return customize; 179 } 180 181 182 187 public final Color[] getColorsCustom() { 188 return customArray; 189 } 190 191 192 197 public final int getColorsType() { 198 return colorsType; 199 } 200 201 202 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 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 )needsUpdateVector.get (index)).booleanValue()) return true; 228 229 return false; 230 } 231 232 233 237 final void addObject2D (Object object2D) { 238 239 if (!object2DVector.contains (object2D)) { 240 object2DVector.add (object2D); 241 needsUpdateVector.add (new Boolean (true)); 242 } 243 } 244 245 246 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 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 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 (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 == 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 ( (int)(varyType * DEGREEVARIATION), 341 (int)(varyType * DEGREEVARIATION), 342 (int)(varyDegree * DEGREEVARIATION)); 343 if (++i == maxNum) break; 344 345 colorsArray[i] = new Color ( (int)(varyType * DEGREEVARIATION), 347 (int)(varyDegree * DEGREEVARIATION), 348 (int)(varyType * DEGREEVARIATION)); 349 if (++i == maxNum) break; 350 351 colorsArray[i] = new Color ( (int)(varyDegree * DEGREEVARIATION), 353 (int)(varyType * DEGREEVARIATION), 354 (int)(varyType * DEGREEVARIATION)); 355 if (++i == maxNum) break; 356 357 colorsArray[i] = new Color ( (int)(varyType * DEGREEVARIATION), 359 (int)(varyDegree * DEGREEVARIATION), 360 (int)(varyDegree * DEGREEVARIATION)); 361 if (++i == maxNum) break; 362 363 colorsArray[i] = new Color ( (int)(varyDegree * DEGREEVARIATION), 365 (int)(varyDegree * DEGREEVARIATION), 366 (int)(varyType * DEGREEVARIATION)); 367 if (++i == maxNum) break; 368 369 colorsArray[i] = new Color ( (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 (false)); 384 } 385 } 386 } 387 } | Popular Tags |