KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > axis > MarkerAxisBand


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -------------------
27  * MarkerAxisBand.java
28  * -------------------
29  * (C) Copyright 2000-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: MarkerAxisBand.java,v 1.6 2005/05/19 13:58:11 mungady Exp $
35  *
36  * Changes (from 03-Sep-2002)
37  * --------------------------
38  * 03-Sep-2002 : Updated Javadoc comments (DG);
39  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
40  * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
41  * 26-Mar-2003 : Implemented Serializable (DG);
42  * 13-May-2003 : Renamed HorizontalMarkerAxisBand --> MarkerAxisBand (DG);
43  * 29-Oct-2003 : Added workaround for font alignment in PDF output (DG);
44  * 21-Jan-2004 : Update for renamed method in ValueAxis (DG);
45  * 07-Apr-2004 : Changed text bounds calculation (DG);
46  *
47  */

48
49 package org.jfree.chart.axis;
50
51 import java.awt.AlphaComposite JavaDoc;
52 import java.awt.Color JavaDoc;
53 import java.awt.Composite JavaDoc;
54 import java.awt.Font JavaDoc;
55 import java.awt.FontMetrics JavaDoc;
56 import java.awt.Graphics2D JavaDoc;
57 import java.awt.font.LineMetrics JavaDoc;
58 import java.awt.geom.Rectangle2D JavaDoc;
59 import java.io.Serializable JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.List JavaDoc;
62
63 import org.jfree.chart.plot.IntervalMarker;
64 import org.jfree.text.TextUtilities;
65 import org.jfree.ui.RectangleEdge;
66 import org.jfree.util.ObjectUtilities;
67
68 /**
69  * A band that can be added to a number axis to display regions.
70  */

