KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > plot > ShapeAttributesHint


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.plot;
20
21 import java.awt.Color JavaDoc;
22
23 import jcckit.graphic.GraphicAttributes;
24 import jcckit.graphic.ShapeAttributes;
25 import jcckit.util.ConfigParameters;
26
27 /**
28  * An {@link AttributesHint} which wraps {@link ShapeAttributes}.
29  * Each call of {@link #getNextHint()} returns a new instance of
30  * <tt>ShapeAttributes</tt> where fill color, line color and/or
31  * line thickness has been increased by a constant amount.
32  *
33  * @author Franz-Josef Elmer
34  */

35 public class ShapeAttributesHint implements AttributesHint, Cloneable JavaDoc {
36   /** Configuration parameter key. */
37   public static final String JavaDoc INITIAL_ATTRIBUTES_KEY = "initialAttributes",
38                              FILL_COLOR_HSB_INCREMENT_KEY
39                                             = "fillColorHSBIncrement",
40                              LINE_COLOR_HSB_INCREMENT_KEY
41                                             = "lineColorHSBIncrement",
42                              LINE_THICKNESS_INCREMENT_KEY
43                                             = "lineThicknessIncrement";
44   private static float[] extractHSB(Color JavaDoc color) {
45     return color == null ? null
46         : Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(),
47                          null);
48   }
49   
50   private static Color JavaDoc createColor(float[] colorHSB) {
51     return colorHSB == null ? null
52         : Color.getHSBColor(colorHSB[0], colorHSB[1], colorHSB[2]);
53   }
54
55   private static float[] incrementColor(float[] colorHSB,
56                                         double[] increments) {
57     float[] result = null;
58     if (colorHSB != null) {
59       result = (float[]) colorHSB.clone();
60       for (int i = 0; i < 3; i++) {
61         result[i] += increments[i];
62       }
63     }
64     return result;
65   }
66
67   private float[] _fillColorHSB;
68   private float[] _lineColorHSB;
69   private double _lineThickness;
70   private double[] _linePattern;
71   private double[] _fillColorHSBIncrement;
72   private double[] _lineColorHSBIncrement;
73   private double _lineThicknessIncrement;
74
75   /**
76    * Creates an instance from the specified configuration parameters.
77    * <table border=1 cellpadding=5>
78    * <tr><th>Key &amp; Default Value</th><th>Type</th><th>Mandatory</th>
79    * <th>Description</th></tr>
80    * <tr><td><tt>initialAttributes = </tt><i>default values of
81    * {@link ShapeAttributes}</i></td>
82    * <td><tt>ConfigParameters</tt></td><td>no</td>
83    * <td>Initial values of shape attributes. Note, that default
84    * fill and line colors are undefined (they depend on the
85    * <tt>Renderer</tt>). In this case color increments have no effects.
86    * </td></tr>
87    * <tr><td><tt>fillColorHSBIncrement = 0 0 0</tt></td>
88    * <td><tt>double[]</tt></td><td>no</td>
89    * <td>Hue, saturation, and brightness increments of the fill color.
90    * </td></tr>
91    * <tr><td><tt>lineColorHSBIncrement = 0 0 0</tt></td>
92    * <td><tt>double[]</tt></td><td>no</td>
93    * <td>Hue, saturation, and brightness increments of the line color.
94    * </td></tr>
95    * <tr><td><tt>lineThicknessIncrement = 0</tt></td>
96    * <td><tt>double</tt></td><td>no</td>
97    * <td>Line thickness increment.</td></tr>
98    * </table>
99    */

100   public ShapeAttributesHint(ConfigParameters config) {
101     ShapeAttributes attributes
102         = new ShapeAttributes(config.getNode(INITIAL_ATTRIBUTES_KEY));
103     _fillColorHSB = extractHSB(attributes.getFillColor());
104     _lineColorHSB = extractHSB(attributes.getLineColor());
105     _lineThickness = attributes.getLineThickness();
106     _linePattern = attributes.getLinePattern();
107     
108     _fillColorHSBIncrement
109           = config.getDoubleArray(FILL_COLOR_HSB_INCREMENT_KEY, new double[3]);
110     _lineColorHSBIncrement
111           = config.getDoubleArray(LINE_COLOR_HSB_INCREMENT_KEY, new double[3]);
112     _lineThicknessIncrement
113           = config.getDouble(LINE_THICKNESS_INCREMENT_KEY, 0);
114   }
115   
116   /**
117    * Creates a new <tt>ShapeAttributesHint</tt> where all attributes has been
118    * incremented.
119    */

120   public AttributesHint getNextHint() {
121     ShapeAttributesHint nextHint = null;
122     try {
123       nextHint = (ShapeAttributesHint) clone();
124     } catch (CloneNotSupportedException JavaDoc e) {}
125     nextHint._fillColorHSB
126         = incrementColor(_fillColorHSB, _fillColorHSBIncrement);
127     nextHint._lineColorHSB
128         = incrementColor(_lineColorHSB, _lineColorHSBIncrement);
129     nextHint._lineThickness
130         = Math.max(0, _lineThickness + _lineThicknessIncrement);
131     return nextHint;
132   }
133
134   /** Returns the wrapped {@link ShapeAttributes} instance. */
135   public GraphicAttributes getAttributes() {
136     return new ShapeAttributes(createColor(_fillColorHSB),
137                                createColor(_lineColorHSB),
138                                _lineThickness, _linePattern);
139   }
140 }
141
Popular Tags