KickJava   Java API By Example, From Geeks To Geeks.

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


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.image.*;
29 import javax.swing.*;
30
31
32 /**
33  * An abstract class for the common methods of Chart2D, Clocks2D, and Progress2D objects.
34  * An Object2D object is one with an enclosing area and a title.
35  * Changes through its set methods are updated upon next repaint() or getImage() calls.
36  */

37 public abstract class Object2D extends JComponent {
38
39
40   /**
41    * The default is new Dimension (1024, 768).
42    */

43   public static final Dimension MAX_SIZE_DEFAULT = new Dimension (1024, 768);
44
45   /**
46    * The default is new Dimension (1, 1).
47    */

48   public static final Dimension MIN_SIZE_DEFAULT = new Dimension (1, 1);
49
50
51   private Object2DProperties object2DProps;
52   private boolean needsUpdate;
53
54
55   /**
56    * Creates an Object2D object with its defaults.
57    * An Object2DProperties object must be set for this object before it is used.
58    */

59   public Object2D() {
60
61     needsUpdate = true;
62     setMaximumSize (MAX_SIZE_DEFAULT);
63     setMinimumSize (MIN_SIZE_DEFAULT);
64   }
65
66
67   /**
68    * Sets the Object2DProperties for this Object2D.
69    * @param props The Object2DProperties.
70    */

71   public final void setObject2DProperties (Object2DProperties props) {
72
73     needsUpdate = true;
74     props.addObject2D (this);
75     if (object2DProps != null) object2DProps.removeObject2D (this);
76     object2DProps = props;
77   }
78
79
80   /**
81    * Sets a custom preferred size for the chart.
82    * This custom size will override the preferred size calculations that normally occurr.
83    * If null is passed, the preferred size calculations will be reinstated.
84    * @param size The custom preferred size for this chart.
85    */

86   public abstract void setPreferredSize (Dimension size);
87
88
89   /**
90    * Sets the maximum size of this object.
91    * @param size The maximum size.
92    */

93   public final void setMaximumSize (Dimension size) {
94
95     needsUpdate = true;
96     super.setMaximumSize (size);
97   }
98
99
100   /**
101    * Sets the minimum size of this object.
102    * @param size The minimum size.
103    */

104   public final void setMinimumSize (Dimension size) {
105
106     needsUpdate = true;
107     super.setMinimumSize (size);
108   }
109
110
111   /**
112    * Gets the Object2DProperties for this Object2D.
113    * @return The Object2DProperties.
114    */

115   public final Object2DProperties getObject2DProperties() {
116     return object2DProps;
117   }
118
119
120   /**
121    * Gets the preferred size of the chart.
122    * The preferred size is within the maximum and minimum sizes of the chart.
123    * Much calculation is performed when calling this method.
124    * @return The preferred minimum size of the chart.
125    */

126   public abstract Dimension getPreferredSize();
127
128
129   /**
130    * Gets whether this object needs to be updated with new properties.
131    * @return If true then needs update.
132    */

133   final boolean getNeedsUpdateObject2D() {
134     return (needsUpdate || object2DProps.getObject2DNeedsUpdate (this));
135   }
136
137
138   /**
139    * Gets the TitledArea of this Object2D.
140    * @return The TitledArea.
141    */

142   abstract TitledArea getObjectArea();
143
144
145   /**
146    * Validates the properties of this object.
147    * If debug is true then prints a messages indicating whether each property is valid.
148    * Returns true if all the properties were valid and false otherwise.
149    * @param debug If true then will print status messages.
150    * @return If true then valid.
151    */

152   final boolean validateObject2D (boolean debug) {
153
154     if (debug) System.out.println ("Validating Object2D");
155
156     boolean valid = true;
157
158     if (object2DProps == null) {
159       valid = false;
160       if (debug) System.out.println ("Object2DProperties is null");
161     }
162     else if (!object2DProps.validate (debug)) valid = false;
163
164     if (getMaximumSize() == null ||
165       getMaximumSize().height < 1 || getMaximumSize().width < 1 ||
166       getMaximumSize().height < getMinimumSize().height ||
167       getMaximumSize().width < getMinimumSize().width) {
168       valid = false;
169       if (debug) System.out.println ("Problem with maximum size");
170     }
171     if (getMinimumSize() == null ||
172       getMinimumSize().height < 1 || getMinimumSize().width < 1) {
173       valid = false;
174       if (debug) System.out.println ("Problem with minimum size");
175     }
176
177     if (debug) {
178       if (valid) System.out.println ("Object2D was valid");
179       else System.out.println ("Object2D was invalid");
180     }
181
182     return valid;
183   }
184
185
186   /**
187    * Updates this object.
188    */

189   final void updateObject2D() {
190
191     if (getNeedsUpdateObject2D()) {
192
193       needsUpdate = false;
194       object2DProps.updateObject2D (this);
195
196       TitledArea object = (TitledArea)getObjectArea();
197       object.setAutoSizes (!object2DProps.getObjectMagnifyWhenResize(), true);
198       object.setBorderExistence (object2DProps.getObjectBorderExistence());
199       object.setBorderThicknessModel (object2DProps.getObjectBorderThicknessModel());
200       object.setBorderColor (object2DProps.getObjectBorderColor());
201       object.setGapExistence (object2DProps.getObjectGapExistence());
202       object.setGapThicknessModel (object2DProps.getObjectGapThicknessModel());
203       object.setBackgroundExistence (object2DProps.getObjectBackgroundExistence());
204       object.setBackgroundColor (object2DProps.getObjectBackgroundColor());
205       object.setLightSource (object2DProps.getObjectBackgroundLightSource());
206       object.setTitleExistence (object2DProps.getObjectTitleExistence());
207       object.setTitle (object2DProps.getObjectTitleText());
208       object.setFontPointModel (object2DProps.getObjectTitleFontPointModel());
209       object.setFontName (object2DProps.getObjectTitleFontName());
210       object.setFontColor (object2DProps.getObjectTitleFontColor());
211       object.setFontStyle (object2DProps.getObjectTitleFontStyle());
212       object.setBetweenTitleAndSpaceGapExistence (
213         object2DProps.getObjectTitleBetweenRestGapExistence());
214       object.setBetweenTitleAndSpaceGapThicknessModel (
215         object2DProps.getObjectTitleBetweenRestGapThicknessModel());
216     }
217   }
218
219
220   /**
221    * Gets a buffered image of the chart.
222    * @return An image of this chart
223    */

224   public abstract BufferedImage getImage();
225
226
227   /**
228    * Causes the object to reinintialize to it's preferred size.
229    */

230   public abstract void pack();
231
232
233   /**
234    * Validates the properties of this object.
235    * If debug is true then prints a messages indicating whether each property is valid.
236    * Returns true if all the properties were valid and false otherwise.
237    * @param debug If true then will print status messages.
238    * @return If true then valid.
239    */

240   public abstract boolean validate (boolean debug);
241 }
Popular Tags