KickJava   Java API By Example, From Geeks To Geeks.

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


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
29
30 /**
31  * Maintains a font to be used within an area. Contains all variables for the
32  * font, such as point, name, style, and color. Builds a font from these
33  * variable. When building font, uses the lesser ratio of the area class.
34  * This ensures proper growing and shrinking of the font respective to the
35  * changing in the area's size. Grows and shrinks if and only if max model
36  * area auto size is disabled.
37  */

38 class FontArea extends Area {
39
40
41   private int fontPointModel;
42   private String JavaDoc fontName;
43   private int fontStyle;
44   private Color fontColor;
45   private Font font;
46   private boolean needsUpdate;
47
48
49   /**
50    * Builds a new FontArea with the default values.
51    */

52   FontArea () {
53
54     setFontPointModel (12);
55     setFontName ("SansSerif");
56     setFontStyle (Font.PLAIN);
57     setFontColor (Color.black);
58     resetFontAreaModel (true);
59     needsUpdate = true;
60   }
61
62
63   /**
64    * Changes the model font's point size. This is the size that the font would
65    * be if the maximum size of the area was equal to the model size of the area.
66    * Otherwise, a ratio based on maximum size / model size is taken and applied
67    * to this point, producing the actual font point.
68    * @param p The new model font point.
69    */

70   final void setFontPointModel (int p) {
71
72     needsUpdate = true;
73     fontPointModel = p;
74   }
75
76
77   /**
78    * Changes the name of this font. This accepts the same values as
79    * the Font classes constructor
80    * <code>Font (String name, int style, int point)</code>.
81    * @param n Then new name of the font.
82    */

83   final void setFontName (String JavaDoc n) {
84
85     needsUpdate = true;
86     fontName = n;
87   }
88
89
90   /**
91    * Changes the style of this font. This accepts the same values as
92    * the Font classes constructor
93    * <code>Font (String name, int style, int point)</code>.
94    * @param s Then new style of the font.
95    */

96   final void setFontStyle (int s) {
97
98     needsUpdate = true;
99     fontStyle = s;
100   }
101
102
103   /**
104    * Changes the color of this font. This accepts all Color.* compatible
105    * values.
106    * @param c Then new color of the font.
107    */

108   final void setFontColor (Color c) {
109
110     needsUpdate = true;
111     fontColor = c;
112   }
113
114
115   /**
116    * Returns the model font's point; not the actual font point.
117    * @return The model font's point.
118    */

119   final int getFontPointModel() {
120
121     return fontPointModel;
122   }
123
124
125   /**
126    * Returns the name of this font.
127    * @return Then name of the font.
128    */

129   final String JavaDoc getFontName() {
130
131     return fontName;
132   }
133
134
135   /**
136    * Returns the style of this font.
137    * @return Then style of the font.
138    */

139   final int getFontStyle() {
140
141     return fontStyle;
142   }
143
144
145   /**
146    * Returns this font's color.
147    * @return This font's color.
148    */

149   final Color getFontColor() {
150
151     return fontColor;
152   }
153
154
155   /**
156    * Returns a font built from the present variable values. Updates
157    * the font point based on the model font point, builds an font, and return
158    * it.
159    * @return The current font.
160    */

161   final Font getFont() {
162
163     updateFontArea();
164     return font;
165   }
166
167
168   /**
169    * Indicates whether some property of this class has changed.
170    * @return True if some property has changed.
171    */

172     final boolean getFontAreaNeedsUpdate() {
173
174       return (needsUpdate || getAreaNeedsUpdate());
175     }
176
177
178   /**
179    * Resets the model for this class. The model is used for shrinking and
180    * growing of its components based on the maximum size of this class. If this
181    * method is called, then the next time the maximum size is set, this classes
182    * model maximum size will be made equal to the new maximum size. Effectively
183    * what this does is ensure that whenever this objects maximum size is equal
184    * to the one given, then all of the components will take on their default
185    * model sizes. Note: This is only useful when auto model max sizing is
186    * disabled.
187    * @param reset True causes the max model size to be set upon the next max
188    * sizing.
189    */

190   final void resetFontAreaModel (boolean reset) {
191
192     needsUpdate = true;
193     resetAreaModel (reset);
194   }
195
196
197   /**
198    * Updates all this classes variables. First updates it's parent class, then
199    * then updates its own variables.
200    */

201   final void updateFontArea () {
202
203     if (getFontAreaNeedsUpdate()) {
204       updateArea ();
205       update();
206     }
207     needsUpdate = false;
208   }
209
210
211   /**
212    * Paints this class. Updates all variables, then paints its parent, since
213    * this class itself doesn't have anything to paint.
214    * @param g2D The graphics context for calculations and painting.
215    */

216   void paintComponent (Graphics2D g2D) {
217
218     updateFontArea();
219     super.paintComponent (g2D);
220   }
221
222
223   private void update() {
224
225     int fontPoint = 0;
226     if (fontPointModel > 0) {
227       fontPoint = applyRatio (fontPointModel, getRatio (LESSER));
228     }
229     else fontPoint = 0;
230     font = new Font (fontName, fontStyle, fontPoint);
231   }
232 }
Popular Tags