1:
49:
50: package ;
51:
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64: import ;
65: import ;
66: import ;
67: import ;
68:
69:
72: public class MarkerAxisBand implements Serializable {
73:
74:
75: private static final long serialVersionUID = -1729482413886398919L;
76:
77:
78: private NumberAxis axis;
79:
80:
81: private double topOuterGap;
82:
83:
84: private double topInnerGap;
85:
86:
87: private double bottomOuterGap;
88:
89:
90: private double bottomInnerGap;
91:
92:
93: private Font font;
94:
95:
96: private List markers;
97:
98:
108: public MarkerAxisBand(NumberAxis axis,
109: double topOuterGap, double topInnerGap,
110: double bottomOuterGap, double bottomInnerGap,
111: Font font) {
112: this.axis = axis;
113: this.topOuterGap = topOuterGap;
114: this.topInnerGap = topInnerGap;
115: this.bottomOuterGap = bottomOuterGap;
116: this.bottomInnerGap = bottomInnerGap;
117: this.font = font;
118: this.markers = new java.util.ArrayList();
119: }
120:
121:
126: public void addMarker(IntervalMarker marker) {
127: this.markers.add(marker);
128: }
129:
130:
137: public double getHeight(Graphics2D g2) {
138:
139: double result = 0.0;
140: if (this.markers.size() > 0) {
141: LineMetrics metrics = this.font.getLineMetrics(
142: "123g", g2.getFontRenderContext()
143: );
144: result = this.topOuterGap + this.topInnerGap + metrics.getHeight()
145: + this.bottomInnerGap + this.bottomOuterGap;
146: }
147: return result;
148:
149: }
150:
151:
159: private void drawStringInRect(Graphics2D g2, Rectangle2D bounds, Font font,
160: String text) {
161:
162: g2.setFont(font);
163: FontMetrics fm = g2.getFontMetrics(font);
164: Rectangle2D r = TextUtilities.getTextBounds(text, g2, fm);
165: double x = bounds.getX();
166: if (r.getWidth() < bounds.getWidth()) {
167: x = x + (bounds.getWidth() - r.getWidth()) / 2;
168: }
169: LineMetrics metrics = font.getLineMetrics(
170: text, g2.getFontRenderContext()
171: );
172: g2.drawString(
173: text, (float) x, (float) (bounds.getMaxY()
174: - this.bottomInnerGap - metrics.getDescent())
175: );
176: }
177:
178:
187: public void draw(Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea,
188: double x, double y) {
189:
190: double h = getHeight(g2);
191: Iterator iterator = this.markers.iterator();
192: while (iterator.hasNext()) {
193: IntervalMarker marker = (IntervalMarker) iterator.next();
194: double start = Math.max(
195: marker.getStartValue(), this.axis.getRange().getLowerBound()
196: );
197: double end = Math.min(
198: marker.getEndValue(), this.axis.getRange().getUpperBound()
199: );
200: double s = this.axis.valueToJava2D(
201: start, dataArea, RectangleEdge.BOTTOM
202: );
203: double e = this.axis.valueToJava2D(
204: end, dataArea, RectangleEdge.BOTTOM
205: );
206: Rectangle2D r = new Rectangle2D.Double(
207: s, y + this.topOuterGap, e - s,
208: h - this.topOuterGap - this.bottomOuterGap
209: );
210:
211: Composite originalComposite = g2.getComposite();
212: g2.setComposite(AlphaComposite.getInstance(
213: AlphaComposite.SRC_OVER, marker.getAlpha())
214: );
215: g2.setPaint(marker.getPaint());
216: g2.fill(r);
217: g2.setPaint(marker.getOutlinePaint());
218: g2.draw(r);
219: g2.setComposite(originalComposite);
220:
221: g2.setPaint(Color.black);
222: drawStringInRect(g2, r, this.font, marker.getLabel());
223: }
224:
225: }
226:
227:
235: public boolean equals(Object obj) {
236: if (obj == this) {
237: return true;
238: }
239: if (!(obj instanceof MarkerAxisBand)) {
240: return false;
241: }
242: MarkerAxisBand that = (MarkerAxisBand) obj;
243: if (this.topOuterGap != that.topOuterGap) {
244: return false;
245: }
246: if (this.topInnerGap != that.topInnerGap) {
247: return false;
248: }
249: if (this.bottomInnerGap != that.bottomInnerGap) {
250: return false;
251: }
252: if (this.bottomOuterGap != that.bottomOuterGap) {
253: return false;
254: }
255: if (!ObjectUtilities.equal(this.font, that.font)) {
256: return false;
257: }
258: if (!ObjectUtilities.equal(this.markers, that.markers)) {
259: return false;
260: }
261: return true;
262: }
263:
264:
269: public int hashCode() {
270: int result = 37;
271: result = 19 * result + this.font.hashCode();
272: result = 19 * result + this.markers.hashCode();
273: return result;
274: }
275:
276: }