001: /**
002: * Chart2D, a java library for drawing two dimensional charts.
003: * Copyright (C) 2001 Jason J. Simas
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * The author of this library may be contacted at:
019: * E-mail: jjsimas@users.sourceforge.net
020: * Street Address: J J Simas, 887 Tico Road, Ojai, CA 93023-3555 USA
021: */package net.sourceforge.chart2d;
022:
023: import java.awt.*;
024:
025: /**
026: * Maintains a font to be used within an area. Contains all variables for the
027: * font, such as point, name, style, and color. Builds a font from these
028: * variable. When building font, uses the lesser ratio of the area class.
029: * This ensures proper growing and shrinking of the font respective to the
030: * changing in the area's size. Grows and shrinks if and only if max model
031: * area auto size is disabled.
032: */
033: class FontArea extends Area {
034:
035: private int fontPointModel;
036: private String fontName;
037: private int fontStyle;
038: private Color fontColor;
039: private Font font;
040: private boolean needsUpdate;
041:
042: /**
043: * Builds a new FontArea with the default values.
044: */
045: FontArea() {
046:
047: setFontPointModel(12);
048: setFontName("SansSerif");
049: setFontStyle(Font.PLAIN);
050: setFontColor(Color.black);
051: resetFontAreaModel(true);
052: needsUpdate = true;
053: }
054:
055: /**
056: * Changes the model font's point size. This is the size that the font would
057: * be if the maximum size of the area was equal to the model size of the area.
058: * Otherwise, a ratio based on maximum size / model size is taken and applied
059: * to this point, producing the actual font point.
060: * @param p The new model font point.
061: */
062: final void setFontPointModel(int p) {
063:
064: needsUpdate = true;
065: fontPointModel = p;
066: }
067:
068: /**
069: * Changes the name of this font. This accepts the same values as
070: * the Font classes constructor
071: * <code>Font (String name, int style, int point)</code>.
072: * @param n Then new name of the font.
073: */
074: final void setFontName(String n) {
075:
076: needsUpdate = true;
077: fontName = n;
078: }
079:
080: /**
081: * Changes the style of this font. This accepts the same values as
082: * the Font classes constructor
083: * <code>Font (String name, int style, int point)</code>.
084: * @param s Then new style of the font.
085: */
086: final void setFontStyle(int s) {
087:
088: needsUpdate = true;
089: fontStyle = s;
090: }
091:
092: /**
093: * Changes the color of this font. This accepts all Color.* compatible
094: * values.
095: * @param c Then new color of the font.
096: */
097: final void setFontColor(Color c) {
098:
099: needsUpdate = true;
100: fontColor = c;
101: }
102:
103: /**
104: * Returns the model font's point; not the actual font point.
105: * @return The model font's point.
106: */
107: final int getFontPointModel() {
108:
109: return fontPointModel;
110: }
111:
112: /**
113: * Returns the name of this font.
114: * @return Then name of the font.
115: */
116: final String getFontName() {
117:
118: return fontName;
119: }
120:
121: /**
122: * Returns the style of this font.
123: * @return Then style of the font.
124: */
125: final int getFontStyle() {
126:
127: return fontStyle;
128: }
129:
130: /**
131: * Returns this font's color.
132: * @return This font's color.
133: */
134: final Color getFontColor() {
135:
136: return fontColor;
137: }
138:
139: /**
140: * Returns a font built from the present variable values. Updates
141: * the font point based on the model font point, builds an font, and return
142: * it.
143: * @return The current font.
144: */
145: final Font getFont() {
146:
147: updateFontArea();
148: return font;
149: }
150:
151: /**
152: * Indicates whether some property of this class has changed.
153: * @return True if some property has changed.
154: */
155: final boolean getFontAreaNeedsUpdate() {
156:
157: return (needsUpdate || getAreaNeedsUpdate());
158: }
159:
160: /**
161: * Resets the model for this class. The model is used for shrinking and
162: * growing of its components based on the maximum size of this class. If this
163: * method is called, then the next time the maximum size is set, this classes
164: * model maximum size will be made equal to the new maximum size. Effectively
165: * what this does is ensure that whenever this objects maximum size is equal
166: * to the one given, then all of the components will take on their default
167: * model sizes. Note: This is only useful when auto model max sizing is
168: * disabled.
169: * @param reset True causes the max model size to be set upon the next max
170: * sizing.
171: */
172: final void resetFontAreaModel(boolean reset) {
173:
174: needsUpdate = true;
175: resetAreaModel(reset);
176: }
177:
178: /**
179: * Updates all this classes variables. First updates it's parent class, then
180: * then updates its own variables.
181: */
182: final void updateFontArea() {
183:
184: if (getFontAreaNeedsUpdate()) {
185: updateArea();
186: update();
187: }
188: needsUpdate = false;
189: }
190:
191: /**
192: * Paints this class. Updates all variables, then paints its parent, since
193: * this class itself doesn't have anything to paint.
194: * @param g2D The graphics context for calculations and painting.
195: */
196: void paintComponent(Graphics2D g2D) {
197:
198: updateFontArea();
199: super .paintComponent(g2D);
200: }
201:
202: private void update() {
203:
204: int fontPoint = 0;
205: if (fontPointModel > 0) {
206: fontPoint = applyRatio(fontPointModel, getRatio(LESSER));
207: } else
208: fontPoint = 0;
209: font = new Font(fontName, fontStyle, fontPoint);
210: }
211: }
|