KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > ImageTitle


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ---------------
23  * ImageTitle.java
24  * ---------------
25  * (C) Copyright 2000-2003, by David Berry and Contributors;
26  *
27  * Original Author: David Berry;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: ImageTitle.java,v 1.6 2003/09/24 12:15:02 mungady Exp $
31  *
32  * Changes (from 18-Sep-2001)
33  * --------------------------
34  * 18-Sep-2001 : Added standard header (DG);
35  * 07-Nov-2001 : Separated the JCommon Class Library classes, JFreeChart now requires
36  * jcommon.jar (DG);
37  * 09-Jan-2002 : Updated Javadoc comments (DG);
38  * 07-Feb-2002 : Changed blank space around title from Insets --> Spacer, to allow for relative
39  * or absolute spacing (DG);
40  * 25-Jun-2002 : Updated import statements (DG);
41  * 23-Sep-2002 : Fixed errors reported by Checkstyle (DG);
42  * 26-Nov-2002 : Added method for drawing images at left or right (DG);
43  * 22-Sep-2003 : Added checks that the Image can never be null (TM).
44  */

45
46 package org.jfree.chart;
47
48 import java.awt.Graphics2D JavaDoc;
49 import java.awt.Image JavaDoc;
50 import java.awt.geom.Rectangle2D JavaDoc;
51
52 import org.jfree.chart.event.TitleChangeEvent;
53 import org.jfree.ui.Size2D;
54
55 /**
56  * A chart title that displays an image. This is useful, for example, if you
57  * have an image of your corporate logo and want to use as a footnote or part
58  * of a title in a chart you create.
59  * <P>
60  * ImageTitle needs an image passed to it in the constructor. For ImageTitle
61  * to work, you must have already loaded this image from its source (disk or
62  * URL). It is recomended you use something like
63  * Toolkit.getDefaultToolkit().getImage() to get the image. Then, use
64  * MediaTracker or some other message to make sure the image is fully loaded
65  * from disk.
66  *
67  * @author David Berry
68  */

