KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.geom.*;
29 import java.util.*;
30
31
32 /**
33  * A line capable of gradient paint and warning region coloring.
34  */

35 final class FancyLine extends FancyShape {
36
37
38   private GeneralPath line;
39   private BasicStroke stroke;
40   private BasicStroke outlineStroke;
41   private float thickness;
42   private boolean fillArea;
43   private Rectangle2D.Float clipBounds;
44   private FancyLine[] warningLines;
45   private boolean needsUpdate;
46
47
48   /**
49    * Creates a default FancyLine object.
50    */

51   FancyLine() {
52     needsUpdate = true;
53     line = new GeneralPath (GeneralPath.WIND_EVEN_ODD);
54   }
55
56
57   /**
58    * Sets the thickness of this line.
59    * @param t The line thickness.
60    */

61   final void setThickness (float t) {
62     thickness = t;
63     needsUpdate = true;
64   }
65
66
67   /**
68    * Sets whether the bounds of this line intersected with the graph bounds will be filled.
69    * @param f If true, then filled.
70    */

71   final void setFillArea (boolean f) {
72     fillArea = f;
73   }
74
75
76   /**
77    * Sets the general path of this line.
78    * @param l The general path.
79    */

80   final void setLine (GeneralPath l) {
81     needsUpdate = true;
82     line = l;
83   }
84
85
86   /**
87    * Sets the clipping bounds (specifying which parts of the line to paint).
88    * @param b The clipping bounds.
89    */

90   final void setClipBounds (Rectangle2D.Float b) {
91     needsUpdate = true;
92     clipBounds = b;
93   }
94
95
96   /**
97    * Gets the thickness of this line.
98    * return The line thickness.
99    */

100   final float getThickness() {
101     return thickness;
102   }
103
104
105   /**
106    * Gets whether the bounds of this line intersected with the graph bounds will be filled.
107    * @return If true, then filled.
108    */

109   final boolean getFillArea() {
110     return fillArea;
111   }
112
113
114   /**
115    * Gets the general path of this line.
116    * @return The general path.
117    */

118   final GeneralPath getLine() {
119     return line;
120   }
121
122
123   /**
124    * Gets the clipping bounds (specifying which parts of the line to paint).
125    * @return The clipping bounds.
126    */

127   final Rectangle2D.Float getClipBounds() {
128     return clipBounds;
129   }
130
131
132   /**
133    * Paints the line on the Graphics2D object after calling update.
134    * @param g2D The Graphics2D object.
135    */

136   final void paint (Graphics2D g2D) {
137
138     update();
139
140     Stroke oldStroke = g2D.getStroke();
141     Paint oldPaint = g2D.getPaint();
142     Shape oldClip = g2D.getClip();
143
144     java.awt.geom.Area JavaDoc clipArea = new java.awt.geom.Area JavaDoc (clipBounds);
145     clipArea.intersect (new java.awt.geom.Area JavaDoc (oldClip));
146     g2D.setClip (clipArea);
147
148     if (getOutlineExistence() && thickness > 2f) {
149       g2D.setStroke (outlineStroke);
150       g2D.setPaint (getOutlinePaint());
151       g2D.draw (line);
152     }
153
154     g2D.setPaint (getPaint());
155     g2D.setStroke (stroke);
156     if (fillArea) g2D.fill (line);
157     else g2D.draw (line);
158
159     g2D.setPaint (oldPaint);
160     g2D.setClip (oldClip);
161     g2D.setStroke(oldStroke);
162
163     if (warningLines != null) {
164       for (int i = 0; i < warningLines.length; ++i) warningLines[i].paint (g2D);
165     }
166   }
167
168
169   /**
170    * Updates the FancyLine.
171    */

172   final void update() {
173
174     super.update();
175
176     if (needsUpdate) {
177
178       outlineStroke = new BasicStroke ((float)thickness,
179         BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10.0f, Area.CONTINUOUS, 0.0f);
180       float tempThickness = thickness > 2f && getOutlineExistence() ? thickness - 2f : thickness;
181       stroke = new BasicStroke ((float)tempThickness,
182           BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10.0f, Area.CONTINUOUS, 0.0f);
183
184       if (getWarningRegions() != null) {
185
186         warningLines = new FancyLine[getWarningRegions().size()];
187         for (int i = 0; i < warningLines.length; ++i) {
188
189           FancyLine warningLine = new FancyLine();
190
191           WarningRegion warningRegion = (WarningRegion)getWarningRegions().get(i);
192           java.awt.geom.Area JavaDoc clipArea = new java.awt.geom.Area JavaDoc (clipBounds);
193           java.awt.geom.Area JavaDoc wrArea =
194             new java.awt.geom.Area JavaDoc (warningRegion.getBackgroundBounds());
195           clipArea.intersect (wrArea);
196           Rectangle2D.Float wrClipBounds = new Rectangle2D.Float();
197           wrClipBounds.setRect (clipArea.getBounds2D());
198           warningLine.setClipBounds (wrClipBounds);
199           warningLine.setFillArea (getFillArea());
200           warningLine.setThickness (getThickness());
201           warningLine.setColor (warningRegion.getComponentColor());
202           warningLine.setLightBounds (getLightBounds());
203           warningLine.setLightSource (getLightSource());
204           warningLine.setOutlineColor (getOutlineColor());
205           warningLine.setOutlineExistence (getOutlineExistence());
206           warningLine.setType (getType());
207           warningLine.setWarningRegions (null);
208           warningLine.setLine (line);
209           warningLines[i] = warningLine;
210         }
211       }
212       needsUpdate = false;
213     }
214   }
215 }
Popular Tags