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