69 public class ImageTitle extends AbstractTitle {
70
71     /** The title image. */
72     private Image JavaDoc image;
73
74     /** The height used to draw the image (may involve scaling). */
75     private int height;
76
77     /** The width used to draw the image (may involve scaling). */
78     private int width;
79
80     /**
81      * Creates a new image title.
82      *
83      * @param image the image.
84      */

85     public ImageTitle(Image JavaDoc image) {
86
87         this(image,
88             image.getHeight(null),
89             image.getWidth(null),
90             AbstractTitle.DEFAULT_POSITION,
91             AbstractTitle.DEFAULT_HORIZONTAL_ALIGNMENT,
92             AbstractTitle.DEFAULT_VERTICAL_ALIGNMENT,
93             AbstractTitle.DEFAULT_SPACER);
94
95     }
96
97     /**
98      * Creates a new image title.
99      *
100      * @param image the image.
101      * @param position the title position (<code>LEFT</code>, <code>RIGHT</code>,
102      * <code>TOP</code> or <code>BOTTOM</code>).
103      * @param horizontalAlignment the horizontal alignment of the title (<code>LEFT</code>,
104      * <code>CENTER</code> or <code>RIGHT</code>).
105      * @param verticalAlignment the vertical alignment of the title (<code>TOP</code>,
106      * <code>MIDDLE</code> or <code>BOTTOM</code>).
107      */

108     public ImageTitle(Image JavaDoc image, int position, int horizontalAlignment, int verticalAlignment) {
109
110         this(image,
111             image.getHeight(null),
112             image.getWidth(null),
113             position,
114             horizontalAlignment,
115             verticalAlignment,
116             AbstractTitle.DEFAULT_SPACER);
117
118     }
119
120     /**
121      * Creates a new image title with the given image scaled to the given
122      * width and height in the given location.
123      *
124      * @param image the image (not null).
125      * @param height the height used to draw the image.
126      * @param width the width used to draw the image.
127      * @param position the title position (<code>LEFT</code>, <code>RIGHT</code>,
128      * <code>TOP</code> or <code>BOTTOM</code>).
129      * @param horizontalAlignment the horizontal alignment of the title (<code>LEFT</code>,
130      * <code>CENTER</code> or <code>RIGHT</code>).
131      * @param verticalAlignment the vertical alignment of the title (<code>TOP</code>,
132      * <code>MIDDLE</code> or <code>BOTTOM</code>).
133      * @param spacer the amount of space to leave around the outside of the title.
134      */

135     public ImageTitle(Image JavaDoc image, int height, int width, int position,
136                       int horizontalAlignment, int verticalAlignment, Spacer spacer) {
137
138         super(position, horizontalAlignment, verticalAlignment, spacer);
139         if (image == null) {
140             throw new NullPointerException JavaDoc("ImageTitle(..): Image argument is null.");
141         }
142         this.image = image;
143         this.height = height;
144         this.width = width;
145
146     }
147
148     /**
149      * Returns the image for the title.
150      *
151      * @return the image for the title.
152      */

153     public Image JavaDoc getImage() {
154         return this.image;
155     }
156
157     /**
158      * Sets the image for the title and notifies registered listeners that the
159      * title has been modified.
160      *
161      * @param image the new image (<code>null</code> not permitted).
162      */

163     public void setImage(Image JavaDoc image) {
164
165         if (image == null) {
166             throw new NullPointerException JavaDoc("ImageTitle.setImage (..): Image must not be null.");
167         }
168         this.image = image;
169         notifyListeners(new TitleChangeEvent(this));
170
171     }
172
173     /**
174      * Draws the title on a Java 2D graphics device (such as the screen or a printer).
175      *
176      * @param g2 the graphics device.
177      * @param titleArea the area within which the title (and plot) should be drawn.
178      */

179     public void draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc titleArea) {
180
181         int position = getPosition();
182         if (position == TOP || position == BOTTOM) {
183             drawHorizontal(g2, titleArea);
184         }
185         else if (position == LEFT || position == RIGHT) {
186             drawVertical(g2, titleArea);
187         }
188         else {
189             throw new RuntimeException JavaDoc("ImageTitle.draw(...) - invalid title position.");
190         }
191     }
192
193     /**
194      * Returns true for all positions, since an image can be displayed anywhere.
195      *
196      * @param position the title position (LEFT, RIGHT, TOP, BOTTOM).
197      *
198      * @return <code>true</code> if the position is LEFT, RIGHT, TOP or BOTTOM.
199      */

200     public boolean isValidPosition(int position) {
201
202         switch (position) {
203             case TOP:
204             case BOTTOM:
205             case RIGHT:
206             case LEFT:
207                 return true;
208             default :
209                 return false;
210         }
211
212     }
213
214     /**
215      * Returns the preferred width of the title.
216      *
217      * @param g2 the graphics device.
218      *
219      * @return the preferred width of the title.
220      */

221     public double getPreferredWidth(Graphics2D JavaDoc g2) {
222
223         double result = this.width;
224
225         Spacer spacer = getSpacer();
226         //if (spacer != null) {
227
result = spacer.getAdjustedWidth(result);
228         //}
229

230         return result;
231
232     }
233
234     /**
235      * Returns the preferred height of the title.
236      *
237      * @param g2 the graphics device.
238      *
239      * @return the preferred height of the title.
240      */

241     public double getPreferredHeight(Graphics2D JavaDoc g2) {
242
243         double result = this.height;
244
245         Spacer spacer = getSpacer();
246         // AbstractTitle is implemented in a way that spacer can never be null
247
//if (spacer != null) {
248
result = spacer.getAdjustedHeight(result);
249         //}
250

251         return result;
252
253     }
254
255     /**
256      * Draws the title on a Java 2D graphics device (such as the screen or a printer).
257      *
258      * @param g2 the graphics device.
259      * @param chartArea the area within which the title (and plot) should be drawn.
260      *
261      * @return the area used by the title.
262      */