71 public class MarkerAxisBand implements Serializable JavaDoc {
72
73     /** For serialization. */
74     private static final long serialVersionUID = -1729482413886398919L;
75     
76     /** The axis that the band belongs to. */
77     private NumberAxis axis;
78
79     /** The top outer gap. */
80     private double topOuterGap;
81
82     /** The top inner gap. */
83     private double topInnerGap;
84
85     /** The bottom outer gap. */
86     private double bottomOuterGap;
87
88     /** The bottom inner gap. */
89     private double bottomInnerGap;
90
91     /** The font. */
92     private Font JavaDoc font;
93
94     /** Storage for the markers. */
95     private List JavaDoc markers;
96
97     /**
98      * Constructs a new axis band.
99      *
100      * @param axis the owner.
101      * @param topOuterGap the top outer gap.
102      * @param topInnerGap the top inner gap.
103      * @param bottomOuterGap the bottom outer gap.
104      * @param bottomInnerGap the bottom inner gap.
105      * @param font the font.
106      */

107     public MarkerAxisBand(NumberAxis axis,
108                           double topOuterGap, double topInnerGap,
109                           double bottomOuterGap, double bottomInnerGap,
110                           Font JavaDoc font) {
111         this.axis = axis;
112         this.topOuterGap = topOuterGap;
113         this.topInnerGap = topInnerGap;
114         this.bottomOuterGap = bottomOuterGap;
115         this.bottomInnerGap = bottomInnerGap;
116         this.font = font;
117         this.markers = new java.util.ArrayList JavaDoc();
118     }
119
120     /**
121      * Adds a marker to the band.
122      *
123      * @param marker the marker.
124      */

125     public void addMarker(IntervalMarker marker) {
126         this.markers.add(marker);
127     }
128
129     /**
130      * Returns the height of the band.
131      *
132      * @param g2 the graphics device.
133      *
134      * @return The height of the band.
135      */

136     public double getHeight(Graphics2D JavaDoc g2) {
137
138         double result = 0.0;
139         if (this.markers.size() > 0) {
140             LineMetrics JavaDoc metrics = this.font.getLineMetrics(
141                 "123g", g2.getFontRenderContext()
142             );
143             result = this.topOuterGap + this.topInnerGap + metrics.getHeight()
144                      + this.bottomInnerGap + this.bottomOuterGap;
145         }
146         return result;
147
148     }
149
150     /**
151      * A utility method that draws a string inside a rectangle.
152      *
153      * @param g2 the graphics device.
154      * @param bounds the rectangle.
155      * @param font the font.
156      * @param text the text.
157      */

158     private void drawStringInRect(Graphics2D JavaDoc g2, Rectangle2D JavaDoc bounds, Font JavaDoc font,
159                                   String JavaDoc text) {
160
161         g2.setFont(font);
162         FontMetrics JavaDoc fm = g2.getFontMetrics(font);
163         Rectangle2D JavaDoc r = TextUtilities.getTextBounds(text, g2, fm);
164         double x = bounds.getX();
165         if (r.getWidth() < bounds.getWidth()) {
166             x = x + (bounds.getWidth() - r.getWidth()) / 2;
167         }
168         LineMetrics JavaDoc metrics = font.getLineMetrics(
169             text, g2.getFontRenderContext()
170         );
171         g2.drawString(
172             text, (float) x, (float) (bounds.getMaxY()
173                 - this.bottomInnerGap - metrics.getDescent())
174         );
175     }
176
177     /**
178      * Draws the band.
179      *
180      * @param g2 the graphics device.
181      * @param plotArea the plot area.
182      * @param dataArea the data area.
183      * @param x the x-coordinate.
184      * @param y the y-coordinate.
185      */

186     public void draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc plotArea, Rectangle2D JavaDoc dataArea,
187                      double x, double y) {
188
189         double h = getHeight(g2);
190         Iterator JavaDoc iterator = this.markers.iterator();
191         while (iterator.hasNext()) {
192             IntervalMarker marker = (IntervalMarker) iterator.next();
193             double start = Math.max(
194                 marker.getStartValue(), this.axis.getRange().getLowerBound()
195             );
196             double end = Math.min(
197                 marker.getEndValue(), this.axis.getRange().getUpperBound()
198             );
199             double s = this.axis.valueToJava2D(
200                 start, dataArea, RectangleEdge.BOTTOM
201             );
202             double e = this.axis.valueToJava2D(
203                 end, dataArea, RectangleEdge.BOTTOM
204             );
205             Rectangle2D JavaDoc r = new Rectangle2D.Double JavaDoc(
206                 s, y + this.topOuterGap, e - s,
207                 h - this.topOuterGap - this.bottomOuterGap
208             );
209
210             Composite JavaDoc originalComposite = g2.getComposite();
211             g2.setComposite(AlphaComposite.getInstance(
212                 AlphaComposite.SRC_OVER, marker.getAlpha())
213             );
214             g2.setPaint(marker.getPaint());
215             g2.fill(r);
216             g2.setPaint(marker.getOutlinePaint());
217             g2.draw(r);
218             g2.setComposite(originalComposite);
219
220             g2.setPaint(Color.black);
221             drawStringInRect(g2, r, this.font, marker.getLabel());
222         }
223
224     }
225
226     /**
227      * Tests this axis for equality with another object. Note that the axis
228      * that the band belongs to is ignored in the test.
229      *
230      * @param obj the object (<code>null</code> permitted).
231      *
232      * @return <code>true</code> or <code>false</code>.
233      */

234     public boolean equals(Object JavaDoc obj) {
235         if (obj == this) {
236             return true;
237         }
238         if (!(obj instanceof MarkerAxisBand)) {
239             return false;
240         }
241         MarkerAxisBand that = (MarkerAxisBand) obj;
242         if (this.topOuterGap != that.topOuterGap) {
243             return false;
244         }
245         if (this.topInnerGap != that.topInnerGap) {
246             return false;
247         }
248         if (this.bottomInnerGap != that.bottomInnerGap) {
249             return false;
250         }
251         if (this.bottomOuterGap != that.bottomOuterGap) {
252             return false;
253         }
254         if (!ObjectUtilities.equal(this.font, that.font)) {
255             return false;
256         }
257         if (!ObjectUtilities.equal(this.markers, that.markers)) {
258             return false;
259         }
260         return true;
261     }
262     
263     /**
264      * Returns a hash code for the object.
265      *
266      * @return A hash code.
267      */

268     public int hashCode() {
269         int result = 37;
270         result = 19 * result + this.font.hashCode();
271         result = 19 * result + this.markers.hashCode();
272         return result;
273     }
274
275 }
276
Popular Tags