001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------
028: * ImageTitle.java
029: * ---------------
030: * (C) Copyright 2000-2007, by David Berry and Contributors;
031: *
032: * Original Author: David Berry;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: ImageTitle.java,v 1.8.2.3 2007/02/02 15:03:19 mungady Exp $
036: *
037: * Changes (from 18-Sep-2001)
038: * --------------------------
039: * 18-Sep-2001 : Added standard header (DG);
040: * 07-Nov-2001 : Separated the JCommon Class Library classes, JFreeChart now
041: * requires jcommon.jar (DG);
042: * 09-Jan-2002 : Updated Javadoc comments (DG);
043: * 07-Feb-2002 : Changed blank space around title from Insets --> Spacer, to
044: * allow for relative or absolute spacing (DG);
045: * 25-Jun-2002 : Updated import statements (DG);
046: * 23-Sep-2002 : Fixed errors reported by Checkstyle (DG);
047: * 26-Nov-2002 : Added method for drawing images at left or right (DG);
048: * 22-Sep-2003 : Added checks that the Image can never be null (TM).
049: * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0
050: * release (DG);
051: * 02-Feb-2005 : Changed padding mechanism for all titles (DG);
052: * 20-Apr-2005 : Added new draw() method (DG);
053: * ------------- JFREECHART 1.0.x ---------------------------------------------
054: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
055: *
056: */
057:
058: package org.jfree.chart.title;
059:
060: import java.awt.Graphics2D;
061: import java.awt.Image;
062: import java.awt.geom.Rectangle2D;
063:
064: import org.jfree.chart.event.TitleChangeEvent;
065: import org.jfree.ui.HorizontalAlignment;
066: import org.jfree.ui.RectangleEdge;
067: import org.jfree.ui.RectangleInsets;
068: import org.jfree.ui.Size2D;
069: import org.jfree.ui.VerticalAlignment;
070:
071: /**
072: * A chart title that displays an image. This is useful, for example, if you
073: * have an image of your corporate logo and want to use as a footnote or part
074: * of a title in a chart you create.
075: * <P>
076: * ImageTitle needs an image passed to it in the constructor. For ImageTitle
077: * to work, you must have already loaded this image from its source (disk or
078: * URL). It is recomended you use something like
079: * Toolkit.getDefaultToolkit().getImage() to get the image. Then, use
080: * MediaTracker or some other message to make sure the image is fully loaded
081: * from disk.
082: */
083: public class ImageTitle extends Title {
084:
085: /** The title image. */
086: private Image image;
087:
088: /**
089: * Creates a new image title.
090: *
091: * @param image the image (<code>null</code> not permitted).
092: */
093: public ImageTitle(Image image) {
094: this (image, image.getHeight(null), image.getWidth(null),
095: Title.DEFAULT_POSITION,
096: Title.DEFAULT_HORIZONTAL_ALIGNMENT,
097: Title.DEFAULT_VERTICAL_ALIGNMENT, Title.DEFAULT_PADDING);
098: }
099:
100: /**
101: * Creates a new image title.
102: *
103: * @param image the image (<code>null</code> not permitted).
104: * @param position the title position.
105: * @param horizontalAlignment the horizontal alignment.
106: * @param verticalAlignment the vertical alignment.
107: */
108: public ImageTitle(Image image, RectangleEdge position,
109: HorizontalAlignment horizontalAlignment,
110: VerticalAlignment verticalAlignment) {
111:
112: this (image, image.getHeight(null), image.getWidth(null),
113: position, horizontalAlignment, verticalAlignment,
114: Title.DEFAULT_PADDING);
115: }
116:
117: /**
118: * Creates a new image title with the given image scaled to the given
119: * width and height in the given location.
120: *
121: * @param image the image (<code>null</code> not permitted).
122: * @param height the height used to draw the image.
123: * @param width the width used to draw the image.
124: * @param position the title position.
125: * @param horizontalAlignment the horizontal alignment.
126: * @param verticalAlignment the vertical alignment.
127: * @param padding the amount of space to leave around the outside of the
128: * title.
129: */
130: public ImageTitle(Image image, int height, int width,
131: RectangleEdge position,
132: HorizontalAlignment horizontalAlignment,
133: VerticalAlignment verticalAlignment, RectangleInsets padding) {
134:
135: super (position, horizontalAlignment, verticalAlignment, padding);
136: if (image == null) {
137: throw new NullPointerException("Null 'image' argument.");
138: }
139: this .image = image;
140: setHeight(height);
141: setWidth(width);
142:
143: }
144:
145: /**
146: * Returns the image for the title.
147: *
148: * @return The image for the title (never <code>null</code>).
149: */
150: public Image getImage() {
151: return this .image;
152: }
153:
154: /**
155: * Sets the image for the title and notifies registered listeners that the
156: * title has been modified.
157: *
158: * @param image the new image (<code>null</code> not permitted).
159: */
160: public void setImage(Image image) {
161: if (image == null) {
162: throw new NullPointerException("Null 'image' argument.");
163: }
164: this .image = image;
165: notifyListeners(new TitleChangeEvent(this ));
166: }
167:
168: /**
169: * Draws the title on a Java 2D graphics device (such as the screen or a
170: * printer).
171: *
172: * @param g2 the graphics device.
173: * @param titleArea the area within which the title (and plot) should be
174: * drawn.
175: */
176: public void draw(Graphics2D g2, Rectangle2D titleArea) {
177:
178: RectangleEdge position = getPosition();
179: if (position == RectangleEdge.TOP
180: || position == RectangleEdge.BOTTOM) {
181: drawHorizontal(g2, titleArea);
182: } else if (position == RectangleEdge.LEFT
183: || position == RectangleEdge.RIGHT) {
184: drawVertical(g2, titleArea);
185: } else {
186: throw new RuntimeException("Invalid title position.");
187: }
188: }
189:
190: /**
191: * Draws the title on a Java 2D graphics device (such as the screen or a
192: * printer).
193: *
194: * @param g2 the graphics device.
195: * @param chartArea the area within which the title (and plot) should be
196: * drawn.
197: *
198: * @return The size of the area used by the title.
199: */
200: protected Size2D drawHorizontal(Graphics2D g2, Rectangle2D chartArea) {
201:
202: double startY = 0.0;
203: double topSpace = 0.0;
204: double bottomSpace = 0.0;
205: double leftSpace = 0.0;
206: double rightSpace = 0.0;
207:
208: double w = getWidth();
209: double h = getHeight();
210: RectangleInsets padding = getPadding();
211: topSpace = padding.calculateTopOutset(h);
212: bottomSpace = padding.calculateBottomOutset(h);
213: leftSpace = padding.calculateLeftOutset(w);
214: rightSpace = padding.calculateRightOutset(w);
215:
216: if (getPosition() == RectangleEdge.TOP) {
217: startY = chartArea.getY() + topSpace;
218: } else {
219: startY = chartArea.getY() + chartArea.getHeight()
220: - bottomSpace - h;
221: }
222:
223: // what is our alignment?
224: HorizontalAlignment horizontalAlignment = getHorizontalAlignment();
225: double startX = 0.0;
226: if (horizontalAlignment == HorizontalAlignment.CENTER) {
227: startX = chartArea.getX() + leftSpace
228: + chartArea.getWidth() / 2.0 - w / 2.0;
229: } else if (horizontalAlignment == HorizontalAlignment.LEFT) {
230: startX = chartArea.getX() + leftSpace;
231: } else if (horizontalAlignment == HorizontalAlignment.RIGHT) {
232: startX = chartArea.getX() + chartArea.getWidth()
233: - rightSpace - w;
234: }
235: g2.drawImage(this .image, (int) startX, (int) startY, (int) w,
236: (int) h, null);
237:
238: return new Size2D(
239: chartArea.getWidth() + leftSpace + rightSpace, h
240: + topSpace + bottomSpace);
241:
242: }
243:
244: /**
245: * Draws the title on a Java 2D graphics device (such as the screen or a
246: * printer).
247: *
248: * @param g2 the graphics device.
249: * @param chartArea the area within which the title (and plot) should be
250: * drawn.
251: *
252: * @return The size of the area used by the title.
253: */
254: protected Size2D drawVertical(Graphics2D g2, Rectangle2D chartArea) {
255:
256: double startX = 0.0;
257: double topSpace = 0.0;
258: double bottomSpace = 0.0;
259: double leftSpace = 0.0;
260: double rightSpace = 0.0;
261:
262: double w = getWidth();
263: double h = getHeight();
264:
265: RectangleInsets padding = getPadding();
266: if (padding != null) {
267: topSpace = padding.calculateTopOutset(h);
268: bottomSpace = padding.calculateBottomOutset(h);
269: leftSpace = padding.calculateLeftOutset(w);
270: rightSpace = padding.calculateRightOutset(w);
271: }
272:
273: if (getPosition() == RectangleEdge.LEFT) {
274: startX = chartArea.getX() + leftSpace;
275: } else {
276: startX = chartArea.getMaxX() - rightSpace - w;
277: }
278:
279: // what is our alignment?
280: VerticalAlignment alignment = getVerticalAlignment();
281: double startY = 0.0;
282: if (alignment == VerticalAlignment.CENTER) {
283: startY = chartArea.getMinY() + topSpace
284: + chartArea.getHeight() / 2.0 - h / 2.0;
285: } else if (alignment == VerticalAlignment.TOP) {
286: startY = chartArea.getMinY() + topSpace;
287: } else if (alignment == VerticalAlignment.BOTTOM) {
288: startY = chartArea.getMaxY() - bottomSpace - h;
289: }
290:
291: g2.drawImage(this .image, (int) startX, (int) startY, (int) w,
292: (int) h, null);
293:
294: return new Size2D(
295: chartArea.getWidth() + leftSpace + rightSpace, h
296: + topSpace + bottomSpace);
297:
298: }
299:
300: /**
301: * Draws the block within the specified area.
302: *
303: * @param g2 the graphics device.
304: * @param area the area.
305: * @param params ignored (<code>null</code> permitted).
306: *
307: * @return Always <code>null</code>.
308: */
309: public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
310: draw(g2, area);
311: return null;
312: }
313:
314: }
|