263     protected Size2D drawHorizontal(Graphics2D JavaDoc g2, Rectangle2D JavaDoc chartArea) {
264
265         double startY = 0.0;
266         double topSpace = 0.0;
267         double bottomSpace = 0.0;
268         double leftSpace = 0.0;
269         double rightSpace = 0.0;
270
271         Spacer spacer = getSpacer();
272         // AbstractTitle is implemented in a way that spacer can never be null
273
//if (spacer != null) {
274
topSpace = spacer.getTopSpace(this.height);
275         bottomSpace = spacer.getBottomSpace(this.height);
276         leftSpace = spacer.getLeftSpace(this.width);
277         rightSpace = spacer.getRightSpace(this.width);
278         //}
279

280         if (getPosition() == TOP) {
281             startY = chartArea.getY() + topSpace;
282         }
283         else {
284             startY = chartArea.getY() + chartArea.getHeight() - bottomSpace - this.height;
285         }
286
287         // what is our alignment?
288
int horizontalAlignment = getHorizontalAlignment();
289         double startX = 0.0;
290         if (horizontalAlignment == CENTER) {
291             startX = chartArea.getX() + leftSpace + chartArea.getWidth() / 2 - this.width / 2;
292         }
293         else {
294             if (horizontalAlignment == LEFT) {
295                 startX = chartArea.getX() + leftSpace;
296             }
297             else {
298                 if (horizontalAlignment == RIGHT) {
299                     startX = chartArea.getX() + chartArea.getWidth() - rightSpace - this.width;
300                 }
301             }
302         }
303
304         g2.drawImage(image, (int) startX, (int) startY, this.width, this.height, null);
305
306         return new Size2D(chartArea.getWidth() + leftSpace + rightSpace,
307             this.height + topSpace + bottomSpace);
308
309     }
310
311     /**
312      * Draws the title on a Java 2D graphics device (such as the screen or a printer).
313      *
314      * @param g2 the graphics device.
315      * @param chartArea the area within which the title (and plot) should be drawn.
316      *
317      * @return the area used by the title.
318      */

319     protected Size2D drawVertical(Graphics2D JavaDoc g2, Rectangle2D JavaDoc chartArea) {
320
321         double startX = 0.0;
322         double topSpace = 0.0;
323         double bottomSpace = 0.0;
324         double leftSpace = 0.0;
325         double rightSpace = 0.0;
326
327         Spacer spacer = getSpacer();
328         if (spacer != null) {
329             topSpace = spacer.getTopSpace(this.height);
330             bottomSpace = spacer.getBottomSpace(this.height);
331             leftSpace = spacer.getLeftSpace(this.width);
332             rightSpace = spacer.getRightSpace(this.width);
333         }
334
335         if (getPosition() == LEFT) {
336             startX = chartArea.getX() + leftSpace;
337         }
338         else {
339             startX = chartArea.getMaxX() - rightSpace - this.width;
340         }
341
342         // what is our alignment?
343
int alignment = getVerticalAlignment();
344         double startY = 0.0;
345         if (alignment == MIDDLE) {
346             startY = chartArea.getMinY() + topSpace + chartArea.getHeight() / 2 - this.height / 2;
347         }
348         else {
349             if (alignment == TOP) {
350                 startY = chartArea.getMinY() + topSpace;
351             }
352             else {
353                 if (alignment == BOTTOM) {
354                     startY = chartArea.getMaxY() - bottomSpace - this.height;
355                 }
356             }
357         }
358
359         g2.drawImage(image, (int) startX, (int) startY, this.width, this.height, null);
360
361         return new Size2D(chartArea.getWidth() + leftSpace + rightSpace,
362             this.height + topSpace + bottomSpace);
363
364     }
365
366 }
367
Popular Tags